import { Button as e } from "../ui/button.js";
import { Card as t, CardContent as n } from "../ui/card.js";
import { Collapsible as r, CollapsibleContent as i, CollapsibleTrigger as a } from "../ui/collapsible.js";
import { useArrayDefaultValue as o } from "./hooks/useArrayDefaultValue.js";
import { useMergeParentKey as s } from "./hooks/useMergeParentKey.js";
import { FormElement as c } from "./FormElement.js";
import { useState as l } from "react";
import { jsx as u, jsxs as d } from "react/jsx-runtime";
import { ChevronDown as f, ChevronRight as p, Plus as m, Trash2 as h } from "lucide-react";
import { useFieldArray as g } from "react-hook-form";
//#region src/components/dynamic-form/ArrayController.tsx
var _ = ({ name: _, schema: v, ui: y, form: b, disabled: x, parentKey: S, viewOnly: C }) => {
	let w = s(S, _), T = y?.collapsed ?? !1, E = y?.fixed ?? !1, [D, O] = l(!T), { fields: k, append: A, remove: j } = g({
		control: b.control,
		name: w ?? ""
	}), M = o(v.items);
	return /* @__PURE__ */ d(r, {
		open: D,
		onOpenChange: O,
		children: [/* @__PURE__ */ u(a, {
			asChild: !0,
			children: /* @__PURE__ */ d(e, {
				variant: "ghost",
				size: "sm",
				className: "mb-2 flex items-center gap-2 p-2 hover:bg-gray-100",
				type: "button",
				children: [u(D ? f : p, { className: "h-4 w-4" }), /* @__PURE__ */ d("span", {
					className: "font-medium",
					children: [y?.title ?? v.title ?? _, !D && /* @__PURE__ */ d("span", {
						className: "ml-2 text-sm text-gray-500",
						children: [
							"(",
							k.length,
							" ",
							k.length === 1 ? "item" : "items",
							")"
						]
					})]
				})]
			})
		}), /* @__PURE__ */ u(i, { children: /* @__PURE__ */ d("div", {
			className: "space-y-2",
			children: [k.map((r, i) => /* @__PURE__ */ d(t, {
				className: "gap-0 rounded-lg py-0 shadow-none",
				children: [!C && !E && /* @__PURE__ */ u("div", {
					className: "flex justify-end px-4 pt-2 pb-0",
					children: /* @__PURE__ */ u(e, {
						type: "button",
						onClick: () => j(i),
						variant: "ghost",
						size: "sm",
						disabled: x,
						className: "h-7 w-7 p-1.5",
						children: /* @__PURE__ */ u(h, { className: "h-3.5 w-3.5" })
					})
				}), /* @__PURE__ */ u(n, {
					className: `px-4 pb-2 ${!C && !E ? "pt-0" : "pt-3"}`,
					children: v.items && /* @__PURE__ */ u(c, {
						name: i.toString(),
						parentKey: w,
						required: !1,
						schema: v.items,
						ui: y?.items,
						form: b,
						disabled: x,
						viewOnly: C
					})
				})]
			}, r.id)), !C && !E ? /* @__PURE__ */ d(e, {
				type: "button",
				onClick: () => A(M),
				variant: "outline",
				size: "sm",
				disabled: x,
				className: "mt-2",
				children: [
					/* @__PURE__ */ u(m, { className: "mr-2 h-4 w-4" }),
					"Add ",
					y?.items?.title ?? v.items?.title ?? "Item"
				]
			}) : ""]
		}) })]
	});
};
//#endregion
export { _ as ArrayController };
