{"version":3,"sources":["../src/core/index.ts"],"sourcesContent":["import type { StoreApi } from 'zustand'\n\n/** The parameter of a `setState` function */\nexport type SetStateFnParam<T> = T | ((oldState: T) => T)\n/** The type of a `setState` function. Compatible with React `setState` functions. */\nexport type SetStateFn<T> = (newState: SetStateFnParam<T>) => void\n\n/**\n * Creates the `setState` function for a given piece of state.\n *\n * ```ts\n *  import { create } 'zustand';\n *  import { createSetterFn, type SetStateFn } from 'zustand-create-setter-fn';\n *\n *  interface CounterStore {\n *    count: number\n *    setCount: SetStateFn<number>\n *    increment: () => void\n *    reset: () => void\n * }\n *\n * const useCounterStore = create<CounterStore>()((set) => {\n *   // Use this function like you would any React setState function\n *   const setCount = createSetterFn(set, 'count')\n *\n *   return {\n *     count: 0,\n *     setCount,\n *     // Pass in a function to use the previous state as part of the new state\n *     increment: () => setCount(oldCount => oldCount + 1),\n *     // Pass in a value directly to set the state to that exact value\n *     reset: () => setCount(0),\n *   }\n * })\n * ```\n */\nexport function createSetterFn<\n\tState extends Record<string, any>,\n\tKey extends keyof State,\n>(set: StoreApi<State>['setState'], key: Key): SetStateFn<State[Key]> {\n\ttype Value = State[Key]\n\ttype FunctionState = (oldState: Value) => Value\n\n\tfunction setterFn(newState: SetStateFnParam<Value>): void {\n\t\tset((oldState) => ({\n\t\t\t...oldState,\n\t\t\t[key]:\n\t\t\t\ttypeof newState === 'function'\n\t\t\t\t\t? (newState as FunctionState)(oldState[key])\n\t\t\t\t\t: newState,\n\t\t}))\n\t}\n\n\treturn setterFn\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAoCO,SAAS,eAGd,KAAkC,KAAkC;AAIrE,WAAS,SAAS,UAAwC;AACzD,QAAI,CAAC,cAAc;AAAA,MAClB,GAAG;AAAA,MACH,CAAC,GAAG,GACH,OAAO,aAAa,aAChB,SAA2B,SAAS,GAAG,CAAC,IACzC;AAAA,IACL,EAAE;AAAA,EACH;AAEA,SAAO;AACR;","names":[]}