'use strict';

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

function toDate(value) {
  if (!value) return void 0;
  if (value instanceof Date) return value;
  if (typeof value === "string") {
    const date = new Date(value);
    return isNaN(date.getTime()) ? void 0 : date;
  }
  return void 0;
}
function AutoFormDate({
  label: label$1,
  isRequired,
  field,
  fieldConfigItem,
  fieldProps,
  zodItem
}) {
  const baseType = helpers.getBaseType(zodItem);
  const expectsDateObject = baseType === "ZodDate";
  const handleChange = (date) => {
    if (!date) {
      field.onChange(void 0);
      return;
    }
    if (expectsDateObject) {
      field.onChange(date);
    } else {
      field.onChange(date.toISOString());
    }
  };
  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.jsx(
      datePicker.DatePicker,
      {
        date: toDate(field.value),
        setDate: handleChange,
        ...fieldProps
      }
    ) }),
    /* @__PURE__ */ jsxRuntime.jsx(tooltip, { fieldConfigItem }),
    /* @__PURE__ */ jsxRuntime.jsx(form.FormMessage, {})
  ] });
}

module.exports = AutoFormDate;
