'use strict';

const jsxRuntime = require('react/jsx-runtime');
const accordion = require('../../accordion.cjs');
const form = require('../../form.cjs');
const reactHookForm = require('react-hook-form');
const z = require('zod');
const config = require('../config.cjs');
const helpers = require('../helpers.cjs');
const array = require('./array.cjs');
const dependencies = require('../dependencies.cjs');

function _interopNamespaceCompat(e) {
  if (e && typeof e === 'object' && 'default' in e) return e;
  const n = Object.create(null);
  if (e) {
    for (const k in e) {
      n[k] = e[k];
    }
  }
  n.default = e;
  return n;
}

const z__namespace = /*#__PURE__*/_interopNamespaceCompat(z);

function DefaultParent({ children }) {
  return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children });
}
function AutoFormObject({
  schema,
  form: form$1,
  fieldConfig,
  path = [],
  dependencies: dependencies$1 = []
}) {
  const { watch } = reactHookForm.useFormContext();
  if (!schema) {
    return null;
  }
  const { shape } = helpers.getBaseSchema(schema) || {};
  if (!shape) {
    return null;
  }
  const handleIfZodNumber = (item) => {
    const def = item._zod?.def;
    const defType = def?.type;
    const innerDefType = def?.innerType?._zod?.def?.type;
    const isZodNumber = defType === "number" || defType === "int" || defType === "float";
    const isInnerZodNumber = innerDefType === "number" || innerDefType === "int" || innerDefType === "float";
    if (isZodNumber && def) {
      def.coerce = true;
    } else if (isInnerZodNumber && def?.innerType?._zod?.def) {
      def.innerType._zod.def.coerce = true;
    }
    return item;
  };
  const sortedFieldKeys = helpers.sortFieldsByOrder(fieldConfig, Object.keys(shape));
  return /* @__PURE__ */ jsxRuntime.jsx(accordion.Accordion, { type: "multiple", className: "space-y-5 border-none", children: sortedFieldKeys.map((name) => {
    let item = shape[name];
    item = handleIfZodNumber(item);
    const zodBaseType = helpers.getBaseType(item);
    const itemName = helpers.beautifyObjectName(name);
    const key = [...path, name].join(".");
    const {
      isHidden,
      isDisabled,
      isRequired: isRequiredByDependency,
      overrideOptions
    } = dependencies(dependencies$1, name, watch);
    if (isHidden) {
      return null;
    }
    if (zodBaseType === "ZodObject") {
      const objectFieldConfig = fieldConfig?.[name] ?? {};
      if (typeof objectFieldConfig.fieldType === "function") {
        const zodInputProps2 = helpers.zodToHtmlInputProps(item);
        let isRequired2 = isRequiredByDependency || zodInputProps2.required || false;
        if (objectFieldConfig.inputProps?.required !== void 0) {
          isRequired2 = objectFieldConfig.inputProps.required;
        }
        const CustomComponent = objectFieldConfig.fieldType;
        const ParentElement = objectFieldConfig.renderParent ?? DefaultParent;
        return /* @__PURE__ */ jsxRuntime.jsx(
          form.FormField,
          {
            control: form$1.control,
            name: key,
            render: ({ field }) => {
              const fieldProps = {
                ...zodInputProps2,
                ...field,
                ...objectFieldConfig.inputProps,
                disabled: objectFieldConfig.inputProps?.disabled || isDisabled,
                ref: void 0,
                value: field.value
              };
              return /* @__PURE__ */ jsxRuntime.jsx(ParentElement, { children: /* @__PURE__ */ jsxRuntime.jsx(
                CustomComponent,
                {
                  zodInputProps: zodInputProps2,
                  field,
                  fieldConfigItem: objectFieldConfig,
                  label: objectFieldConfig.label || itemName,
                  isRequired: isRequired2,
                  zodItem: item,
                  fieldProps
                }
              ) }, `${key}.parent`);
            }
          },
          key
        );
      }
      return /* @__PURE__ */ jsxRuntime.jsxs(accordion.AccordionItem, { value: name, className: "border-none", children: [
        /* @__PURE__ */ jsxRuntime.jsx(accordion.AccordionTrigger, { children: itemName }),
        /* @__PURE__ */ jsxRuntime.jsx(accordion.AccordionContent, { className: "p-2", children: /* @__PURE__ */ jsxRuntime.jsx(
          AutoFormObject,
          {
            schema: item,
            form: form$1,
            fieldConfig: fieldConfig?.[name] ?? {},
            path: [...path, name]
          }
        ) })
      ] }, key);
    }
    if (zodBaseType === "ZodArray") {
      const arrayFieldConfig = fieldConfig?.[name] ?? {};
      if (typeof arrayFieldConfig.fieldType === "function") {
        const zodInputProps2 = helpers.zodToHtmlInputProps(item);
        let isRequired2 = isRequiredByDependency || zodInputProps2.required || false;
        if (arrayFieldConfig.inputProps?.required !== void 0) {
          isRequired2 = arrayFieldConfig.inputProps.required;
        }
        const CustomComponent = arrayFieldConfig.fieldType;
        const ParentElement = arrayFieldConfig.renderParent ?? DefaultParent;
        return /* @__PURE__ */ jsxRuntime.jsx(
          form.FormField,
          {
            control: form$1.control,
            name: key,
            render: ({ field }) => {
              const fieldProps = {
                ...zodInputProps2,
                ...field,
                ...arrayFieldConfig.inputProps,
                disabled: arrayFieldConfig.inputProps?.disabled || isDisabled,
                ref: void 0,
                value: field.value
              };
              return /* @__PURE__ */ jsxRuntime.jsx(ParentElement, { children: /* @__PURE__ */ jsxRuntime.jsx(
                CustomComponent,
                {
                  zodInputProps: zodInputProps2,
                  field,
                  fieldConfigItem: arrayFieldConfig,
                  label: arrayFieldConfig.label || itemName,
                  isRequired: isRequired2,
                  zodItem: item,
                  fieldProps
                }
              ) }, `${key}.parent`);
            }
          },
          key
        );
      }
      return /* @__PURE__ */ jsxRuntime.jsx(
        array,
        {
          name,
          item,
          form: form$1,
          fieldConfig: arrayFieldConfig,
          path: [...path, name]
        },
        key
      );
    }
    const fieldConfigItem = fieldConfig?.[name] ?? {};
    const zodInputProps = helpers.zodToHtmlInputProps(item);
    let isRequired = isRequiredByDependency || zodInputProps.required || false;
    if (fieldConfigItem.inputProps?.required !== void 0) {
      isRequired = fieldConfigItem.inputProps.required;
    }
    if (overrideOptions) {
      item = z__namespace.enum(overrideOptions);
    }
    return /* @__PURE__ */ jsxRuntime.jsx(
      form.FormField,
      {
        control: form$1.control,
        name: key,
        render: ({ field }) => {
          const inputType = fieldConfigItem.fieldType ?? config.DEFAULT_ZOD_HANDLERS[zodBaseType] ?? "fallback";
          const InputComponent = typeof inputType === "function" ? inputType : config.INPUT_COMPONENTS[inputType];
          const ParentElement = fieldConfigItem.renderParent ?? DefaultParent;
          const defaultValue = fieldConfigItem.inputProps?.defaultValue;
          const value = field.value ?? defaultValue ?? "";
          const fieldProps = {
            ...helpers.zodToHtmlInputProps(item),
            ...field,
            ...fieldConfigItem.inputProps,
            disabled: fieldConfigItem.inputProps?.disabled || isDisabled,
            ref: void 0,
            value
          };
          if (InputComponent === void 0) {
            return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, {});
          }
          return /* @__PURE__ */ jsxRuntime.jsx(ParentElement, { children: /* @__PURE__ */ jsxRuntime.jsx(
            InputComponent,
            {
              zodInputProps,
              field,
              fieldConfigItem,
              label: itemName,
              isRequired,
              zodItem: item,
              fieldProps,
              className: fieldProps.className
            }
          ) }, `${key}.parent`);
        }
      },
      key
    );
  }) });
}

module.exports = AutoFormObject;
