{"version":3,"sources":["../../../src/react/enhancers/with-use-snapshot.ts"],"sourcesContent":["import { useSnapshot } from '../use-snapshot.js'\nimport { isFunction } from '../../utils/index.js'\n\nimport type { VanillaStore } from '../../vanilla/create.js'\nimport type { SnapshotOptions } from '../use-snapshot.js'\nimport type { StoreUseSnapshot } from '../create-with-hooks.js'\nimport type { SnapshotSelector } from '../../vanilla/snapshot.js'\n\nexport interface WithUseSnapshotContributes<State extends object> {\n  /**\n   * Get the snapshot of the state.\n   */\n  useSnapshot: StoreUseSnapshot<State>\n}\n\n/**\n * Enhances a store with `useSnapshot` method that returns a snapshot of the store's state.\n *\n * @param store - The store to enhance.\n * @returns The enhanced store.\n *\n * @since 0.2.0\n *\n * @example\n *\n * ```tsx\n * const store = withUseSnapshot(\n *   createVanilla({\n *     count: 123,\n *     info: { name: 'Viki' },\n *   }),\n * )\n *\n * // in normal JS/TS\n * console.log(store.snapshot(), store.snapshot((s) => s.count))\n *\n * // in React component\n * export function App() {\n *   const { count } = store.useSnapshot()\n *   const count = store.useSnapshot((s) => s.count)\n * }\n *\n * ```\n */\nexport function withUseSnapshot<Store extends VanillaStore<object>>(\n  store: Store\n): Store & WithUseSnapshotContributes<Store['mutate']> {\n  const boundUseSnapshot: StoreUseSnapshot<Store['mutate']> = <StateSlice>(\n    selectorOrOption?:\n      | SnapshotOptions<StateSlice>\n      | SnapshotSelector<Store['mutate'], StateSlice>\n      | undefined,\n    maybeOptions?: SnapshotOptions<StateSlice>\n  ) => {\n    let options: SnapshotOptions<StateSlice> | undefined\n    let selector: SnapshotSelector<Store['mutate'], StateSlice> | undefined\n\n    if (selectorOrOption && !isFunction(selectorOrOption)) {\n      options = selectorOrOption\n      selector = undefined\n    } else {\n      options = maybeOptions\n      selector = selectorOrOption\n    }\n\n    return useSnapshot(store.mutate, selector, options)\n  }\n\n  return {\n    ...store,\n    useSnapshot: boundUseSnapshot,\n  }\n}\n"],"names":["withUseSnapshot","store","boundUseSnapshot","selectorOrOption","maybeOptions","options","selector","isFunction","undefined","useSnapshot","mutate"],"mappings":";;;;+BA4CgBA;;;eAAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAT,SAASA,gBACdC,KAAY;IAEZ,IAAMC,mBAAsD,SAC1DC,kBAIAC;QAEA,IAAIC;QACJ,IAAIC;QAEJ,IAAIH,oBAAoB,CAACI,IAAAA,oBAAU,EAACJ,mBAAmB;YACrDE,UAAUF;YACVG,WAAWE;QACb,OAAO;YACLH,UAAUD;YACVE,WAAWH;QACb;QAEA,OAAOM,IAAAA,2BAAW,EAACR,MAAMS,MAAM,EAAEJ,UAAUD;IAC7C;IAEA,OAAO,wCACFJ;QACHQ,aAAaP;;AAEjB"}