'use strict';

const core = require('@sankhyalabs/core');
const constants = require('./constants-6d82e405.js');

const iconMap = new Map([
  [core.UserInterface.DATE, 'calendar'],
  [core.UserInterface.DATETIME, 'calendar-clock'],
  [core.UserInterface.ELAPSEDTIME, 'timer-outline'],
  [core.UserInterface.TIME, 'timer-outline'],
  [core.UserInterface.DECIMALNUMBER, 'number'],
  [core.UserInterface.INTEGERNUMBER, 'number'],
  [core.UserInterface.FILE, 'zip'],
  [core.UserInterface.IMAGE, 'pdf'],
  [core.UserInterface.HTML, 'tag_code'],
  [core.UserInterface.SEARCH, 'search'],
  [core.UserInterface.SEARCHPLUS, 'search'],
  [core.UserInterface.OPTIONSELECTOR, 'list'],
  [core.UserInterface.CHECKBOX, 'list'],
  [core.UserInterface.SWITCH, 'boolean'],
  [core.UserInterface.PASSWORD, 'eye-off'],
]);
const ID_AVAILABLE_FIELDS = 'container-available-fields';
exports.SORTABLE_PATH = void 0;
(function (SORTABLE_PATH) {
  SORTABLE_PATH["FROM"] = "from";
  SORTABLE_PATH["TO"] = "to";
})(exports.SORTABLE_PATH || (exports.SORTABLE_PATH = {}));
function getFieldsStructure(evt, path, group, tab) {
  var _a;
  const htmlStructure = (_a = evt === null || evt === void 0 ? void 0 : evt[path]) === null || _a === void 0 ? void 0 : _a.children;
  if (!htmlStructure)
    return [];
  return Array.from(htmlStructure).map(({ fieldConfig }) => {
    return Object.assign(Object.assign({}, fieldConfig), { group, tab });
  });
}
function updateTabInFieldsFromGroupList(guideGroups, updatedName) {
  return guideGroups.map(group => {
    return Object.assign(Object.assign({}, group), { fields: group.fields.map(field => (Object.assign(Object.assign({}, field), { tab: updatedName }))) });
  });
}
function getValidatedFieldValue(fieldValue, dataUnit, fieldName) {
  var _a;
  if (fieldValue == undefined || !dataUnit || !fieldName)
    return;
  const dataType = (_a = dataUnit.getField(fieldName)) === null || _a === void 0 ? void 0 : _a.dataType;
  if (dataType == undefined)
    return;
  if ((fieldValue === null || fieldValue === void 0 ? void 0 : fieldValue.waitmessage) !== undefined)
    return undefined;
  const isNonObjectValue = dataType !== core.DataType.OBJECT &&
    dataType !== core.DataType.DATE &&
    typeof fieldValue === 'object';
  const processedValue = isNonObjectValue ? fieldValue.value : fieldValue;
  return dataType === core.DataType.OBJECT
    ? JSON.stringify(processedValue)
    : processedValue;
}
function getEnabledByProperty(attribName, propertyName, fieldDescriptor) {
  if (!fieldDescriptor)
    return true;
  const fieldProperties = fieldDescriptor.properties || {};
  const fromAttribName = fieldDescriptor[attribName] !== true;
  return fieldProperties.hasOwnProperty(propertyName)
    ? fieldProperties[propertyName] !== 'S'
    : fromAttribName;
}
function getValueByProperty(property, fieldDescriptor, defaultValue) {
  if (fieldDescriptor && fieldDescriptor[property] === true) {
    return true;
  }
  return defaultValue;
}
function getIconName(userInterface) {
  return iconMap.get(userInterface) || 'text';
}
function buildNewGuideName(guidesList, getMessage) {
  var _a, _b;
  const newGuideText = getMessage('snkFormConfig.form.newGuide');
  const qtdWithNewGuideName = (_b = (_a = guidesList.filter(g => g.name.includes(newGuideText))) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
  return qtdWithNewGuideName === 0 ? newGuideText : `${newGuideText} (${qtdWithNewGuideName})`;
}
function getFieldsToSave(guidesMap) {
  const fieldsToSave = [];
  guidesMap.forEach(groups => {
    groups.forEach(({ fields }) => {
      fields === null || fields === void 0 ? void 0 : fields.forEach((fieldConfig) => {
        fieldsToSave.push(buildFieldToSave(fieldConfig));
      });
    });
  });
  return fieldsToSave;
}
function buildGuidesMap(rawFields, tabs, dataUnit, generalTab) {
  const fields = handleFieldsTabNames(rawFields);
  const guidesMap = initGuidesMap(tabs);
  if (!(fields === null || fields === void 0 ? void 0 : fields.length))
    return;
  for (let guideName of guidesMap.keys()) {
    const fieldsFromGuide = getFieldsFromGuide(fields, guideName, generalTab);
    const fieldsByGroupMap = getGroupsFromGuide(fieldsFromGuide, dataUnit, generalTab);
    const groupsList = parserGroupMapIntoList(fieldsByGroupMap);
    guidesMap.set(guideName, groupsList);
  }
  return guidesMap;
}
function buildGuides(config, mainArea) {
  let tabs = config.tabs ? [...config.tabs] : buildTabsFromFields(config === null || config === void 0 ? void 0 : config.fields);
  tabs = getPossibleGuides(tabs);
  tabs = initializeTabsNames(tabs);
  return handleMainTab(tabs, mainArea);
}
function buildChildrenGuides(dataUnit, initialOrderIndex = 0, guidesList) {
  var _a;
  if (!dataUnit || !dataUnit.metadata) {
    return [];
  }
  const children = ((_a = dataUnit.metadata) === null || _a === void 0 ? void 0 : _a.children) || [];
  const childrenWithNoGuide = [];
  for (const child of children) {
    const guideOnConfig = guidesList.find(guide => child.label === guide.label);
    if (guideOnConfig) {
      guideOnConfig.isCustom = true;
      continue;
    }
    childrenWithNoGuide.push({
      label: child.label,
      name: child.name,
      visible: true,
      isCustom: true,
      order: initialOrderIndex++,
    });
  }
  return childrenWithNoGuide;
}
function handleFieldsTabNames(allFields) {
  // Campos que não fazem parte de nenhuma aba ou da aba "Geral", devem ser movidos para a aba __main.
  return allFields.map(field => {
    const tabName = field.tab;
    return Object.assign(Object.assign({}, field), { tab: (!tabName || tabName === constants.TAB_NAMES.general) ? constants.TAB_NAMES.main : field.tab });
  });
}
function initializeTabsNames(tabs) {
  return tabs.map((tab) => {
    return Object.assign(Object.assign({}, tab), { name: tab.name ? tab.name : tab.label });
  });
}
function handleMainTab(tabs, mainTabName) {
  const mainTab = { name: constants.TAB_NAMES.main, label: mainTabName, visible: true };
  const hasTabMain = tabs.some((tab) => tab.name === constants.TAB_NAMES.main);
  if (hasTabMain) {
    tabs = tabs.map(tab => tab.name === constants.TAB_NAMES.main ? mainTab : tab);
  }
  else {
    tabs.unshift(mainTab);
  }
  return tabs;
}
function getPossibleGuides(tabs) {
  //O usuário não pode criar uma guia com o nome "Geral".
  // Nesses casos, precisamos mover os campos dessa guia, para a guia __main.
  return [...tabs.filter(tab => tab.label !== constants.TAB_NAMES.general)];
}
function initGuidesMap(tabs) {
  const map = new Map();
  let possibleGuides = getPossibleGuides(tabs);
  possibleGuides.forEach(tab => {
    map.set(tab.name, []);
  });
  return map;
}
function handleFieldLabel(field, fieldDescriptor) {
  if (!field.label) {
    field.label = fieldDescriptor.label;
  }
}
function handleFieldTab(field, generalTabName) {
  if (!field.tab) {
    field.tab = generalTabName;
  }
}
function handleFieldGroup(field) {
  if (!field.group) {
    field.group = constants.GROUP_NAMES.noGroup;
  }
}
function isVisibleOnConfig(fieldDescriptor) {
  var _a;
  /**
   * Quando um campo é ocultado pelo dev, mas queremos manter ele na config precisamos fazer essa tratativa.
   */
  return ((_a = fieldDescriptor === null || fieldDescriptor === void 0 ? void 0 : fieldDescriptor.properties) === null || _a === void 0 ? void 0 : _a.visibleOnConfig) === true && (fieldDescriptor === null || fieldDescriptor === void 0 ? void 0 : fieldDescriptor.visible) === false;
}
function getGroupsFromGuide(fieldsFromGuide, dataUnit, generalTabName) {
  const groupsMap = new Map();
  groupsMap.set(constants.GROUP_NAMES.noGroup, []);
  for (let field of fieldsFromGuide) {
    const fieldDescriptor = dataUnit.getField(field.name);
    if (!(fieldDescriptor === null || fieldDescriptor === void 0 ? void 0 : fieldDescriptor.visible) && !isVisibleOnConfig(fieldDescriptor))
      continue;
    handleFieldLabel(field, fieldDescriptor);
    handleFieldTab(field, generalTabName);
    handleFieldGroup(field);
    if (groupsMap.has(field.group)) {
      groupsMap.get(field.group).push(field);
    }
    else {
      groupsMap.set(field.group, [field]);
    }
  }
  return groupsMap;
}
function getFieldsFromGuide(fields, guideName, generalTabName) {
  return fields.filter(field => {
    const fieldTab = getTabName(field.tab, generalTabName);
    return fieldTab === guideName;
  });
}
function getTabName(tab, generalTabName) {
  var _a;
  if (typeof tab === 'string')
    return tab;
  return (_a = tab === null || tab === void 0 ? void 0 : tab.label) !== null && _a !== void 0 ? _a : generalTabName;
}
function parserGroupMapIntoList(groupsMap) {
  const groupsList = [];
  for (let groupName of groupsMap.keys()) {
    groupsList.push({ name: groupName, fields: groupsMap.get(groupName) });
  }
  return groupsList;
}
function canAddTab(tab, guideList) {
  return tab && !(guideList.map(g => g.name).includes(tab));
}
function buildTabsFromFields(fields) {
  const guideList = [];
  let order = 1;
  fields === null || fields === void 0 ? void 0 : fields.forEach((field) => {
    const tab = field.tab;
    if (canAddTab(tab, guideList)) {
      guideList.push({ name: tab, label: tab, visible: true, order });
      order = order + 1;
    }
  });
  return guideList;
}
function buildFieldToSave(fieldConfig) {
  const fieldToSave = {
    cleanOnCopy: fieldConfig.cleanOnCopy || false,
    group: fieldConfig.group === constants.GROUP_NAMES.noGroup ? undefined : fieldConfig.group,
    label: fieldConfig.label,
    name: fieldConfig.name,
    required: fieldConfig.required,
    readOnly: fieldConfig.readOnly,
    tab: fieldConfig.tab,
  };
  if (fieldConfig.defaultValue != undefined) {
    if (fieldConfig.defaultValue.type == undefined) {
      fieldToSave.defaultValue = {
        type: constants.DEFAULT_TYPE.fixed,
        value: fieldConfig.defaultValue,
      };
    }
    else {
      fieldToSave.defaultValue = fieldConfig.defaultValue;
    }
  }
  return fieldToSave;
}

exports.ID_AVAILABLE_FIELDS = ID_AVAILABLE_FIELDS;
exports.buildChildrenGuides = buildChildrenGuides;
exports.buildGuides = buildGuides;
exports.buildGuidesMap = buildGuidesMap;
exports.buildNewGuideName = buildNewGuideName;
exports.getEnabledByProperty = getEnabledByProperty;
exports.getFieldsStructure = getFieldsStructure;
exports.getFieldsToSave = getFieldsToSave;
exports.getIconName = getIconName;
exports.getValidatedFieldValue = getValidatedFieldValue;
exports.getValueByProperty = getValueByProperty;
exports.updateTabInFieldsFromGroupList = updateTabInFieldsFromGroupList;
