{"version":3,"file":"use-list-state.mjs","names":[],"sources":["../../src/use-list-state/use-list-state.ts"],"sourcesContent":["import { useCallback, useMemo, useState } from 'react';\n\nexport interface UseListStateHandlers<T> {\n  setState: React.Dispatch<React.SetStateAction<T[]>>;\n  append: (...items: T[]) => void;\n  prepend: (...items: T[]) => void;\n  insert: (index: number, ...items: T[]) => void;\n  pop: () => void;\n  shift: () => void;\n  apply: (fn: (item: T, index?: number) => T) => void;\n  applyWhere: (\n    condition: (item: T, index: number) => boolean,\n    fn: (item: T, index?: number) => T\n  ) => void;\n  remove: (...indices: number[]) => void;\n  reorder: ({ from, to }: { from: number; to: number }) => void;\n  swap: ({ from, to }: { from: number; to: number }) => void;\n  setItem: (index: number, item: T) => void;\n  setItemProp: <K extends keyof T, U extends T[K]>(index: number, prop: K, value: U) => void;\n  filter: (fn: (item: T, i: number) => boolean) => void;\n}\n\nexport type UseListStateReturnValue<T> = [T[], UseListStateHandlers<T>];\n\nexport function useListState<T>(initialValue: T[] | (() => T[]) = []): UseListStateReturnValue<T> {\n  const [state, setState] = useState(initialValue);\n\n  const append = useCallback((...items: T[]) => setState((current) => [...current, ...items]), []);\n\n  const prepend = useCallback((...items: T[]) => setState((current) => [...items, ...current]), []);\n\n  const insert = useCallback(\n    (index: number, ...items: T[]) =>\n      setState((current) => [...current.slice(0, index), ...items, ...current.slice(index)]),\n    []\n  );\n\n  const apply = useCallback(\n    (fn: (item: T, index?: number) => T) =>\n      setState((current) => current.map((item, index) => fn(item, index))),\n    []\n  );\n\n  const remove = useCallback(\n    (...indices: number[]) =>\n      setState((current) => current.filter((_, index) => !indices.includes(index))),\n    []\n  );\n\n  const pop = useCallback(\n    () =>\n      setState((current) => {\n        const cloned = [...current];\n        cloned.pop();\n        return cloned;\n      }),\n    []\n  );\n\n  const shift = useCallback(\n    () =>\n      setState((current) => {\n        const cloned = [...current];\n        cloned.shift();\n        return cloned;\n      }),\n    []\n  );\n\n  const reorder = useCallback(\n    ({ from, to }: { from: number; to: number }) =>\n      setState((current) => {\n        const cloned = [...current];\n        const item = current[from];\n\n        cloned.splice(from, 1);\n        cloned.splice(to, 0, item);\n\n        return cloned;\n      }),\n    []\n  );\n\n  const swap = useCallback(\n    ({ from, to }: { from: number; to: number }) =>\n      setState((current) => {\n        const cloned = [...current];\n        const fromItem = cloned[from];\n        const toItem = cloned[to];\n\n        cloned.splice(to, 1, fromItem);\n        cloned.splice(from, 1, toItem);\n\n        return cloned;\n      }),\n    []\n  );\n\n  const setItem = useCallback(\n    (index: number, item: T) =>\n      setState((current) => {\n        const cloned = [...current];\n        cloned[index] = item;\n        return cloned;\n      }),\n    []\n  );\n\n  const setItemProp = useCallback(\n    <K extends keyof T, U extends T[K]>(index: number, prop: K, value: U) =>\n      setState((current) => {\n        const cloned = [...current];\n        cloned[index] = { ...cloned[index], [prop]: value };\n        return cloned;\n      }),\n    []\n  );\n\n  const applyWhere = useCallback(\n    (condition: (item: T, index: number) => boolean, fn: (item: T, index?: number) => T) =>\n      setState((current) =>\n        current.map((item, index) => (condition(item, index) ? fn(item, index) : item))\n      ),\n    []\n  );\n\n  const filter = useCallback((fn: (item: T, i: number) => boolean) => {\n    setState((current) => current.filter(fn));\n  }, []);\n\n  const handlers = useMemo(\n    () => ({\n      setState,\n      append,\n      prepend,\n      insert,\n      pop,\n      shift,\n      apply,\n      applyWhere,\n      remove,\n      reorder,\n      swap,\n      setItem,\n      setItemProp,\n      filter,\n    }),\n    []\n  );\n\n  return [state, handlers];\n}\n\nexport namespace useListState {\n  export type ReturnValue<T> = UseListStateReturnValue<T>;\n  export type Handlers<T> = UseListStateHandlers<T>;\n}\n"],"mappings":";;;AAwBA,SAAgB,aAAgB,eAAkC,CAAC,GAA+B;CAChG,MAAM,CAAC,OAAO,YAAY,SAAS,YAAY;CAE/C,MAAM,SAAS,aAAa,GAAG,UAAe,UAAU,YAAY,CAAC,GAAG,SAAS,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;CAE/F,MAAM,UAAU,aAAa,GAA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