{"version":3,"file":"index.mjs","sources":["../../../src/utils/create-zustand-context/index.tsx"],"sourcesContent":["import { createContext, useContext, useState } from 'react';\nimport type { StoreApi, StateCreator } from 'zustand';\nimport { useStore as useZustandStore, createStore } from 'zustand';\nimport { combine, devtools } from 'zustand/middleware';\nimport { immer } from 'zustand/middleware/immer';\nimport type { WithImmer, Write } from './with-immer';\nimport type { WithDevtools } from './with-devtools';\n\ntype StoreType<TState, TActions> = WithImmer<\n  WithDevtools<StoreApi<Write<TState, TActions>>>\n>;\n\ntype ExtractState<S> = S extends {\n  getState: () => infer T;\n}\n  ? T\n  : never;\ntype WithReact<S extends StoreApi<unknown>> = S & {\n  getServerState?: () => ExtractState<S>;\n};\n\nexport type StateSelector<S extends WithReact<StoreApi<unknown>>> = {\n  (): ExtractState<S>;\n  <U>(selector: (state: ExtractState<S>) => U, equals?: (a: U, b: U) => boolean): U;\n};\n\nexport type ProviderProps<TState extends object, TActions extends object> = {\n  name?: string;\n  initialState: TState;\n  actions: StateCreator<\n    TState & TActions,\n    [['zustand/devtools', never], ['zustand/immer', never]],\n    [],\n    TActions\n  >;\n};\n\nexport const createZustandContext = <\n  TState extends object,\n  TActions extends object\n>() => {\n  const Context = createContext<StoreType<TState, TActions> | null>(null);\n\n  const useStore: StateSelector<StoreApi<TState & TActions>> = ((\n    selector: any,\n    equals: any\n  ) => {\n    const store = useContext(Context);\n    if (!store) throw new Error('using columns outside a provider');\n    return useZustandStore(store, selector, equals);\n  }) as any;\n\n  const Provider: React.FC<React.PropsWithChildren<ProviderProps<TState, TActions>>> = ({\n    initialState,\n    name,\n\n    actions,\n\n    children,\n  }) => {\n    const [store] = useState<any>(() =>\n      createStore(devtools(combine(initialState, immer(actions as any)), { name }))\n    );\n\n    return <Context.Provider value={store}>{children}</Context.Provider>;\n  };\n\n  return [Provider, useStore] as const;\n};\n"],"names":["createZustandContext","Context","createContext","useStore","selector","equals","store","useContext","Error","useZustandStore","Provider","initialState","name","actions","children","useState","createStore","devtools","combine","immer","_jsx","value"],"mappings":";;;;;;AAqCO,MAAMA,uBAAuB,IAG7B;IACL,MAAMC,OAAAA,iBAAUC,cAAkD,IAAI,CAAA,CAAA;IAEtE,MAAMC,UAAAA,GAAwD,CAC5DC,QAAAA,EACAC,MACG,GAAA;AACH,QAAA,MAAMC,QAAQC,UAAWN,CAAAA,OAAAA,CAAAA,CAAAA;AACzB,QAAA,IAAI,CAACK,KAAAA,EAAO,MAAM,IAAIE,MAAM,kCAAoC,CAAA,CAAA;QAChE,OAAOC,QAAAA,CAAgBH,OAAOF,QAAUC,EAAAA,MAAAA,CAAAA,CAAAA;AAC1C,KAAA,CAAA;IAEA,MAAMK,QAAAA,GAA+E,CAAC,EACpFC,YAAY,GACZC,IAAI,GAEJC,OAAO,GAEPC,QAAQ,GACT,GAAK;QACJ,MAAM,CAACR,KAAM,CAAA,GAAGS,QAAc,CAAA,IAC5BC,YAAYC,QAASC,CAAAA,OAAAA,CAAQP,YAAcQ,EAAAA,KAAAA,CAAMN,OAAkB,CAAA,CAAA,EAAA;AAAED,gBAAAA,IAAAA;AAAK,aAAA,CAAA,CAAA,CAAA,CAAA;QAG5E,qBAAOQ,GAAA,CAACnB,QAAQS,QAAQ,EAAA;YAACW,KAAOf,EAAAA,KAAAA;AAAQQ,YAAAA,QAAAA,EAAAA,QAAAA;;AAC1C,KAAA,CAAA;IAEA,OAAO;AAACJ,QAAAA,QAAAA;AAAUP,QAAAA,UAAAA;AAAS,KAAA,CAAA;AAC7B;;;;"}