{"version":3,"file":"use-queue.cjs","names":[],"sources":["../../src/hooks/use-queue.ts"],"sourcesContent":["import { useCallback, useMemo, useState } from \"react\";\n\nexport interface UseQueueOptions<T> {\n    initialValues?: T[];\n    limit?: number;\n}\n\nexport interface UseQueueResult<T> {\n    /** Items currently held within the queue (up to `limit`). */\n    queue: T[];\n    /** Append items; overflow beyond `limit` is held back, not dropped. */\n    add: (...items: T[]) => void;\n    /** Replace the held queue via a mapper over the current held items. */\n    update: (fn: (state: T[]) => T[]) => void;\n    /** Drop all currently held (visible) items, keeping any overflow. */\n    cleanQueue: () => void;\n    /** Number of visible items in the queue. */\n    size: number;\n}\n\n/**\n * FIFO queue with an optional `limit`. When more items are added than `limit`\n * allows, the surplus is kept in an internal overflow buffer and surfaces into\n * `queue` as room frees up (e.g. after `cleanQueue`). Loosely mirrors Mantine's\n * `useQueue` shape.\n *\n * @typeParam T - element type.\n * @param options - `initialValues` and `limit` (default limit `Infinity`).\n * @returns `{ queue, add, update, cleanQueue, size }`.\n */\nexport function useQueue<T>(options: UseQueueOptions<T> = {}): UseQueueResult<T> {\n    const { initialValues = [], limit = Infinity } = options;\n\n    const [state, setState] = useState<{ queue: T[]; overflow: T[] }>(() => ({\n        queue: initialValues.slice(0, limit),\n        overflow: initialValues.slice(limit),\n    }));\n\n    const add = useCallback(\n        (...items: T[]): void => {\n            setState((current) => {\n                const all = [...current.queue, ...current.overflow, ...items];\n                return {\n                    queue: all.slice(0, limit),\n                    overflow: all.slice(limit),\n                };\n            });\n        },\n        [limit],\n    );\n\n    const update = useCallback(\n        (fn: (queueState: T[]) => T[]): void => {\n            setState((current) => {\n                const all = [...fn(current.queue), ...current.overflow];\n                return {\n                    queue: all.slice(0, limit),\n                    overflow: all.slice(limit),\n                };\n            });\n        },\n        [limit],\n    );\n\n    const cleanQueue = useCallback((): void => {\n        setState((current) => ({\n            queue: current.overflow.slice(0, limit),\n            overflow: current.overflow.slice(limit),\n        }));\n    }, [limit]);\n\n    return useMemo<UseQueueResult<T>>(\n        () => ({\n            queue: state.queue,\n            add,\n            update,\n            cleanQueue,\n            size: state.queue.length,\n        }),\n        [state.queue, add, update, cleanQueue],\n    );\n}\n"],"mappings":"uBA8BA,SAAgB,EAAY,EAA8B,CAAC,EAAsB,CAC7E,GAAM,CAAE,gBAAgB,CAAC,EAAG,QAAQ,KAAa,EAE3C,CAAC,EAAO,IAAA,EAAY,EAAA,SAAA,MAA+C,CACrE,MAAO,EAAc,MAAM,EAAG,CAAK,EACnC,SAAU,EAAc,MAAM,CAAK,CACvC,EAAE,EAEI,GAAA,EAAM,EAAA,YAAA,EACP,GAAG,IAAqB,CACrB,EAAU,GAAY,CAClB,IAAM,EAAM,CAAC,GAAG,EAAQ,MAAO,GAAG,EAAQ,SAAU,GAAG,CAAK,EAC5D,MAAO,CACH,MAAO,EAAI,MAAM,EAAG,CAAK,EACzB,SAAU,EAAI,MAAM,CAAK,CAC7B,CACJ,CAAC,CACL,EACA,CAAC,CAAK,CACV,EAEM,GAAA,EAAS,EAAA,YAAA,CACV,GAAuC,CACpC,EAAU,GAAY,CAClB,IAAM,EAAM,CAAC,GAAG,EAAG,EAAQ,KAAK,EAAG,GAAG,EAAQ,QAAQ,EACtD,MAAO,CACH,MAAO,EAAI,MAAM,EAAG,CAAK,EACzB,SAAU,EAAI,MAAM,CAAK,CAC7B,CACJ,CAAC,CACL,EACA,CAAC,CAAK,CACV,EAEM,GAAA,EAAa,EAAA,YAAA,KAAwB,CACvC,EAAU,IAAa,CACnB,MAAO,EAAQ,SAAS,MAAM,EAAG,CAAK,EACtC,SAAU,EAAQ,SAAS,MAAM,CAAK,CAC1C,EAAE,CACN,EAAG,CAAC,CAAK,CAAC,EAEV,OAAA,EAAO,EAAA,QAAA,MACI,CACH,MAAO,EAAM,MACb,MACA,SACA,aACA,KAAM,EAAM,MAAM,MACtB,GACA,CAAC,EAAM,MAAO,EAAK,EAAQ,CAAU,CACzC,CACJ"}