"use strict";
const common = require("./common-B47boGYI.js");
function deFormatText(key) {
  return key.toLowerCase().split(" ").join("_");
}
const formBuilderConversion = (data) => {
  const backend_output = [];
  const addSectionInBackendOutput = (section) => {
    if ((section == null ? void 0 : section.section_type) === common.SECTION_TYPES.FORM_SWITCH_SECTION) {
      backend_output.push({
        is_section_field: true,
        field_type: section == null ? void 0 : section.field_type,
        label: section == null ? void 0 : section.label,
        section_type: section == null ? void 0 : section.section_type,
        form_switcher_label: section == null ? void 0 : section.form_switcher_label,
        form_switcher_name: section == null ? void 0 : section.form_switcher_name,
        switcher_forms: section == null ? void 0 : section.switcher_forms,
        is_accordion: section == null ? void 0 : section.is_accordion,
        section_name: (section == null ? void 0 : section.label) || "",
        name: (section == null ? void 0 : section.label) || "",
        section_order: (section == null ? void 0 : section.section_order) || 0,
        placeholder: "",
        validation: []
      });
    }
  };
  data.tab && data.tab.forEach((tab) => {
    var _a;
    const tab_name = tab.title;
    const tab_order = tab.tab_order;
    (_a = tab.data) == null ? void 0 : _a.forEach((section) => {
      var _a2;
      addSectionInBackendOutput(section);
      const section_name = section.label;
      const is_accordion = section.is_accordion;
      (_a2 = section.members) == null ? void 0 : _a2.forEach((member) => {
        var _a3, _b, _c, _d, _e;
        const validationArray = [];
        if (member.field_type !== "info") {
          validationArray.push(
            {
              type: "required",
              value: member.is_required,
              msg_en: "This Field is required"
            },
            {
              type: "unique",
              value: member.is_unique,
              msg_en: "This Field is unique"
            }
          );
        }
        if (member.field_type === "text") {
          validationArray.push(
            {
              type: "min_length",
              value: member.min_length ?? 1,
              msg_en: `The minimum length should be ${member.min_length}`
            },
            {
              type: "max_length",
              value: member.max_length || (member == null ? void 0 : member.is_multiline) ? 1e3 : 255,
              msg_en: `The maximum length should be ${member.max_length}`
            }
          );
        } else if (member.field_type === "number") {
          validationArray.push(
            {
              type: "min_val",
              value: member.min_length ?? 1,
              msg_en: `The minimum value should be ${member.min_length}`
            },
            {
              type: "max_val",
              value: member.max_length ?? 1e4,
              msg_en: `The maximum value should be ${member.max_length}`
            }
          );
        } else if (member.field_type === "date") {
          validationArray.push(
            {
              type: "is_future_dates_allowed",
              value: member.is_future_dates_allowed ?? false,
              msg_en: ""
            },
            {
              type: "is_past_dates_allowed",
              value: member.is_past_dates_allowed ?? false,
              msg_en: ""
            }
          );
        } else if (member.field_type === "image" || member.field_type === "file") {
          validationArray.push({
            type: "max_file_size",
            value: member.max_size && member.max_size > 2 ? Number(member.max_size) : 20,
            msg_en: `Max size of file should be ${member.max_size}`
          });
        }
        const outputObj = {
          ...member,
          tab_name,
          section_name: section_name || "",
          tab_order,
          section_order: member.section_order || 0,
          label: member.label,
          name: (member == null ? void 0 : member.name) || deFormatText(member.label) || "",
          placeholder: member.placeholder,
          field_type: member.field_type === "file" ? "media" : member.field_type,
          default_value: member.default_value,
          ...member.option && (member == null ? void 0 : member.field_type) === "time" && { time_format: member.option },
          ...member.option && (member == null ? void 0 : member.field_type) === "currency" && { currency: member.option },
          table_columns: (_a3 = member.table_columns) == null ? void 0 : _a3.map((col) => (member == null ? void 0 : member.is_new) ? `${member == null ? void 0 : member.table}.${col.accessorKey}` : col.accessorKey),
          field_properties: {
            ...member.is_multiline !== void 0 && {
              is_multiline: member.is_multiline
            },
            is_accordion,
            ...member.is_negative !== void 0 && {
              is_negative: member.is_negative
            },
            ...member.float_step && {
              float_step: Number(member.float_step)
            },
            ...member.options && { options: member.options },
            ...member.is_multiselect !== void 0 && {
              is_multiselect: member.is_multiselect
            },
            ...member.title !== void 0 && {
              title: member.title
            },
            ...member.title_position !== void 0 && {
              title_position: member.title_position
            },
            ...(member == null ? void 0 : member.table_columns) !== void 0 && {
              table: (member == null ? void 0 : member.table) || "custom",
              table_columns: (_b = member.table_columns) == null ? void 0 : _b.map((col) => (member == null ? void 0 : member.is_new) ? `${member == null ? void 0 : member.table}.${col.accessorKey}` : col.accessorKey)
            },
            ...((_c = member == null ? void 0 : member.field_properties) == null ? void 0 : _c.apiType) !== void 0 && {
              apiType: member.field_properties.apiType
            },
            ...((_d = member == null ? void 0 : member.field_properties) == null ? void 0 : _d.customeFilter) !== void 0 && {
              customeFilter: member.field_properties.customeFilter
            },
            ...((_e = member == null ? void 0 : member.field_properties) == null ? void 0 : _e.attributes) !== void 0 && {
              attributes: member.field_properties.attributes
            }
          },
          validation: validationArray
        };
        if (outputObj) {
          backend_output.push(outputObj);
        }
      });
    });
  });
  data.section && data.section.forEach((section) => {
    addSectionInBackendOutput(section);
    const section_name = section.label;
    const is_accordion = section.is_accordion;
    section.members.forEach((member) => {
      var _a, _b, _c, _d, _e;
      const validationArray = [];
      if (member.field_type !== "info") {
        validationArray.push(
          {
            type: "required",
            value: member.is_required ?? false,
            msg_en: "This Field is required"
          },
          {
            type: "unique",
            value: member.is_unique ?? false,
            msg_en: "This Field is unique"
          }
        );
      }
      if (member.field_type === "text") {
        validationArray.push(
          {
            type: "min_length",
            value: member.min_length ?? 1,
            msg_en: `The minimum length should be ${member.min_length}`
          },
          {
            type: "max_length",
            value: member.max_length || (member == null ? void 0 : member.is_multiline) ? 1e3 : 255,
            msg_en: `The maximum length should be ${member.max_length}`
          }
        );
      } else if (member.field_type === "number") {
        validationArray.push(
          {
            type: "min_val",
            value: member.min_length ?? 1,
            msg_en: `The minimum value should be ${member.min_length}`
          },
          {
            type: "max_val",
            value: member.max_length ?? 1e4,
            msg_en: `The maximum value should be ${member.max_length}`
          }
        );
      } else if (member.field_type === "date") {
        validationArray.push(
          {
            type: "is_future_dates_allowed",
            value: member.is_future_dates_allowed ?? false,
            msg_en: ""
          },
          {
            type: "is_past_dates_allowed",
            value: member.is_past_dates_allowed ?? false,
            msg_en: ""
          }
        );
      } else if (member.field_type === "image" || member.field_type === "file") {
        validationArray.push({
          type: "max_file_size",
          value: member.max_size && member.max_size > 2 ? Number(member.max_size) : 20,
          msg_en: `Max size of file should be ${member.max_size}`
        });
      }
      const outputObj = {
        ...member,
        section_order: member.section_order,
        section_name,
        label: member.label,
        name: (member == null ? void 0 : member.name) || deFormatText(member.label),
        placeholder: member.placeholder,
        field_type: member.field_type === "file" ? "media" : member.field_type,
        default_value: member.default_value,
        ...member.option && (member == null ? void 0 : member.field_type) === "time" && { time_format: member.option },
        ...member.option && (member == null ? void 0 : member.field_type) === "currency" && { currency: member.option },
        table_columns: (_a = member.table_columns) == null ? void 0 : _a.map((col) => (member == null ? void 0 : member.is_new) ? `${member == null ? void 0 : member.table}.${col.accessorKey}` : col.accessorKey),
        field_properties: {
          ...member.is_multiline !== void 0 && {
            is_multiline: member.is_multiline
          },
          is_accordion,
          ...member.is_negative !== void 0 && {
            is_negative: member.is_negative
          },
          ...member.float_step && {
            float_step: Number(member.float_step)
          },
          ...member.options && { options: member.options },
          ...member.is_multiselect !== void 0 && {
            is_multiselect: member.is_multiselect
          },
          ...member.title !== void 0 && {
            title: member.title
          },
          ...member.title_position !== void 0 && {
            title_position: member.title_position
          },
          ...(member == null ? void 0 : member.table_columns) !== void 0 && {
            table: (member == null ? void 0 : member.table) || "custom",
            table_columns: (_b = member.table_columns) == null ? void 0 : _b.map((col) => (member == null ? void 0 : member.is_new) ? `${member == null ? void 0 : member.table}.${col.accessorKey}` : col.accessorKey)
          },
          ...((_c = member == null ? void 0 : member.field_properties) == null ? void 0 : _c.apiType) !== void 0 && {
            apiType: member.field_properties.apiType
          },
          ...((_d = member == null ? void 0 : member.field_properties) == null ? void 0 : _d.customeFilter) !== void 0 && {
            customeFilter: member.field_properties.customeFilter
          },
          ...((_e = member == null ? void 0 : member.field_properties) == null ? void 0 : _e.attributes) !== void 0 && {
            attributes: member.field_properties.attributes
          }
        },
        validation: validationArray
      };
      if (outputObj) {
        backend_output.push(outputObj);
      }
    });
  });
  return backend_output;
};
exports.deFormatText = deFormatText;
exports.formBuilderConversion = formBuilderConversion;
//# sourceMappingURL=form-builder-conversion-ljHVeDPX.js.map
