{"version":3,"file":"create-store.cjs","names":[],"sources":["../../src/store/create-store.ts"],"sourcesContent":["import { create } from \"zustand\";\nimport type { StateCreator, StoreApi, UseBoundStore } from \"zustand\";\nimport { persist, createJSONStorage } from \"zustand/middleware\";\nimport type { PersistOptions } from \"zustand/middleware\";\n\n/**\n * Persistence options for {@link createStore}. Mirrors the subset of Zustand's\n * `persist` middleware that apps usually need, with sensible Tempest defaults.\n */\nexport interface CreateStorePersistOptions<T> {\n    /** Storage key (required to enable persistence). */\n    name: string;\n    /** Which Web Storage to use (default: `\"local\"`). */\n    storage?: \"local\" | \"session\";\n    /**\n     * Pick the slice of state to persist. Defaults to persisting the whole\n     * state. Use this to avoid writing transient/derived fields.\n     */\n    partialize?: PersistOptions<T, Partial<T>>[\"partialize\"];\n    /** Persisted schema version, forwarded to Zustand for migrations. */\n    version?: number;\n    /** Migration function forwarded to Zustand's `persist` middleware. */\n    migrate?: PersistOptions<T, Partial<T>>[\"migrate\"];\n}\n\n/**\n * Options accepted by {@link createStore}.\n */\nexport interface CreateStoreOptions<T> {\n    /** Enable `localStorage`/`sessionStorage` persistence for the store. */\n    persist?: CreateStorePersistOptions<T>;\n}\n\n/**\n * Create a typed Zustand store with optional `persist` middleware, wired with\n * Tempest defaults. This is the generic counterpart to `createAuthStore`: use\n * it for any domain slice (cart, preferences, wizard state, …) without\n * re-writing the persistence boilerplate in every app.\n *\n * @typeParam T - The store state shape (state + actions).\n * @param initializer - Standard Zustand `StateCreator` (`(set, get) => state`).\n * @param options - Optional persistence configuration.\n * @returns A bound Zustand hook (`useStore`) with the usual `getState`/`setState`/`subscribe` API.\n *\n * @example\n * interface CartState {\n *     items: string[];\n *     add: (id: string) => void;\n *     clear: () => void;\n * }\n *\n * const useCart = createStore<CartState>(\n *     (set) => ({\n *         items: [],\n *         add: (id) => set((s) => ({ items: [...s.items, id] })),\n *         clear: () => set({ items: [] }),\n *     }),\n *     { persist: { name: \"cart\", partialize: (s) => ({ items: s.items }) } },\n * );\n */\nexport function createStore<T>(\n    initializer: StateCreator<T, [], []>,\n    options: CreateStoreOptions<T> = {},\n): UseBoundStore<StoreApi<T>> {\n    if (!options.persist) {\n        return create<T>()(initializer);\n    }\n\n    const { name, storage = \"local\", partialize, version, migrate } = options.persist;\n    const storageImpl = storage === \"session\" ? () => sessionStorage : () => localStorage;\n\n    return create<T>()(\n        persist(initializer, {\n            name,\n            storage: createJSONStorage(storageImpl),\n            ...(partialize ? { partialize } : {}),\n            ...(version !== undefined ? { version } : {}),\n            ...(migrate ? { migrate } : {}),\n        }),\n    );\n}\n"],"mappings":"yDA4DA,SAAgB,EACZ,EACA,EAAiC,CAAC,EACR,CAC1B,GAAI,CAAC,EAAQ,QACT,OAAA,EAAO,EAAA,OAAA,CAAU,CAAC,CAAC,CAAW,EAGlC,GAAM,CAAE,OAAM,UAAU,QAAS,aAAY,UAAS,WAAY,EAAQ,QACpE,EAAc,IAAY,cAAkB,mBAAuB,aAEzE,OAAA,EAAO,EAAA,OAAA,CAAU,CAAC,EAAA,EACd,EAAA,QAAA,CAAQ,EAAa,CACjB,OACA,SAAA,EAAS,EAAA,kBAAA,CAAkB,CAAW,EACtC,GAAI,EAAa,CAAE,YAAW,EAAI,CAAC,EACnC,GAAI,IAAY,IAAA,GAA0B,CAAC,EAAf,CAAE,SAAQ,EACtC,GAAI,EAAU,CAAE,SAAQ,EAAI,CAAC,CACjC,CAAC,CACL,CACJ"}