'use strict';

const jsxRuntime = require('react/jsx-runtime');
const accordion = require('../../accordion.cjs');
const button = require('../../button.cjs');
const separator = require('../../separator.cjs');
const LucideIcons = require('lucide-react');
const reactHookForm = require('react-hook-form');
const helpers = require('../helpers.cjs');
const object = require('./object.cjs');

function getDefType(schema) {
  return schema._zod?.def?.type || "";
}
function getArrayElementType(item) {
  const def = item._zod?.def;
  const defType = getDefType(item);
  if (defType === "array") {
    return def?.element || null;
  }
  if (["default", "optional", "nullable"].includes(defType)) {
    const innerType = def?.innerType;
    if (innerType) {
      return getArrayElementType(innerType);
    }
  }
  return null;
}
function AutoFormArray({
  name,
  item,
  form,
  path = [],
  fieldConfig
}) {
  const fieldPath = path.join(".");
  const { fields, append, remove } = reactHookForm.useFieldArray({
    control: form.control,
    name: fieldPath
  });
  const title = fieldConfig?.label ?? helpers.beautifyObjectName(name);
  const itemDefType = getArrayElementType(item);
  return /* @__PURE__ */ jsxRuntime.jsxs(accordion.AccordionItem, { value: name, className: "border-none", children: [
    /* @__PURE__ */ jsxRuntime.jsx(accordion.AccordionTrigger, { children: title }),
    /* @__PURE__ */ jsxRuntime.jsxs(accordion.AccordionContent, { children: [
      fields.map((_field, index) => {
        const key = _field.id;
        return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "mt-4 flex flex-col", children: [
          /* @__PURE__ */ jsxRuntime.jsx(
            object,
            {
              schema: itemDefType,
              form,
              fieldConfig,
              path: [...path, index.toString()]
            }
          ),
          /* @__PURE__ */ jsxRuntime.jsx("div", { className: "my-4 flex justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(
            button.Button,
            {
              variant: "secondary",
              size: "icon",
              type: "button",
              className: "hover:bg-zinc-300 hover:text-black focus:ring-0 focus:ring-offset-0 focus-visible:ring-0 focus-visible:ring-offset-0 dark:bg-white dark:text-black dark:hover:bg-zinc-300 dark:hover:text-black dark:hover:ring-0 dark:hover:ring-offset-0 dark:focus-visible:ring-0 dark:focus-visible:ring-offset-0",
              onClick: () => remove(index),
              children: /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Trash, { className: "size-4 " })
            }
          ) }),
          /* @__PURE__ */ jsxRuntime.jsx(separator.Separator, {})
        ] }, `${key}`);
      }),
      /* @__PURE__ */ jsxRuntime.jsxs(
        button.Button,
        {
          type: "button",
          variant: "secondary",
          onClick: () => append({}),
          className: "mt-4 flex items-center",
          children: [
            /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Plus, { className: "mr-2", size: 16 }),
            "Add"
          ]
        }
      )
    ] })
  ] });
}

module.exports = AutoFormArray;
