"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Field_exports = {};
__export(Field_exports, {
  default: () => Field
});
module.exports = __toCommonJS(Field_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_formik = require("formik");
var import_SingleLineTextTableField = __toESM(require("./components/SingleLineTextTableField"));
var import_LongTextTableField = __toESM(require("./components/LongTextTableField"));
var import_CheckboxTableField = __toESM(require("./components/CheckboxTableField"));
var import_MultipleSelectTableField = __toESM(require("./components/MultipleSelectTableField"));
var import_SingleSelectTableField = __toESM(require("./components/SingleSelectTableField"));
var import_PhoneNumberTableField = __toESM(require("./components/PhoneNumberTableField"));
var import_EmailTableField = __toESM(require("./components/EmailTableField"));
var import_URLTableField = __toESM(require("./components/URLTableField"));
var import_NumberTableField = __toESM(require("./components/NumberTableField"));
var import_PlaceholderTableField = __toESM(require("./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 import_SingleLineTextTableField.default;
    case "LongTextTableColumn":
      return import_LongTextTableField.default;
    case "CheckboxTableColumn":
      return import_CheckboxTableField.default;
    case "MultipleSelectTableColumn":
      return import_MultipleSelectTableField.default;
    case "SingleSelectTableColumn":
      return import_SingleSelectTableField.default;
    case "PhoneNumberTableColumn":
      return import_PhoneNumberTableField.default;
    case "EmailTableColumn":
      return import_EmailTableField.default;
    case "URLTableColumn":
      return import_URLTableField.default;
    case "NumberTableColumn":
      return import_NumberTableField.default;
    default:
      return import_SingleLineTextTableField.default;
  }
}
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 = (0, import_react.useRef)(null);
  if (!tableColumn)
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_PlaceholderTableField.default, {});
  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__ */ (0, import_jsx_runtime.jsx)(import_formik.Field, { name: formikFieldName, validate, children: ({ field, form }) => hidden ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", { ...field, ref: input, type: "hidden" }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    TableColumnField,
    {
      ...field,
      type,
      form,
      tableColumn,
      ref: input,
      id,
      error: (0, import_formik.getIn)(form.touched, field.name) && (0, import_formik.getIn)(form.errors, field.name),
      label,
      placeholder,
      required,
      hideLabel
    }
  ) });
}
//# sourceMappingURL=index.js.map