import { jsx } from "react/jsx-runtime";
import { useRef } from "react";
import { Field as FormikField, getIn } from "formik";
import SingleLineTextTableField from "./components/SingleLineTextTableField";
import LongTextTableField from "./components/LongTextTableField";
import CheckboxTableField from "./components/CheckboxTableField";
import MultipleSelectTableField from "./components/MultipleSelectTableField";
import SingleSelectTableField from "./components/SingleSelectTableField";
import PhoneNumberTableField from "./components/PhoneNumberTableField";
import EmailTableField from "./components/EmailTableField";
import URLTableField from "./components/URLTableField";
import NumberTableField from "./components/NumberTableField";
import PlaceholderTableField from "./components/PlaceholderTableField";
function getTypeMismatchErrorMessage(tableColumn, label) {
  switch ((tableColumn || {}).__typename) {
    case "PhoneNumberTableColumn":
      return `${label} field must be a valid phone number.`;
    case "EmailTableColumn":
      return `${label} field must be a valid email.`;
    case "URLTableColumn":
      return `${label} field must be a valid URL.`;
    case "NumberTableColumn":
      return `${label} field must be a valid number.`;
    case "SingleLineTextTableColumn":
    case "LongTextTableColumn":
    case "CheckboxTableColumn":
    default:
      return `${label} field is invalid.`;
  }
}
function getTableColumnField(tableColumn) {
  switch ((tableColumn || {}).__typename) {
    case "SingleLineTextTableColumn":
      return SingleLineTextTableField;
    case "LongTextTableColumn":
      return LongTextTableField;
    case "CheckboxTableColumn":
      return CheckboxTableField;
    case "MultipleSelectTableColumn":
      return MultipleSelectTableField;
    case "SingleSelectTableColumn":
      return SingleSelectTableField;
    case "PhoneNumberTableColumn":
      return PhoneNumberTableField;
    case "EmailTableColumn":
      return EmailTableField;
    case "URLTableColumn":
      return URLTableField;
    case "NumberTableColumn":
      return NumberTableField;
    default:
      return SingleLineTextTableField;
  }
}
function escapeCharacters(string) {
  return string.replace(/[[\]']/g, "\\$&");
}
function Field({
  tableColumn,
  tableFormField: {
    id,
    label = "",
    placeholder,
    required = false,
    hidden = false,
    type = "radio",
    hideLabel = false
  }
}) {
  const TableColumnField = getTableColumnField(tableColumn);
  const input = useRef(null);
  if (!tableColumn)
    return /* @__PURE__ */ jsx(PlaceholderTableField, {});
  function validate() {
    let errorMessage;
    if (input.current) {
      const { validity = {} } = input.current;
      if (validity.valueMissing)
        errorMessage = `${label} is required.`;
      if (validity.typeMismatch)
        errorMessage = getTypeMismatchErrorMessage(tableColumn, label);
    }
    return errorMessage;
  }
  const formikFieldName = `['${escapeCharacters(tableColumn.name)}']`;
  return /* @__PURE__ */ jsx(FormikField, { name: formikFieldName, validate, children: ({ field, form }) => hidden ? /* @__PURE__ */ jsx("input", { ...field, ref: input, type: "hidden" }) : /* @__PURE__ */ jsx(
    TableColumnField,
    {
      ...field,
      type,
      form,
      tableColumn,
      ref: input,
      id,
      error: getIn(form.touched, field.name) && getIn(form.errors, field.name),
      label,
      placeholder,
      required,
      hideLabel
    }
  ) });
}
export {
  Field as default
};
//# sourceMappingURL=index.js.map