{"version":3,"sources":["../../../src/react/enhancers/with-use-derived.ts"],"sourcesContent":["import { useSnapshot } from '../use-snapshot.js'\nimport { withDerived } from '../../vanilla/enhancers/with-derived.js'\n\nimport type { VanillaStore } from '../../vanilla/index.js'\nimport type { SnapshotOptions } from '../use-snapshot.js'\nimport type { WithDerivedContributes } from '../../vanilla/enhancers/with-derived.js'\n\nexport interface WithUseDerivedContributes<State extends object, Derived extends object = State>\n  extends WithDerivedContributes<State, Derived> {\n  /**\n   * Get the derived state.\n   */\n  useDerived: (options?: SnapshotOptions<Derived>) => Derived\n}\n\n/**\n * Enhances a store with `useDerived` & `derived` method that returns the derived state.\n * \n * @param store - The store to enhance.\n * @param mapFn - The function to map the state to the derived state.\n * @returns The enhanced store.\n * \n * @since 0.2.0\n * \n * @example\n * \n * ```tsx\n * const store = withDerived(\n *  create({\n *    count: 123,\n *    info: { name: 'Viki' },\n *  }),\n *  (s) => ({\n *    isViki: s.info.name === 'Viki',\n *    isNegative: s.count < 0,\n *  }),\n * )\n\n * // in normal JS/TS\n * console.log(store.derived().isViki)\n\n * // in React component\n * export function App() {\n *  const { isNegative } = store.useDerived()\n * }\n * ```\n */\nexport function withUseDerived<\n  Store extends VanillaStore<object>,\n  Derived extends object = Store['mutate'],\n>(\n  store: Store,\n  mapFn: (state: Store['mutate']) => Derived = (s) => s as unknown as Derived\n): Store & WithUseDerivedContributes<Store['mutate'], Derived> {\n  const storeWithDerived = withDerived<Store, Derived>(store, mapFn)\n\n  return {\n    ...storeWithDerived,\n    useDerived: (options: SnapshotOptions<Derived> = {}) => {\n      return useSnapshot<Store['mutate'], Derived>(store.mutate, mapFn, options)\n    },\n  }\n}\n"],"names":["withUseDerived","store","mapFn","s","storeWithDerived","withDerived","useDerived","options","useSnapshot","mutate"],"mappings":";;;;+BA+CgBA;;;eAAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAT,SAASA,eAIdC,KAAY;QACZC,QAAAA,iEAA6C,SAACC;eAAMA;;IAEpD,IAAMC,mBAAmBC,IAAAA,2BAAW,EAAiBJ,OAAOC;IAE5D,OAAO,wCACFE;QACHE,YAAY;gBAACC,2EAAoC,CAAC;YAChD,OAAOC,IAAAA,2BAAW,EAA2BP,MAAMQ,MAAM,EAAEP,OAAOK;QACpE;;AAEJ"}