import { Button } from "../ui/button.js";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "../ui/collapsible.js";
import { useArrayDefaultValue } from "./hooks/useArrayDefaultValue.js";
import { useMergeParentKey } from "./hooks/useMergeParentKey.js";
import { FormElement } from "./FormElement.js";
import { c } from "react/compiler-runtime";
import React, { useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { ChevronDown, ChevronRight, Plus, Trash2 } from "lucide-react";
import { useFieldArray } from "react-hook-form";
const ArrayController = (_) => {
	let v = c(45), { name: y, schema: b, ui: x, form: S, disabled: C, parentKey: w, viewOnly: T } = _, E = useMergeParentKey(w, y), [D, O] = useState(!(x?.collapsed ?? !1)), k = E ?? "", A;
	v[0] !== S.control || v[1] !== k ? (A = {
		control: S.control,
		name: k
	}, v[0] = S.control, v[1] = k, v[2] = A) : A = v[2];
	let { fields: j, append: M, remove: N } = useFieldArray(A), P = useArrayDefaultValue(b.items), F;
	v[3] === D ? F = v[4] : (F = jsx(D ? ChevronDown : ChevronRight, { className: "h-4 w-4" }), v[3] = D, v[4] = F);
	let I = x?.title ?? b.title ?? y, L;
	v[5] !== j.length || v[6] !== D ? (L = !D && /* @__PURE__ */ jsxs("span", {
		className: "ml-2 text-sm text-gray-500",
		children: [
			"(",
			j.length,
			" ",
			j.length === 1 ? "item" : "items",
			")"
		]
	}), v[5] = j.length, v[6] = D, v[7] = L) : L = v[7];
	let R;
	v[8] !== I || v[9] !== L ? (R = /* @__PURE__ */ jsxs("span", {
		className: "font-medium",
		children: [I, L]
	}), v[8] = I, v[9] = L, v[10] = R) : R = v[10];
	let z;
	v[11] !== F || v[12] !== R ? (z = /* @__PURE__ */ jsx(CollapsibleTrigger, {
		asChild: !0,
		children: /* @__PURE__ */ jsxs(Button, {
			variant: "ghost",
			size: "sm",
			className: "mb-2 flex items-center gap-2 p-2 hover:bg-gray-100",
			type: "button",
			children: [F, R]
		})
	}), v[11] = F, v[12] = R, v[13] = z) : z = v[13];
	let B;
	if (v[14] !== C || v[15] !== j || v[16] !== S || v[17] !== E || v[18] !== N || v[19] !== b.items || v[20] !== x?.items || v[21] !== T) {
		let h;
		v[23] !== C || v[24] !== S || v[25] !== E || v[26] !== N || v[27] !== b.items || v[28] !== x?.items || v[29] !== T ? (h = (h, g) => /* @__PURE__ */ jsxs("div", {
			className: `mb-2 flex items-start border-l border-gray-300 pt-2 pr-0 pl-4 ${g === 0 ? "mt-2" : "mt-5"}`,
			children: [/* @__PURE__ */ jsx("div", {
				className: "mr-1 min-w-0 flex-1",
				children: b.items && /* @__PURE__ */ jsx(FormElement, {
					name: g.toString(),
					parentKey: E,
					required: !1,
					schema: b.items,
					ui: x?.items,
					form: S,
					disabled: C,
					viewOnly: T
				})
			}), T ? "" : /* @__PURE__ */ jsx(Button, {
				type: "button",
				onClick: () => N(g),
				variant: "ghost",
				size: "sm",
				disabled: C,
				className: "mt-1 h-8 w-8 p-2",
				children: /* @__PURE__ */ jsx(Trash2, { className: "h-4 w-4" })
			})]
		}, h.id), v[23] = C, v[24] = S, v[25] = E, v[26] = N, v[27] = b.items, v[28] = x?.items, v[29] = T, v[30] = h) : h = v[30], B = j.map(h), v[14] = C, v[15] = j, v[16] = S, v[17] = E, v[18] = N, v[19] = b.items, v[20] = x?.items, v[21] = T, v[22] = B;
	} else B = v[22];
	let V;
	v[31] !== M || v[32] !== P || v[33] !== C || v[34] !== b.items?.title || v[35] !== x?.items?.title || v[36] !== T ? (V = T ? "" : /* @__PURE__ */ jsxs(Button, {
		type: "button",
		onClick: () => M(P),
		variant: "outline",
		size: "sm",
		disabled: C,
		className: "mt-4",
		children: [
			/* @__PURE__ */ jsx(Plus, { className: "mr-2 h-4 w-4" }),
			"Add ",
			x?.items?.title ?? b.items?.title ?? "Item"
		]
	}), v[31] = M, v[32] = P, v[33] = C, v[34] = b.items?.title, v[35] = x?.items?.title, v[36] = T, v[37] = V) : V = v[37];
	let H;
	v[38] !== B || v[39] !== V ? (H = /* @__PURE__ */ jsx(CollapsibleContent, { children: /* @__PURE__ */ jsxs("div", {
		className: "pl-4",
		children: [B, V]
	}) }), v[38] = B, v[39] = V, v[40] = H) : H = v[40];
	let U;
	return v[41] !== D || v[42] !== H || v[43] !== z ? (U = /* @__PURE__ */ jsxs(Collapsible, {
		open: D,
		onOpenChange: O,
		children: [z, H]
	}), v[41] = D, v[42] = H, v[43] = z, v[44] = U) : U = v[44], U;
};
export { ArrayController };
