{"version":3,"sources":["../../src/vanilla/create.ts"],"sourcesContent":["import { withSnapshot } from './enhancers/with-snapshot.js'\nimport { withSubscribe } from './enhancers/with-subscribe.js'\nimport { proxy } from './proxy.js'\nimport { deepCloneWithRef } from './ref.js'\n\nimport type { SubscribeListener } from './subscribe.js'\nimport type { WithSnapshotContributes } from './enhancers/with-snapshot.js'\nimport type { WithSubscribeContributes } from './enhancers/with-subscribe.js'\n\n// biome-ignore lint/suspicious/noEmptyInterface: for future use\nexport interface StoreCreateOptions {}\n\nexport type StoreSubscriber<State extends object> = (\n  /**\n   * Callback to be called when state changes.\n   */\n  callback: SubscribeListener<State>,\n  /**\n   * Whether to sync the callback with the current state.\n   */\n  sync?: boolean,\n  /**\n   * @deprecated It is confusing, and makes TS type wrong in callback's `changes` argument. It will be removed in the next major version.\n   */\n  selector?: (state: State) => object\n) => () => void\n\nexport interface VanillaStore<State extends object> {\n  /**\n   * The mutable state object, whose changes will trigger subscribers.\n   */\n  mutate: State\n  /**\n   * Restore to initial state.\n   */\n  restore: (options?: RestoreOptions<State>) => void\n}\n\nexport interface RestoreOptions<State extends object> {\n  /**\n   * Exclude some **top** keys from restoring.\n   *\n   * @since 0.2.5\n   */\n  exclude?: (keyof State)[]\n}\n\n/**\n * Create a vanilla store.\n *\n * @param initState The initial state object.\n * @param options Options for creating the store.\n *\n * @since 0.2.0\n */\nexport function createVanilla<State extends object>(\n  initState: State,\n  _options: StoreCreateOptions = {}\n): VanillaStore<State> & WithSubscribeContributes<State> & WithSnapshotContributes<State> {\n  const proxyState = proxy(initState)\n\n  function restore(options: RestoreOptions<State> = {}) {\n    const { exclude = [] } = options\n\n    const clonedState = deepCloneWithRef(initState)\n    const initialKeys = Object.keys(initState)\n\n    for (const key of Object.keys(clonedState) as (keyof State)[]) {\n      if (exclude.includes(key)) continue\n      proxyState[key] = clonedState[key]\n    }\n\n    const addedKeys = Object.keys(proxyState).filter((key) => !initialKeys.includes(key))\n\n    for (const key of addedKeys as (keyof State)[]) {\n      if (exclude.includes(key)) continue\n      // delete keys that are not in the initial state\n      delete proxyState[key]\n    }\n  }\n\n  const store = { mutate: proxyState, restore }\n\n  return withSubscribe(withSnapshot(store))\n}\n\nexport const create = createVanilla\n"],"names":["create","createVanilla","initState","_options","proxyState","proxy","restore","options","exclude","clonedState","deepCloneWithRef","initialKeys","Object","keys","key","includes","addedKeys","filter","store","mutate","withSubscribe","withSnapshot"],"mappings":";;;;;;;;;;;QAsFaA;eAAAA;;QA/BGC;eAAAA;;;;;;;AAAT,SAASA,cACdC,SAAgB;QAChBC,WAAAA,iEAA+B,CAAC;IAEhC,IAAMC,aAAaC,IAAAA,eAAK,EAACH;IAEzB,SAASI;YAAQC,UAAAA,iEAAiC,CAAC;QACjD,uBAAyBA,QAAjBC,SAAAA,wCAAU,EAAE;QAEpB,IAAMC,cAAcC,IAAAA,wBAAgB,EAACR;QACrC,IAAMS,cAAcC,OAAOC,IAAI,CAACX;YAE3B,kCAAA,2BAAA;;YAAL,QAAK,YAAaU,OAAOC,IAAI,CAACJ,iCAAzB,SAAA,6BAAA,QAAA,yBAAA,iCAA0D;gBAA1D,IAAMK,MAAN;gBACH,IAAIN,QAAQO,QAAQ,CAACD,MAAM;gBAC3BV,UAAU,CAACU,IAAI,GAAGL,WAAW,CAACK,IAAI;YACpC;;YAHK;YAAA;;;qBAAA,6BAAA;oBAAA;;;oBAAA;0BAAA;;;;QAKL,IAAME,YAAYJ,OAAOC,IAAI,CAACT,YAAYa,MAAM,CAAC,SAACH;mBAAQ,CAACH,YAAYI,QAAQ,CAACD;;YAE3E,mCAAA,4BAAA;;YAAL,QAAK,aAAaE,8BAAb,UAAA,8BAAA,SAAA,0BAAA,kCAA2C;gBAA3C,IAAMF,OAAN;gBACH,IAAIN,QAAQO,QAAQ,CAACD,OAAM;gBAC3B,gDAAgD;gBAChD,OAAOV,UAAU,CAACU,KAAI;YACxB;;YAJK;YAAA;;;qBAAA,8BAAA;oBAAA;;;oBAAA;0BAAA;;;;IAKP;IAEA,IAAMI,QAAQ;QAAEC,QAAQf;QAAYE,SAAAA;IAAQ;IAE5C,OAAOc,IAAAA,+BAAa,EAACC,IAAAA,6BAAY,EAACH;AACpC;AAEO,IAAMlB,SAASC"}