import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import React, { forwardRef, useImperativeHandle } from "react";
import { getUniqueId } from "@pisell/utils";
import classNames from "classnames";
//#region src/components/listComponent/index.tsx
const ListComponent = ({ value = [], onChange = () => {}, renderItem = () => null, renderAddButton = () => null, createItem = (item) => item, maxLength = 999, rowKey = "_id", className, style, wrapClassName }, ref) => {
	const handleAddItem = (item) => {
		if (value.length < maxLength) onChange([...value, createItem(_objectSpread2({ [rowKey]: getUniqueId() }, item))]);
	};
	const handleOnChangeItem = (item, newValue) => {
		onChange(value.map((d) => {
			if (d[rowKey] === item[rowKey]) return _objectSpread2(_objectSpread2({}, d), newValue);
			return d;
		}));
	};
	const handleDeleteItem = (current) => {
		onChange(value.filter((item) => item[rowKey] !== current[rowKey]));
	};
	useImperativeHandle(ref, () => {
		return {
			add: handleAddItem,
			delete: handleDeleteItem
		};
	});
	return /* @__PURE__ */ React.createElement("div", { className: classNames("list-component-wrap", wrapClassName) }, /* @__PURE__ */ React.createElement("ul", {
		className: classNames("list-component-ul", className),
		style
	}, value.map((item, index) => renderItem(item, index, handleDeleteItem, handleOnChangeItem))), value.length < maxLength && renderAddButton(handleAddItem));
};
var listComponent_default = forwardRef(ListComponent);
//#endregion
export { listComponent_default as default };
