{"version":3,"sources":["jsdelivr-header.js","/npm/@reatom/lens@3.12.0/src/utils.ts","/npm/@reatom/lens@3.12.0/src/match.ts","/npm/@reatom/lens@3.12.0/src/bind.ts","/npm/@reatom/lens@3.12.0/src/delay.ts","/npm/@reatom/lens@3.12.0/src/effect.ts","/npm/@reatom/lens@3.12.0/src/filter.ts","/npm/@reatom/lens@3.12.0/src/onLensUpdate.ts","/npm/@reatom/lens@3.12.0/src/parseAtoms.ts","/npm/@reatom/lens@3.12.0/src/sample.ts","/npm/@reatom/lens@3.12.0/src/select.ts","/npm/@reatom/lens@3.12.0/src/withReset.ts","/npm/@reatom/lens@3.12.0/src/index.ts"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA,05BCJa,QAAU,EACnB,SAAU,GACZ,EACA,IACG,GAAA,GAAW,EAAM,QAAQ,ICkDvB,SAAS,MACd,EACA,GAAO,EAAA,cAAA,SAAQ,UAMf,MAAM,EAAqB,GAC3B,IAAI,EACA,EACA,EAEJ,MAAM,GAAY,EAAA,cAAA,OAAM,IACtB,MAAM,GAAQ,EAAA,cAAA,QAAO,GACjB,EAAO,IAAI,GACW,mBAAf,EACL,EAAmB,GACnB,EAEA,EAAO,IAAI,EAAO,EAAS,EAAQ,GAAU,OAAO,SAE1D,IAAK,MAAM,OAAE,EAAA,UAAQ,KAAe,EAClC,GAAI,EAAO,EAAQ,GACjB,OAAO,EAAA,cAAA,QAAO,GACV,EAAO,IAAI,GACU,mBAAd,EACL,EAAU,EAAQ,GAClB,CAGH,GACN,GAEH,OAAO,OAAO,OAAO,EAAW,CAC9B,GAAA,CAAG,EAAa,KACd,EAAM,KAAK,CACT,QAAQ,EAAA,cAAA,QAAO,GACX,CAAC,EAAK,IAAU,OAAO,GAAG,EAAO,EAAI,IAAI,IACvB,mBAAX,EACL,EACA,CAAC,EAAK,IAAU,OAAO,GAAG,EAAO,GACvC,cAEK,GAET,KAAA,CAAK,EAAW,KACd,EAAM,KAAK,CACT,OAAQ,C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C,EAAA,cAAA,kBAAiB,EAAO,SAAS,SAAU,iBAExB,iBAAR,IACT,EAAO,EACP,OAAM,GAER,IAAQ,CAAC,EAAU,IAAW,EAG9B,MAAM,GAAgC,EAAA,cAAA,OACnC,IAEC,MAAM,EAAmC,IAA1B,EAAI,MAAM,KAAK,OACxB,EAAQ,EAAI,IAAI,GACtB,OAAO,EACH,GACA,CAAC,CAAE,OAAQ,CAAC,GAAQ,QAAS,EAAY,EAAK,IAAS,GAE7D,QAAQ,EAAQ,WAAY,IAK9B,OAHA,EAAU,SAAS,UAAW,EAC9B,EAAU,KAAO,CAAC,GAEX,CAAA,E","file":"/npm/@reatom/lens@3.12.0/build/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@reatom/lens@3.12.0/build/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import { Atom } from '@reatom/core'\n\nexport const mapName = (\n  { __reatom: proto }: Atom,\n  operator: string,\n  name?: string,\n) => name ?? `${proto.name}.${operator}`\n","import {\n  Atom,\n  Ctx,\n  CtxSpy,\n  Fn,\n  __count,\n  atom,\n  isAtom,\n  throwReatomError,\n} from '@reatom/core'\nimport { isRec, isShallowEqual, merge } from '@reatom/utils'\n\ntype Primitive = null | undefined | string | number | boolean | symbol | bigint\n\nexport type BuiltIns = Primitive | Date | RegExp\n\nexport type PartialDeep<T> = T extends BuiltIns\n  ? T | undefined\n  : T extends object\n    ? T extends ReadonlyArray<any>\n      ? never\n      : {\n          [K in keyof T]?: PartialDeep<T[K]>\n        }\n    : unknown\n\ninterface Match<Expression = any, State = never, Default = undefined>\n  extends Atom<State | Default> {\n  is<T, const MatchedExpression extends Expression = Expression>(\n    clause:\n      | MatchedExpression\n      | Atom<MatchedExpression>\n      | ((ctx: Ctx, expression: Expression) => boolean),\n    statement:\n      | T\n      | Atom<T>\n      | ((ctx: CtxSpy, expression: MatchedExpression) => T),\n  ): Match<Expression, State | T, Default>\n  with<T, Part extends PartialDeep<Expression>>(\n    part: Part,\n    statement?:\n      | T\n      | Atom<T>\n      | ((ctx: CtxSpy, expression: Part & Expression) => T),\n  ): Match<Exclude<Expression, Part>, T>\n  truthy<T>(\n    statement: T | Atom<T> | ((ctx: CtxSpy, expression: Expression) => T),\n  ): Match<Expression, State | T, Default>\n  falsy<T>(\n    statement: T | Atom<T> | ((ctx: CtxSpy, expression: Expression) => T),\n  ): Match<Expression, State | T, Default>\n  default<T = never>(\n    statement?: T | Atom<T> | ((ctx: CtxSpy, expression: Expression) => T),\n  ): Match<Expression, State, T>\n}\n\nexport function match<T>(\n  expression: T | Atom<T> | ((ctx: CtxSpy) => T),\n  name = __count('match'),\n): Match<T> {\n  type Case = {\n    clause: (ctx: CtxSpy, expression: T) => boolean\n    statement: {} | Atom | ((ctx: Ctx, expression: T) => any)\n  }\n  const cases: Array<Case> = []\n  let _truthy: Case\n  let _falsy: Case\n  let _default: Case\n\n  const matchAtom = atom((ctxSpy) => {\n    const value = isAtom(expression)\n      ? ctxSpy.spy(expression)\n      : typeof expression === 'function'\n        ? (expression as Fn)(ctxSpy)\n        : expression\n\n    const list = [...cases, _truthy, _falsy, _default].filter(Boolean)\n\n    for (const { clause, statement } of list) {\n      if (clause(ctxSpy, value)) {\n        return isAtom(statement)\n          ? ctxSpy.spy(statement)\n          : typeof statement === 'function'\n            ? statement(ctxSpy, value)\n            : statement\n      }\n    }\n    return undefined\n  }, name)\n\n  return Object.assign(matchAtom, {\n    is(clause: any, statement: any) {\n      cases.push({\n        clause: isAtom(clause)\n          ? (ctx, value) => Object.is(value, ctx.spy(clause))\n          : typeof clause === 'function'\n            ? clause\n            : (ctx, value) => Object.is(value, clause),\n        statement,\n      })\n      return matchAtom\n    },\n    with(part: any, statement: any) {\n      cases.push({\n        clause: (ctx, expr) => {\n          const visit = (part: any, expr: any) => {\n            if (isRec(part)) {\n              for (const key in part) {\n                if (!visit(part[key], expr[key])) return false\n              }\n              return true\n            }\n            return Object.is(part, expr)\n          }\n\n          return visit(part, expr)\n        },\n        statement,\n      })\n      return matchAtom\n    },\n    truthy(statement: any) {\n      throwReatomError(_truthy, 'the case is already defined')\n      _truthy = { clause: (ctx, value) => !!value, statement }\n      return matchAtom\n    },\n    falsy(statement: any) {\n      throwReatomError(_falsy, 'the case is already defined')\n      _falsy = { clause: (ctx, value) => !value, statement }\n      return matchAtom\n    },\n    default(statement = () => throwReatomError(true, 'no match') as never) {\n      throwReatomError(_default, 'the case is already defined')\n      _default = { clause: (ctx, value) => true, statement }\n      return matchAtom\n    },\n  }) as Match<T>\n}\n","import { Ctx, CtxParams, Fn } from '@reatom/core'\n\nexport type Binded<T extends Fn> = Fn<CtxParams<T>, ReturnType<T>>\n// & {\n//   [K in keyof T]: T[K]\n// }\n\nconst ctxMap = new WeakMap<Ctx, WeakMap<Fn, Binded<Fn>>>()\nexport const bind = <T extends Fn>(ctx: Ctx, fn: T): Binded<T> => {\n  let fnMap = ctxMap.get(ctx)\n  if (!fnMap) ctxMap.set(ctx, (fnMap = new WeakMap()))\n\n  let bfn = fnMap.get(fn)\n  if (!bfn) fnMap.set(fn, (bfn = fn.bind(null, ctx)))\n\n  return bfn as Binded<T>\n}\n","import {\n  Action,\n  atom,\n  Atom,\n  AtomCache,\n  AtomState,\n  Ctx,\n  Fn,\n  isAtom,\n  throwReatomError,\n} from '@reatom/core'\nimport { onCtxAbort } from '@reatom/effects'\nimport { mapName } from './utils'\nimport { type LensAtom, type LensAction } from './'\n\nexport interface DelayOptions {\n  /** The minimum amount of the delay (debounce-like)\n   * @default `max`\n   */\n  min?: number | Atom<number>\n  /** The maximum amount of the delay (throttle-like)\n   * @default `min`\n   */\n  max?: number | Atom<number>\n  /** Should the first update be captured (throttle-like)?\n   * @default true\n   */\n  leading?: boolean\n  /** Should the last update be captured (debounce-like)?\n   * @default true\n   */\n  trailing?: boolean\n  /** Should subscribe to an AbortController from the cause?\n   * @default true\n   */\n  abortable?: boolean\n}\n\n/** Flexible updates delayer */\nexport const delay: {\n  // TODO for some reason an atom not handled by overloads, if an action overload is first\n  <T extends Atom>(\n    options: DelayOptions,\n    name?: string,\n  ): Fn<\n    [T],\n    T extends Action<infer Params, infer Payload>\n      ? LensAction<Params, Payload>\n      : LensAtom<AtomState<T>>\n  >\n} = (options, name) => (anAtom: Atom) => {\n  // listeners is a unique object for each atom instance\n  const running = new WeakMap<AtomCache['listeners'], number>()\n  let {\n    min: minOption,\n    max: maxOption,\n    leading = true,\n    trailing = true,\n    abortable = true,\n  } = options\n\n  throwReatomError(\n    minOption === undefined && maxOption === undefined,\n    'wrong options',\n  )\n\n  minOption ??= maxOption\n  maxOption ??= Number.MAX_SAFE_INTEGER\n\n  name = mapName(anAtom, 'delay', name)\n\n  const minAtom = isAtom(minOption)\n    ? minOption\n    : atom(minOption, `${name}._minAtom`)\n  const maxAtom = isAtom(maxOption)\n    ? maxOption\n    : atom(maxOption, `${name}._maxAtom`)\n\n  // @ts-expect-error\n  const theAtom: LensAtom & LensAction = atom((ctx, prevState?: any) => {\n    const min = ctx.get(minAtom)\n    const max = ctx.get(maxAtom)\n    const startsKey = ctx.cause.listeners\n    const depState = ctx.spy(anAtom)\n    let state = ctx.cause.pubs.length\n      ? prevState\n      : proto.isAction\n        ? []\n        : depState\n\n    if (!ctx.cause.pubs.length) {\n      state = proto.isAction ? [] : depState\n    } else {\n      const now = Date.now()\n      const isRunning = running.has(startsKey)\n      const start = running.get(startsKey) ?? now\n      const skip = isRunning || !leading\n\n      state = skip ? prevState : proto.isAction ? [depState[0]] : depState\n\n      const delay = Math.max(0, Math.min(min!, max! - (now - start)))\n\n      running.set(startsKey, start!)\n      ctx.schedule(() => running.delete(startsKey), -1)\n\n      const timeoutId = setTimeout(\n        () =>\n          ctx.get((read, acualize) => {\n            if (read(proto) === ctx.cause) {\n              running.delete(startsKey)\n              if (trailing && skip) {\n                acualize!(ctx, proto, (patchCtx: Ctx, patch: AtomCache) => {\n                  patch.state = proto.isAction ? [depState.at(-1)] : depState\n                })\n              }\n            }\n          }),\n        delay,\n      )\n      timeoutId.unref?.()\n      ctx.schedule(() => clearTimeout(timeoutId), -1)\n      if (abortable) onCtxAbort(ctx, () => clearTimeout(timeoutId))\n    }\n\n    return state\n  }, name)\n  const proto = theAtom.__reatom\n  proto.isAction = anAtom.__reatom.isAction\n  theAtom.deps = [anAtom]\n\n  return theAtom as any\n}\n\n/** Delay updates by timeout */\nexport const debounce: {\n  // TODO for some reason an atom not handled by overloads, if an action overload is first\n  <T extends Atom>(\n    wait: DelayOptions['min'],\n    name?: string,\n  ): Fn<\n    [T],\n    T extends Action<infer Params, infer Payload>\n      ? LensAction<Params, Payload>\n      : LensAtom<AtomState<T>>\n  >\n} =\n  (min = 1, name) =>\n  (anAtom) =>\n    // @ts-expect-error\n    delay(\n      { min, leading: false, trailing: true },\n      mapName(anAtom, 'debounce', name),\n    )(anAtom)\n\n/** Skip updates by interval */\nexport const throttle: {\n  // TODO for some reason an atom not handled by overloads, if an action overload is first\n  <T extends Atom>(\n    wait: DelayOptions['max'],\n    name?: string,\n  ): Fn<\n    [T],\n    T extends Action<infer Params, infer Payload>\n      ? LensAction<Params, Payload>\n      : LensAtom<AtomState<T>>\n  >\n} =\n  (max = 1, name) =>\n  (anAtom) =>\n    // @ts-expect-error\n    delay(\n      { max, leading: true, trailing: false },\n      mapName(anAtom, 'throttle', name),\n    )(anAtom)\n","import {\n  Action,\n  atom,\n  Atom,\n  AtomCache,\n  AtomState,\n  Ctx,\n  Fn,\n  Unsubscribe,\n} from '@reatom/core'\nimport { __thenReatomed } from '@reatom/effects'\nimport { mapName } from './utils'\nimport { type LensAction } from './'\nimport { onUpdate } from '@reatom/hooks'\nimport { noop } from '@reatom/utils'\n\nexport interface LensEffect<Params extends any[] = any[], Payload = any>\n  extends LensAction<Params, Payload> {\n  unstable_unhook: Unsubscribe\n}\n\n/** Create action which will invoked with the result of effect */\n// @ts-expect-error\nexport const effect: {\n  // TODO for some reason an atom not handled by overloads, if an action overload is first\n  <T extends Atom, Res>(\n    fn: T extends Action<infer Params, infer Payload>\n      ? Fn<[Ctx, Awaited<Payload>, Params], Res>\n      : Fn<[Ctx, AtomState<T>], Res>,\n    name?: string,\n  ): Fn<\n    [T],\n    T extends Action<infer Params, infer Payload>\n      ? LensEffect<\n          [\n            {\n              params: [{ params: Params; payload: Awaited<Payload> }]\n              payload: Awaited<Res>\n            },\n          ],\n          Awaited<Res>\n        >\n      : LensEffect<\n          [{ params: [AtomState<T>]; payload: AtomState<T> }],\n          Awaited<Res>\n        >\n  >\n} = (fn: Fn, name?: string) => (anAtom: Atom) => {\n  const { isAction } = anAtom.__reatom\n  // TODO better error handling\n  // @ts-expect-error\n  const theAtom: LensEffect = atom(\n    (ctx, state = []) => {\n      const resolve = (params: any[], payload: any) =>\n        ctx.get((read, acualize) => {\n          if (payload instanceof Promise) {\n            payload.then((payload) => resolve(params, payload))\n          } else {\n            acualize!(\n              ctx,\n              ctx.cause.proto,\n              (patchCtx: Ctx, patch: AtomCache) => {\n                patch.state = [{ params, payload }]\n              },\n            )\n          }\n        })\n\n      ctx.spy(anAtom, (value) => {\n        if (isAction && value.payload instanceof Promise) {\n          __thenReatomed(ctx, value.payload, (payload) =>\n            ctx.schedule(() =>\n              resolve(\n                [{ params: value.params, payload }],\n                fn(ctx, payload, value.params),\n              ),\n            ),\n          )\n        } else {\n          ctx.schedule(() =>\n            resolve(\n              [value],\n              isAction ? fn(ctx, value.payload, value.params) : fn(ctx, value),\n            ),\n          )\n        }\n      })\n\n      // @ts-expect-error we don't need `spy` anymore\n      ctx.spy = undefined\n\n      return state ?? []\n    },\n    mapName(anAtom, 'effect', name),\n  )\n  theAtom.__reatom.isAction = true\n  theAtom.deps = [anAtom]\n  theAtom.unstable_unhook = onUpdate(theAtom, noop)\n\n  return theAtom\n}\n","import { Action, atom, Atom, AtomState, Ctx, CtxSpy, Fn } from '@reatom/core'\nimport { isShallowEqual } from '@reatom/utils'\nimport { mapName } from './utils'\nimport { type LensAtom, type LensAction } from './'\n\n/** Filter updates by comparator function (\"shallow equal\" for atoms by default) */\nexport const filter: {\n  // TODO for some reason an atom not handled by overloads, if an action overload is first\n  <T extends Atom>(\n    predicate?: T extends Action<infer Params, infer Payload>\n      ? Fn<[Ctx, Payload, Params], boolean>\n      : Fn<[CtxSpy, AtomState<T>, AtomState<T>], boolean>,\n    name?: string,\n  ): Fn<\n    [T],\n    T extends Action<infer Params, infer Payload>\n      ? LensAction<Params, Payload>\n      : T extends Atom<infer State>\n        ? LensAtom<State>\n        : never\n  >\n} =\n  (predicate?: Fn, name?: string) =>\n  (anAtom: Atom): any => {\n    const { isAction } = anAtom.__reatom\n\n    predicate ??= isAction ? () => true : (ctx, a, b) => !isShallowEqual(a, b)\n\n    // @ts-expect-error\n    const theAtom: LensAtom & LensAction = atom(\n      (ctx, prevState?: any) => {\n        const isInit = ctx.cause.pubs.length === 0\n        const state = ctx.spy(anAtom)\n\n        return isAction\n          ? state.reduce(\n              (acc: any, call: any) =>\n                predicate!(ctx, call.payload, call.params) ? [call] : acc,\n              prevState ?? [],\n            )\n          : isInit || predicate!(ctx, state, prevState)\n            ? state\n            : prevState\n      },\n      mapName(anAtom, 'filter', name),\n    )\n    theAtom.deps = [anAtom]\n    theAtom.__reatom.isAction = isAction\n\n    return theAtom\n  }\n","import { Atom } from '@reatom/core'\nimport { onUpdate } from '@reatom/hooks'\nimport { assign } from '@reatom/utils'\nimport { type LensAtom } from './'\n\n/** @deprecated use regular `onUpdate` instead */\nexport const onLensUpdate: typeof onUpdate = onUpdate\n\nexport const toLens =\n  <T extends Atom>(deps: Array<Atom>) =>\n  (\n    anAtom: T,\n  ): T & {\n    deps: Array<Atom>\n  } =>\n    assign(anAtom, {\n      deps: ((anAtom as Atom as LensAtom).deps ?? []).concat(deps),\n    })\n","import { Action, Atom, Ctx, isAction, isAtom, Rec } from '@reatom/core'\nimport {\n  isLinkedListAtom,\n  LinkedList,\n  LinkedListLikeAtom,\n  LLNode,\n} from '@reatom/primitives'\nimport { isRec } from '@reatom/utils'\n\ntype Primitive = string | number | boolean | null | undefined\ntype Builtin = Date | RegExp | Function\n\nexport type ParseAtoms<T> = T extends Action\n  ? T\n  : T extends LinkedListLikeAtom<infer T>\n  ? T extends LinkedList<LLNode<infer T>>\n  ? Array<ParseAtoms<T>>\n  : never\n  : T extends Atom<infer T>\n  ? ParseAtoms<T>\n  : T extends Map<infer K, infer T>\n  ? Map<K, ParseAtoms<T>>\n  : T extends Set<infer T>\n  ? Set<ParseAtoms<T>>\n  : T extends Array<infer T>\n  ? Array<ParseAtoms<T>>\n  : T extends Primitive | Builtin\n  ? T\n  : T extends object\n  ? {\n    [K in keyof T]: ParseAtoms<T[K]>\n  }\n  : T\n\nexport const parseAtoms = <Value>(\n  ctx: Ctx,\n  value: Value,\n): ParseAtoms<Value> => {\n  if (isAction(value)) return value as ParseAtoms<Value>\n\n  if (isLinkedListAtom(value)) value = value.array as any\n\n  while (isAtom(value)) value = ctx.spy ? ctx.spy(value) : ctx.get(value)\n\n  if (typeof value !== 'object' || value === null) return value as any\n\n  if (isRec(value)) {\n    const res = {} as Rec\n    for (const k in value) res[k] = parseAtoms(ctx, value[k])\n    return res as any\n  }\n\n  if (Array.isArray(value)) {\n    const res = []\n    for (const v of value) res.push(parseAtoms(ctx, v))\n    return res as any\n  }\n\n  if (value instanceof Map) {\n    const res = new Map()\n    for (const [k, v] of value) res.set(k, parseAtoms(ctx, v))\n    return res as any\n  }\n\n  if (value instanceof Set) {\n    const res = new Set()\n    for (const v of value) res.add(parseAtoms(ctx, v))\n    return res as any\n  }\n\n  return value as any\n}\n","import { Action, atom, Atom, AtomState, Fn } from '@reatom/core'\nimport { mapName } from './utils'\nimport { type LensAtom, type LensAction } from './'\n\n/** Delay updates until other atom update / action call */\n// https://rxjs.dev/api/operators/sample\n// https://effector.dev/docs/api/effector/sample\n// @ts-expect-error\nexport const sample: {\n  // TODO for some reason an atom not handled by overloads, if an action overload is first\n  <T extends Atom>(\n    signal: Atom,\n    name?: string,\n  ): Fn<\n    [T],\n    T extends Action<infer Params, infer Payload>\n      ? LensAction<[], Payload>\n      : LensAtom<AtomState<T>>\n  >\n} =\n  <T>(signal: Atom, name?: string) =>\n  // @ts-ignore\n  (anAtom) => {\n    name = mapName(anAtom, 'sample', name)\n    const { isAction } = anAtom.__reatom\n    const _cacheAtom = atom<unknown>(null, `${name}._cacheAtom`)\n\n    // @ts-expect-error\n    const theAtom: LensAtom & LensAction = atom((ctx, prevState?: any) => {\n      const patch = ctx.cause\n      const isInit = patch.pubs.length === 0\n\n      ctx.spy(anAtom, (v) => _cacheAtom(ctx, isAction ? [v] : v))\n\n      let changed = false\n      ctx.spy(signal, () => (changed = true))\n\n      if (changed && !(isInit && !signal.__reatom.isAction)) {\n        const state = ctx.get(_cacheAtom)\n        // drop action cache\n        _cacheAtom(ctx, ctx.get(anAtom))\n        return state\n      }\n\n      return isInit && !(signal.__reatom.isAction && changed)\n        ? isAction\n          ? []\n          : ctx.get(anAtom)\n        : changed\n          ? ctx.get(_cacheAtom)\n          : prevState\n    }, name)\n    theAtom.__reatom.isAction = isAction\n    theAtom.deps = [anAtom, signal]\n\n    return theAtom\n  }\n","import {\n  Atom,\n  AtomProto,\n  Ctx,\n  CtxSpy,\n  __count,\n  atom,\n  throwReatomError,\n} from '@reatom/core'\n\ntype FunctionSource = string\n\nconst mapAtom = atom(\n  null as any as WeakMap<AtomProto, Map<FunctionSource, Atom>>,\n  'select._map',\n)\nmapAtom.__reatom.initState = () => new WeakMap()\n\nconst touchedMap = new WeakMap<Ctx, Set<FunctionSource>>()\n\nexport const select = <T>(\n  ctx: CtxSpy,\n  cb: (ctx: CtxSpy) => T,\n  equal: (oldState: T, newState: T) => boolean = () => false,\n): T => {\n  let touched = touchedMap.get(ctx)\n  if (!touched) {\n    touchedMap.set(ctx, (touched = new Set()))\n  }\n\n  const map = ctx.get(mapAtom)\n  let atoms = map.get(ctx.cause.proto)\n\n  if (!atoms) {\n    map.set(ctx.cause.proto, (atoms = new Map()))\n  }\n\n  const selectSource = cb.toString()\n\n  throwReatomError(\n    touched.has(selectSource),\n    'multiple select with the same \"toString\" representation is not allowed',\n  )\n  touched.add(selectSource)\n\n  let selectAtom = atoms.get(selectSource)\n  if (!selectAtom) {\n    let isInit = true\n    atoms.set(\n      selectSource,\n      (selectAtom = atom(\n        (ctx, prevState?: any) => {\n          const newState = cb(ctx)\n          const resultState =\n            isInit || !equal(prevState, newState) ? newState : prevState\n          isInit = false\n          return resultState\n        },\n        __count(`${ctx.cause.proto.name}._select`),\n      )),\n    )\n  }\n\n  return ctx.spy(selectAtom!)\n}\n","import { action, Action, Atom, AtomCache, AtomState, Ctx } from '@reatom/core'\n\nexport const withReset =\n  <T extends Atom>() =>\n  (anAtom: T): T & { reset: Action<[], AtomState<T>> } =>\n    Object.assign(anAtom, {\n      reset: action(\n        (ctx) =>\n          ctx.get(\n            (read, actualize) =>\n              actualize!(\n                ctx,\n                anAtom.__reatom,\n                (patchCtx: Ctx, patch: AtomCache) =>\n                  (patch.state = patch.proto.initState(ctx)),\n              ).state,\n          ),\n        `${anAtom.__reatom.name}._reset`,\n      ),\n    })\n","import {\n  __count,\n  action,\n  Action,\n  ActionPayload,\n  atom,\n  Atom,\n  AtomCache,\n  AtomMut,\n  AtomState,\n  Ctx,\n  CtxParams,\n  CtxSpy,\n  Fn,\n  Rec,\n  throwReatomError,\n} from '@reatom/core'\nimport { __thenReatomed } from '@reatom/effects'\nimport { onUpdate } from '@reatom/hooks'\nimport { Plain } from '@reatom/utils'\nimport { mapName } from './utils'\n\nexport * from './match'\nexport * from './bind'\nexport * from './delay'\nexport * from './effect'\nexport * from './filter'\nexport * from './onLensUpdate'\nexport * from './parseAtoms'\nexport * from './sample'\nexport * from './select'\nexport * from './withReset'\n\nexport interface LensAtom<State = any> extends Atom<State> {\n  deps: Array<Atom>\n}\nexport interface LensAction<Params extends any[] = any[], Payload = any>\n  extends Action<Params, Payload> {\n  deps: Array<Atom>\n}\n\ntype Combined<Shape extends Rec<Atom>> = Plain<{\n  [K in keyof Shape]: AtomState<Shape[K]>\n}>\n\nexport const combine = <Shape extends Rec<Atom>>(\n  shape: Shape,\n  name = __count('_combine'),\n): LensAtom<Combined<Shape>> => {\n  // @ts-expect-error\n  const theAtom: LensAtom = atom((ctx) => {\n    const newState = {} as Combined<Shape>\n    for (const key in shape) newState[key] = ctx.spy(shape[key]!)\n    return newState\n  }, name)\n  theAtom.deps = Object.values(shape)\n\n  return theAtom\n}\n\n/**\n * Skip mark to stop reactive propagation and use previous state\n * (`never` helps to infer correct type)\n * @internal\n * @deprecated\n */\nexport const SKIP: never = 'REATOM_SKIP_MARK' as any as never\n\n/** Remove callable signature to prevent the atom update from outside */\nexport const readonly = <T extends Atom & { deps?: Array<Atom> }>({\n  __reatom,\n  pipe,\n  onChange,\n  // @ts-expect-error the atom could be an action\n  onCall,\n  deps,\n}: T): LensAtom<AtomState<T>> =>\n  Object.assign(\n    {\n      __reatom,\n      pipe,\n      onChange,\n    },\n    deps ? { deps } : {},\n    onCall ? { onCall } : {},\n  ) as LensAtom<AtomState<T>>\n\n/** Remove all extra properties from the atom to pick the essence */\nexport const plain = <T extends Atom>(\n  anAtom: T,\n): T extends Action<infer Params, infer Payload>\n  ? Action<Params, Payload>\n  : T extends AtomMut<infer State>\n    ? AtomMut<State>\n    : Atom<AtomState<T>> => {\n  const theAtom =\n    typeof anAtom === 'function'\n      ? // @ts-expect-error\n        anAtom.bind()\n      : {}\n  theAtom.__reatom = anAtom.__reatom\n  theAtom.pipe = anAtom.pipe\n  theAtom.onChange = anAtom.onChange\n  if ('onCall' in anAtom) theAtom.onCall = anAtom.onCall\n\n  return theAtom\n}\n\n/** Transform atom state */\nexport const mapState =\n  <T extends Atom, Res>(\n    mapper: Fn<[CtxSpy, AtomState<T>, undefined | AtomState<T>, unknown], Res>,\n    name?: string,\n  ): Fn<[T], LensAtom<Res>> =>\n  (anAtom: Atom) => {\n    const theAtom = atom(\n      (ctx, state?: any) =>\n        mapper(ctx, ctx.spy(anAtom), ctx.cause!.pubs.at(0)?.state, state),\n      mapName(anAtom, 'mapState', name),\n    ) as LensAtom<Res>\n    theAtom.deps = [anAtom]\n\n    return theAtom\n  }\n\n/** Transform action payload */\nexport const mapPayload: {\n  <Payload, T, Params extends any[] = any[]>(\n    map: Fn<[Ctx, Payload, Params], T>,\n    name?: string,\n  ): Fn<[Action<Params, Payload>], LensAction<[], T>>\n  <T extends Action>(\n    fallback: ActionPayload<T>,\n    name?: string,\n  ): Fn<[T], LensAtom<ActionPayload<T>>>\n  <T, State>(\n    fallback: State,\n    name?: string,\n  ): Fn<[Action<any[], T>], LensAtom<State | T>>\n  <Payload, State, Params extends any[] = any[]>(\n    fallback: State,\n    map: Fn<[Ctx, Payload, Params, State], State>,\n    name?: string,\n  ): Fn<[Action<Params, Payload>], LensAtom<State>>\n} =\n  (fallbackOrMapper: any, mapOrName?: any, name?: string) =>\n  (anAction: Action): any => {\n    throwReatomError(!anAction.__reatom.isAction, 'action expected')\n\n    const isAction = typeof fallbackOrMapper === 'function'\n    // isAtom\n    let fallback = fallbackOrMapper\n    // isAtom\n    let map = mapOrName ?? ((ctx: Ctx, v: any) => v)\n    if (isAction) {\n      fallback = []\n      map = fallbackOrMapper\n      name = mapOrName\n    }\n\n    const theAtom = Object.assign(\n      () => throwReatomError(1, 'derived action call'),\n      anAction.pipe(\n        mapState(\n          (ctx, depState, prevDepState, prevState = fallback) => {\n            return isAction\n              ? // @ts-expect-error\n                ((ctx.spy = undefined),\n                depState.reduce((acc: any, v) => {\n                  const payload = map(ctx, v.payload, v.params)\n                  return payload === SKIP\n                    ? acc\n                    : [...acc, { params: [v], payload }]\n                }, prevState))\n              : depState.reduce((acc, { payload, params }) => {\n                  const state = map(ctx, payload, params, acc)\n                  return state === SKIP ? acc : state\n                }, prevState)\n          },\n          mapName(anAction, 'mapPayload', name),\n        ),\n      ),\n    )\n    theAtom.__reatom.isAction = isAction\n\n    return theAtom\n  }\n\n/** Transform async action payload */\nexport const mapPayloadAwaited: {\n  <T, Payload = Awaited<T>>(\n    mapper?: Fn<[Ctx, Awaited<T>], Payload>,\n    name?: string,\n  ): Fn<[Action<any[], T>], LensAction<[], Payload>>\n  <T extends Action>(\n    fallback: Awaited<ActionPayload<T>>,\n    name?: string,\n  ): Fn<[T], LensAtom<Awaited<ActionPayload<T>>>>\n  <T, State>(\n    fallback: State,\n    name?: string,\n  ): Fn<[Action<any[], T>], LensAtom<State | Awaited<T>>>\n  <T, State>(\n    fallback: State,\n    map: Fn<[Ctx, Awaited<T>], State>,\n    name?: string,\n  ): Fn<[Action<any[], T>], LensAtom<State>>\n} =\n  (...a: [any?, any?, any?]) =>\n  (anAction: Action): any => {\n    const isAction = a.length === 0 || typeof a[0] === 'function'\n    const [fallback, map = (ctx: Ctx, v: any) => v, name] = isAction\n      ? [[], a[0], a[1]]\n      : a\n    const params = isAction ? [] : [fallback]\n    params.push(\n      (ctx: Ctx, promise: any) => {\n        if (promise instanceof Promise) {\n          __thenReatomed(ctx, promise, (v, read, actualize) =>\n            actualize!(\n              ctx,\n              ctx.cause!.proto,\n              (patchCtx: Ctx, patch: AtomCache) => {\n                patch.cause = ctx.cause.cause\n                const payload = map(ctx, v)\n                patch.state = isAction\n                  ? [...patch.state, { params: [v], payload }]\n                  : payload\n              },\n            ),\n          )\n\n          return SKIP\n        } else {\n          return map(ctx, promise)\n        }\n      },\n      mapName(anAction, 'mapPayloadAwaited', name),\n    )\n\n    // @ts-expect-error reatomAsync\n    return (anAction.onFulfill ?? anAction).pipe(\n      mapPayload(\n        // @ts-ignore\n        ...params,\n      ),\n    )\n  }\n\n/** Transform atom update */\nexport const mapInput =\n  <T extends AtomMut | Action<[any]>, Args extends [Ctx, ...any[]]>(\n    mapper: Fn<Args, Parameters<T>[1]>,\n    name?: string,\n  ): Fn<[T], Action<CtxParams<Args>, AtomState<T>>> =>\n  (anAtom): any =>\n    action(\n      (ctx, ...args) =>\n        anAtom(\n          ctx,\n          // @ts-ignore\n          mapper(ctx, ...args),\n        ),\n      mapName(anAtom, 'mapInput', name),\n    )\n\n/** Convert action to atom with optional fallback state */\n// @ts-expect-error\nexport const toAtom: {\n  <T extends Action>(\n    fallback: ReturnType<T>,\n    name?: string,\n  ): Fn<[T], LensAtom<ReturnType<T>>>\n  <T extends Action>(\n    fallback?: undefined,\n    name?: string,\n  ): Fn<[T], LensAtom<undefined | ReturnType<T>>>\n  <T extends Action, State>(\n    fallback: State,\n    name?: string,\n  ): Fn<[T], LensAtom<State | ReturnType<T>>>\n} =\n  (fallback?: any, name?: string): Fn<[Action], Atom> =>\n  (anAction) =>\n    mapPayload(\n      fallback,\n      (ctx, v: any) => v,\n      mapName(anAction, 'toAtom', name),\n    )(anAction)\n\n// https://rxjs.dev/api/operators/tap\nexport const withOnUpdate =\n  <T extends Atom>(\n    cb: T extends Action<infer Params, infer Payload>\n      ? Fn<\n          [\n            Ctx,\n            Payload,\n            AtomCache<AtomState<Action<Params, Payload>>> & { params: Params },\n          ]\n        >\n      : Fn<[Ctx, AtomState<T>, AtomCache<AtomState<T>>]>,\n  ) =>\n  (anAtom: T): T => {\n    // @ts-expect-error\n    onUpdate(anAtom, cb)\n    return anAtom\n  }\n\n/** Convert an atom to action */\nexport const toAction: {\n  <State, T>(\n    map: Fn<[ctx: Ctx, state: State], T>,\n    name?: string,\n  ): Fn<[Atom<State>], LensAction<[State], T>>\n  <T>(name?: string): Fn<[Atom<T>], LensAction<[T], T>>\n} = (map?: string | Fn, name?: string) => (anAtom: Atom) => {\n  throwReatomError(anAtom.__reatom.isAction, 'atom expected')\n\n  if (typeof map === 'string') {\n    name = map\n    map = undefined\n  }\n  map ??= (ctx: Ctx, v: any) => v\n\n  // @ts-expect-error\n  const theAction: LensAction<[T], T> = atom(\n    (ctx) => {\n      // TODO handle atom mutation in the same transaction\n      const isInit = ctx.cause.pubs.length === 0\n      const state = ctx.spy(anAtom)\n      return isInit\n        ? []\n        : [{ params: [state], payload: (map as Fn)(ctx, state) }]\n    },\n    mapName(anAtom, 'toAction', name),\n  )\n  theAction.__reatom.isAction = true\n  theAction.deps = [anAtom]\n\n  return theAction\n}\n"]}