{"version":3,"sources":["../../src/use-dynamic-list/index.ts"],"sourcesContent":["import { useRef } from 'react'\nimport { useCreation } from '../use-creation'\nimport { useSafeState } from '../use-safe-state'\nimport { useStableFn } from '../use-stable-fn'\nimport { isDev, isFunction } from '../utils/basic'\n\nimport type { SetStateAction } from 'react'\nimport type { ReactSetState } from '../use-safe-state'\n\nexport interface UseDynamicListReturnsActions<T> {\n  /**\n   * insert an item at the specified index\n   */\n  insert: (index: number, item: T) => void\n  /**\n   * merge items at the specified index\n   */\n  merge: (index: number, items: T[]) => void\n  /**\n   * replace an item at the specified index\n   */\n  replace: (index: number, item: T) => void\n  /**\n   * remove an item at the specified index\n   */\n  remove: (index: number) => void\n  /**\n   * get the key of the item at the specified index\n   */\n  getKey: (index: number) => number\n  /**\n   * get the index of the item with the specified key\n   */\n  getIndex: (key: number) => number\n  /**\n   * move an item from the old index to the new index\n   */\n  move: (oldIndex: number, newIndex: number) => void\n  /**\n   * push an item to the end of the list\n   */\n  push: (item: T) => void\n  /**\n   * pop an item from the end of the list\n   */\n  pop(): void\n  /**\n   * unshift an item to the start of the list\n   */\n  unshift: (item: T) => void\n  /**\n   * shift an item from the start of the list\n   */\n  shift(): void\n  /**\n   * sort the list in place\n   */\n  sort: (compare?: (pre: T, next: T) => number) => void\n  /**\n   * reset the list\n   */\n  reset: (newList: T[]) => void\n  /**\n   * set the list\n   */\n  setList: ReactSetState<T[]>\n}\n\nexport type UseDynamicListReturns<T> = readonly [\n  /**\n   * list of items\n   */\n  list: T[],\n  UseDynamicListReturnsActions<T>,\n]\n\n/**\n * A React Hook that helps to manage dynamic list of items.\n */\nexport function useDynamicList<T>(initialList: T[] = []): UseDynamicListReturns<T> {\n  const listIdxRef = useRef(0)\n  const keysRef = useRef<number[]>([])\n\n  function updateInternalKeyAt(index: number) {\n    keysRef.current.splice(index, 0, listIdxRef.current++)\n  }\n\n  const [list, nativeSetList] = useSafeState(() => {\n    initialList.forEach((_, index) => updateInternalKeyAt(index))\n    return initialList\n  })\n\n  const reset = useStableFn((newList: T[]) => {\n    keysRef.current.length = 0\n    nativeSetList(() => {\n      newList.forEach((_, index) => updateInternalKeyAt(index))\n      return newList\n    })\n  })\n\n  const insert = useStableFn((index: number, item: T) => {\n    nativeSetList((list) => {\n      const temp = [...list]\n      temp.splice(index, 0, item)\n      updateInternalKeyAt(index)\n      return temp\n    })\n  })\n\n  const getIndex = useStableFn((key: number) => keysRef.current.findIndex((ele) => ele === key))\n  const getKey = useStableFn((index: number) => keysRef.current[index])\n\n  const merge = useStableFn((index: number, items: T[]) => {\n    nativeSetList((list) => {\n      const temp = [...list]\n      items.forEach((_, i) => updateInternalKeyAt(index + i))\n      temp.splice(index, 0, ...items)\n      return temp\n    })\n  })\n\n  const replace = useStableFn((index: number, item: T) => {\n    nativeSetList((list) => {\n      const temp = [...list]\n      temp[index] = item\n      return temp\n    })\n  })\n\n  const remove = useStableFn((index: number) => {\n    nativeSetList((list) => {\n      const temp = [...list]\n      temp.splice(index, 1)\n      try {\n        keysRef.current.splice(index, 1)\n      } catch (e) {\n        if (isDev) console.error(e)\n      }\n      return temp\n    })\n  })\n\n  const move = useStableFn((preIndex: number, nextIndex: number) => {\n    if (preIndex === nextIndex) return\n    nativeSetList((list) => {\n      const newList = [...list]\n      const temp = newList.filter((_, index: number) => index !== preIndex)\n      temp.splice(nextIndex, 0, newList[preIndex])\n      try {\n        const keyTemp = keysRef.current.filter((_, index: number) => index !== preIndex)\n        keyTemp.splice(nextIndex, 0, keysRef.current[preIndex])\n        keysRef.current = keyTemp\n      } catch (e) {\n        if (isDev) console.error(e)\n      }\n\n      return temp\n    })\n  })\n\n  const push = useStableFn((item: T) => {\n    nativeSetList((list) => {\n      updateInternalKeyAt(list.length)\n      return list.concat([item])\n    })\n  })\n\n  const pop = useStableFn(() => {\n    try {\n      keysRef.current = keysRef.current.slice(0, keysRef.current.length - 1)\n    } catch (e) {\n      if (isDev) console.error(e)\n    }\n\n    nativeSetList((list) => list.slice(0, list.length - 1))\n  })\n\n  const unshift = useStableFn((item: T) => {\n    nativeSetList((list) => {\n      updateInternalKeyAt(0)\n      return [item].concat(list)\n    })\n  })\n\n  const shift = useStableFn(() => {\n    try {\n      keysRef.current = keysRef.current.slice(1, keysRef.current.length)\n    } catch (e) {\n      if (isDev) console.error(e)\n    }\n    nativeSetList((list) => list.slice(1, list.length))\n  })\n\n  const sort = useStableFn((compare?: (pre: T, next: T) => number) => {\n    nativeSetList((list) => {\n      const temp = [...list]\n      const result = temp.sort(compare)\n      result.forEach((_, index) => updateInternalKeyAt(index))\n      return result\n    })\n  })\n\n  const setList = useStableFn((action: SetStateAction<T[]>) => {\n    nativeSetList((preList) => {\n      const newList = isFunction(action) ? action(preList) : action\n      newList.forEach((_, index) => updateInternalKeyAt(index))\n      return newList\n    })\n  })\n\n  const actions = useCreation(() => ({\n    insert,\n    merge,\n    replace,\n    remove,\n    getKey,\n    getIndex,\n    move,\n    push,\n    pop,\n    unshift,\n    shift,\n    sort,\n    reset,\n    setList,\n  }))\n\n  return [list, actions] as const\n}\n"],"names":["useDynamicList","initialList","listIdxRef","useRef","keysRef","updateInternalKeyAt","index","current","splice","useSafeState","forEach","_","list","nativeSetList","reset","useStableFn","newList","length","insert","item","temp","getIndex","key","findIndex","ele","getKey","merge","items","i","replace","remove","e","isDev","console","error","move","preIndex","nextIndex","filter","keyTemp","push","concat","pop","slice","unshift","shift","sort","compare","result","setList","action","preList","isFunction","actions","useCreation"],"mappings":";;;;+BA+EgBA;;;eAAAA;;;qBA/EO;2BACK;4BACC;2BACD;qBACM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2E3B,SAASA;QAAkBC,cAAAA,iEAAmB,EAAE;IACrD,IAAMC,aAAaC,IAAAA,aAAM,EAAC;IAC1B,IAAMC,UAAUD,IAAAA,aAAM,EAAW,EAAE;IAEnC,SAASE,oBAAoBC,KAAa;QACxCF,QAAQG,OAAO,CAACC,MAAM,CAACF,OAAO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