{"version":3,"file":"contexts.cjs","names":["decompose"],"sources":["../../../../src/machine/functions/subcriber/contexts.ts"],"sourcesContent":["import type { Ru } from '#bemedev/globals/types';\nimport {\n  decompose,\n  type Decompose,\n  type DecomposeOptions,\n  type Recompose,\n} from '@bemedev/decompose';\n\n// #region type AssignByBey_F\nexport type AssignByBey_F = <\n  T extends Ru,\n  D extends Decompose<T>,\n  K extends Extract<keyof D, string>,\n  R extends D[K],\n>(\n  obj: T,\n  key: K,\n  value: R,\n) => T;\n// #endregion\nexport interface AssignByKey {\n  (obj: any, key: string, value: any): any;\n  low: (obj: any, key: string, value: any) => any;\n  typed: AssignByBey_F;\n}\n\nconst _assignByKey: AssignByKey['low'] = (obj, key, value) => {\n  if (obj === null || obj === undefined) return obj;\n  const out: any = obj;\n\n  const keys = key.split('.');\n\n  const check1 = keys.length === 1;\n  if (check1) {\n    out[key] = value;\n    return out;\n  }\n  out[keys[0]] = _assignByKey(\n    out[keys[0]],\n    keys.slice(1).join('.'),\n    value,\n  );\n\n  return out;\n};\n\n/**\n * Assigns a value to a path in an object.\n * @param obj The object to assign the value to\n * @param path The key to assign the value to, can be a nested key (e.g. 'a.b.c')\n * @param value The value to assign to the key\n * @returns The modified object with the value assigned to the specified key\n *\n * @see {@linkcode Decompose} for more details on object decomposition.\n */\nexport const assignByKey: AssignByKey = (obj, path, value) => {\n  return _assignByKey(obj, path, value);\n};\n\nassignByKey.low = _assignByKey;\nassignByKey.typed = _assignByKey;\n\n// #region type GetByKey_F\nexport type GetByKey_F = <\n  const T extends Ru,\n  const K extends keyof Decompose<T, { start: false }>,\n>(\n  obj: T,\n  key: Extract<K, string>,\n) => Decompose<T, { start: false }>[K];\n\nexport type GetByKeyOption_F = <const O extends DecomposeOptions>(\n  val: O,\n) => <const T extends Ru, const K extends keyof Decompose<T, O>>(\n  obj: T,\n  key: Extract<K, string>,\n) => Decompose<T, O>[K];\n// #endregion\nexport interface GetByKey {\n  (obj: any, key: string): any;\n  low: (obj: any, key: string) => any;\n  typed: GetByKey_F;\n  options: GetByKeyOption_F;\n}\n\nconst _getByKey: GetByKey['low'] = (obj, key) => {\n  const decomposed = decompose.low(obj, { start: false, object: 'both' });\n  return (decomposed as any)[key];\n};\n\n/**\n * Retrieves a value from an object by a specified key.\n * @param obj The object to retrieve the value from\n * @param key The key to retrieve the value for, can be a nested key (e.g. 'a.b.c')\n * @returns The value associated with the specified key in the object\n *\n * @see {@linkcode Decompose} for more details on object decomposition.\n */\nexport const getByKey: GetByKey = (obj, key) => _getByKey(obj, key);\n\ngetByKey.low = _getByKey;\ngetByKey.typed = _getByKey;\ngetByKey.options = options => (obj, key) => {\n  const decomposed = decompose.low(obj, options);\n  return decomposed[key];\n};\n\n// #region type MergeByKey_F\nexport type MergeByKey_F = (\n  obj: Ru,\n) => <D extends Decompose<typeof obj>, K extends keyof D>(\n  key: Extract<K, string>,\n  value: D[K],\n) => Recompose<Pick<D, K>>;\n// #endregion\nexport interface MergeByKey {\n  (obj: any): (key: string, value: any) => any;\n  low: (obj: any) => (key: string, value: any) => any;\n  typed: MergeByKey_F;\n}\n\nconst _mergeByKey: MergeByKey['low'] = () => {\n  return (key, value) => {\n    const out: any = {};\n    const keys = key.toLocaleString().split('.');\n\n    const check1 = keys.length === 1;\n    if (check1) {\n      out[key] = value;\n    } else {\n      out[keys[0]] = Object.assign(\n        {},\n        _mergeByKey(out[keys[0]])(keys.slice(1).join('.'), value),\n      );\n    }\n\n    return out;\n  };\n};\n\n/**\n * Creates a function that merges a value into an object at a specified key path.\n * @param obj The object to merge the value into\n * @returns A function that takes a key and a value, and merges the value into the object at the specified key path\n *\n * @see {@linkcode Decompose} for more details on object decomposition.\n */\nexport const mergeByKey: MergeByKey = obj => _mergeByKey(obj);\n\nmergeByKey.low = _mergeByKey;\nmergeByKey.typed = _mergeByKey;\n"],"mappings":";;;;AA0BA,MAAM,gBAAoC,KAAK,KAAK,UAAU;AAC5D,KAAI,QAAQ,QAAQ,QAAQ,KAAA,EAAW,QAAO;C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