{"version":3,"file":"atom-collection.mjs","names":[],"sources":["../../../../@mongez/atom/src/atom-collection.ts"],"sourcesContent":["import { createAtom } from './atom';\nimport type { Atom, AtomActions, AtomOptions } from './types';\n\nexport type IndexOrCallback<Value> =\n  | number\n  | ((value: Value, index: number, list: Value[]) => boolean);\n\nexport interface AtomCollectionActions<Value> extends AtomActions<Value[]> {\n  /**\n   * Add items to the end of the array\n   */\n  push(this: Atom<Value[]>, ...items: Value[]): void;\n  /**\n   * Add items to the beginning of the array\n   */\n  unshift(this: Atom<Value[]>, ...items: Value[]): void;\n  /**\n   * Remove the last item from the array\n   */\n  pop(this: Atom<Value[]>): void;\n  /**\n   * Remove the first item from the array\n   */\n  shift(this: Atom<Value[]>): void;\n  /**\n   * Remove item from array either by index or callback\n   */\n  remove(this: Atom<Value[]>, indexOrCallback: IndexOrCallback<Value>): void;\n  /**\n   * Remove item from array by value\n   */\n  removeItem(this: Atom<Value[]>, item: Value): void;\n  /**\n   * Remove all occurrences of an item from the array\n   */\n  removeAll(this: Atom<Value[]>, item: Value): void;\n  /**\n   * Get item from array either by index or callback\n   */\n  get(this: Atom<Value[]>, indexOrCallback: IndexOrCallback<Value>): Value | undefined;\n  /**\n   * Find index of item in array by callback\n   */\n  index(\n    this: Atom<Value[]>,\n    callback: (item: Value, index: number, array: Value[]) => boolean,\n  ): number;\n  /**\n   * Map array items\n   * This will update the array with the new mapped array and trigger update\n   */\n  map(\n    this: Atom<Value[]>,\n    callback: (item: Value, index: number, array: Value[]) => Value,\n  ): Value[];\n  /**\n   * Loop through array items\n   */\n  forEach(\n    this: Atom<Value[]>,\n    callback: (item: Value, index: number, array: Value[]) => void,\n  ): void;\n  /**\n   * Replace item in array by index\n   */\n  replace(this: Atom<Value[]>, index: number, item: Value): void;\n  /**\n   * Get array length\n   */\n  length: number; // As a property\n}\n\nexport type CollectionOptions<Value> = Omit<\n  AtomOptions<Value[], AtomCollectionActions<Value>>,\n  'default'\n> & {\n  default?: Value[];\n};\n\n/**\n * Create an atom collection\n */\nexport function atomCollection<Value = any>(\n  options: CollectionOptions<Value>,\n): Atom<Value[], AtomCollectionActions<Value>> {\n  return createAtom<Value[], AtomCollectionActions<Value>>({\n    key: options.key,\n    default: options.default ?? [],\n    actions: {\n      ...options.actions,\n      push(...items: Value[]) {\n        this.update([...this.currentValue, ...items]);\n      },\n      pop() {\n        this.update(this.currentValue.slice(0, -1));\n      },\n      shift() {\n        this.update(this.currentValue.slice(1));\n      },\n      unshift(...items: Value[]) {\n        this.update([...items, ...this.currentValue]);\n      },\n      remove(indexOrCallback: IndexOrCallback<Value>) {\n        const index =\n          typeof indexOrCallback === 'function'\n            ? this.value.findIndex(indexOrCallback)\n            : indexOrCallback;\n\n        if (index === -1) return;\n\n        this.update(this.value.filter((_, i) => i !== index));\n      },\n      removeItem(item: Value) {\n        const index = this.value.indexOf(item);\n\n        if (index === -1) return;\n\n        // using splice\n        this.value.splice(index, 1);\n\n        this.update([...this.value]);\n      },\n      removeAll(item: Value) {\n        this.update(this.value.filter((value) => value !== item));\n      },\n      get(indexOrCallback: IndexOrCallback<Value>) {\n        const index: number =\n          typeof indexOrCallback === 'function'\n            ? this.value.findIndex(indexOrCallback)\n            : indexOrCallback;\n\n        return this.value[index];\n      },\n      index(callback: (item: Value, index: number, array: Value[]) => boolean) {\n        return this.value.findIndex(callback);\n      },\n      map(callback: (item: Value, index: number, array: Value[]) => Value) {\n        const value = this.value.map(callback);\n\n        this.update(value);\n\n        return value;\n      },\n      forEach(callback: (item: Value, index: number, array: Value[]) => void) {\n        this.value.forEach(callback);\n      },\n      get length() {\n        return this.value?.length;\n      },\n      replace(index: number, item: Value) {\n        this.update(\n          this.value.map((value, i) => {\n            if (i === index) {\n              return item;\n            }\n\n            return value;\n          }),\n        );\n      },\n    },\n  });\n}\n"],"mappings":";;;;;;AAkFA,SAAgB,eACd,SAC6C;CAC7C,OAAO,WAAkD;EACvD,KAAK,QAAQ;EACb,SAAS,QAAQ,WAAW,CAAC;EAC7B,SAA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