import { createContext, useContext, useMemo, type ReactNode } from 'react' import { WailsCache, wailsCache } from '../core/wails_cache' import type { QueryOptions, MutationOptions } from '../../types/ui/wails_ipc' interface WailsContextValue { cache: WailsCache defaultQueryOptions: Partial defaultMutationOptions: Partial } const WailsContext = createContext(null) export interface WailsProviderProps { children: ReactNode /** Usar un cache custom en lugar del singleton */ cache?: WailsCache /** Opciones por defecto para queries */ defaultQueryOptions?: Partial /** Opciones por defecto para mutations */ defaultMutationOptions?: Partial } export function WailsProvider({ children, cache, defaultQueryOptions = {}, defaultMutationOptions = {}, }: WailsProviderProps) { const value = useMemo( () => ({ cache: cache ?? wailsCache, defaultQueryOptions, defaultMutationOptions, }), [cache, defaultQueryOptions, defaultMutationOptions] ) return ( {children} ) } export function useWailsContext(): WailsContextValue { const context = useContext(WailsContext) if (!context) { // Fallback to singleton cache if no provider return { cache: wailsCache, defaultQueryOptions: {}, defaultMutationOptions: {}, } } return context } export function useWailsCache(): WailsCache { return useWailsContext().cache }