"use client";
const require_use_event = require("../../utils/use-event.cjs");
const require_list_collection = require("./list-collection.cjs");
let react = require("react");
//#region src/components/collection/use-list-collection.ts
function useListCollection(props) {
	const { initialItems = [], filter, limit, ...collectionOptions } = props;
	const [items, setItemsImpl] = (0, react.useState)(initialItems);
	const [filterText, setFilterText] = (0, react.useState)("");
	const setItems = require_use_event.useEvent((items) => {
		setItemsImpl(items);
		setFilterText("");
	});
	const collectionOptionsRef = (0, react.useRef)(collectionOptions);
	collectionOptionsRef.current = collectionOptions;
	const create = (0, react.useCallback)((items) => {
		return require_list_collection.createListCollection({
			...collectionOptionsRef.current,
			items
		});
	}, []);
	return {
		collection: (0, react.useMemo)(() => {
			let activeItems = items;
			if (filterText && filter) activeItems = create(items).filter((itemString, _index, item) => filter(itemString, filterText, item)).items;
			const limitedItems = limit == null ? activeItems : activeItems.slice(0, limit);
			return require_list_collection.createListCollection({
				...collectionOptionsRef.current,
				items: limitedItems
			});
		}, [
			items,
			filterText,
			filter,
			limit,
			create
		]),
		filter: require_use_event.useEvent((inputValue) => {
			setFilterText(inputValue || "");
		}),
		set: require_use_event.useEvent((newItems) => {
			setItems(newItems);
		}),
		reset: require_use_event.useEvent(() => {
			setItems(initialItems);
		}),
		clear: require_use_event.useEvent(() => {
			setItems([]);
		}),
		insert: require_use_event.useEvent((index, ...itemsToInsert) => {
			const newItems = create(items).insert(index, ...itemsToInsert).items;
			setItems(newItems);
		}),
		insertBefore: require_use_event.useEvent((value, ...itemsToInsert) => {
			const newItems = create(items).insertBefore(value, ...itemsToInsert).items;
			setItems(newItems);
		}),
		insertAfter: require_use_event.useEvent((value, ...itemsToInsert) => {
			const newItems = create(items).insertAfter(value, ...itemsToInsert).items;
			setItems(newItems);
		}),
		remove: require_use_event.useEvent((...itemOrValues) => {
			const newItems = create(items).remove(...itemOrValues).items;
			setItems(newItems);
		}),
		move: require_use_event.useEvent((value, to) => {
			const newItems = create(items).move(value, to).items;
			setItems(newItems);
		}),
		moveBefore: require_use_event.useEvent((value, ...values) => {
			const newItems = create(items).moveBefore(value, ...values).items;
			setItems(newItems);
		}),
		moveAfter: require_use_event.useEvent((value, ...values) => {
			const newItems = create(items).moveAfter(value, ...values).items;
			setItems(newItems);
		}),
		reorder: require_use_event.useEvent((from, to) => {
			const newItems = create(items).reorder(from, to).items;
			setItems(newItems);
		}),
		append: require_use_event.useEvent((...itemsToAppend) => {
			const newItems = create(items).append(...itemsToAppend).items;
			setItems(newItems);
		}),
		upsert: require_use_event.useEvent((value, item, mode = "append") => {
			const newItems = create(items).upsert(value, item, mode).items;
			setItems(newItems);
		}),
		prepend: require_use_event.useEvent((...itemsToPrepend) => {
			const newItems = create(items).prepend(...itemsToPrepend).items;
			setItems(newItems);
		}),
		update: require_use_event.useEvent((value, item) => {
			const newItems = create(items).update(value, item).items;
			setItems(newItems);
		})
	};
}
//#endregion
exports.useListCollection = useListCollection;
