'use strict';

const jsxRuntime = require('react/jsx-runtime');
const form = require('../../form.cjs');
const select = require('../../select.cjs');
const label = require('../common/label.cjs');
const tooltip = require('../common/tooltip.cjs');
const helpers = require('../helpers.cjs');

function getEnumValues(schema) {
  if (Array.isArray(schema.options)) {
    return schema.options;
  }
  if (schema.enum) {
    return Object.values(schema.enum);
  }
  const def = schema._zod?.def;
  if (def?.entries) {
    return Object.values(def.entries);
  }
  return [];
}
function AutoFormEnum({
  label: label$1,
  isRequired,
  field,
  fieldConfigItem,
  zodItem,
  fieldProps
}) {
  const baseSchema = helpers.getBaseSchema(zodItem);
  const baseValues = getEnumValues(baseSchema);
  let values = [];
  if (!baseValues || baseValues.length === 0) {
    values = [];
  } else {
    values = baseValues.map((value) => [value, value]);
  }
  function findItem(value) {
    return values.find((item) => item[0] === value);
  }
  const handleValueChange = (val) => {
    if (val === "" && field.value && field.value !== "") {
      return;
    }
    field.onChange(val);
  };
  return /* @__PURE__ */ jsxRuntime.jsxs(form.FormItem, { children: [
    /* @__PURE__ */ jsxRuntime.jsx(
      label,
      {
        label: fieldConfigItem?.label || label$1,
        isRequired
      }
    ),
    /* @__PURE__ */ jsxRuntime.jsx(form.FormControl, { children: /* @__PURE__ */ jsxRuntime.jsxs(
      select.Select,
      {
        onValueChange: handleValueChange,
        value: field.value ?? "",
        ...fieldProps,
        children: [
          /* @__PURE__ */ jsxRuntime.jsx(select.SelectTrigger, { className: fieldProps.className, children: /* @__PURE__ */ jsxRuntime.jsx(select.SelectValue, { placeholder: fieldConfigItem.inputProps?.placeholder, children: field.value ? findItem(field.value)?.[1] : "Select an option" }) }),
          /* @__PURE__ */ jsxRuntime.jsx(select.SelectContent, { children: values.map(([value, label2]) => /* @__PURE__ */ jsxRuntime.jsx(select.SelectItem, { value: label2, children: label2 }, value)) })
        ]
      }
    ) }),
    /* @__PURE__ */ jsxRuntime.jsx(tooltip, { fieldConfigItem }),
    /* @__PURE__ */ jsxRuntime.jsx(form.FormMessage, {})
  ] });
}

module.exports = AutoFormEnum;
