import { DataType, UserInterface } from '@sankhyalabs/core';
export function buildGridMetadata(rawValue) {
  const fieldsMetadata = buildFieldsMetadata(rawValue.fieldsMetadata);
  const duName = rawValue.descriptionField;
  const duLabel = rawValue.entityDescription;
  return {
    label: duLabel,
    name: duName,
    fields: fieldsMetadata,
  };
}
function buildFieldsMetadata(rawMetadata) {
  if (!rawMetadata || !rawMetadata.length)
    return [];
  return rawMetadata.map(field => {
    return {
      name: field['fieldName'],
      label: field['description'],
      dataType: buildDataType(field['type']),
      userInterface: buildUserInterface(field),
      properties: bildProperties(field)
    };
  });
}
function bildProperties(field) {
  const properties = {};
  properties['options'] = buildOptions(field);
  properties['mask'] = buildMask(field);
  return properties;
}
function buildMask(field) {
  const fieldUI = field['uiType'];
  return fieldUI ? String(fieldUI).toLowerCase() : undefined;
}
function buildOptions(field) {
  const optionsJson = field.options;
  if (!optionsJson)
    return undefined;
  const options = [];
  Object.keys(optionsJson).forEach(key => {
    options.push({
      label: key,
      value: optionsJson[key]
    });
  });
  return options;
}
function buildUserInterface(field) {
  const options = field.options;
  if (options && Object.keys(options).length > 0)
    return UserInterface.OPTIONSELECTOR;
  if (field['type'] === 'D')
    return UserInterface.DATE;
  if (field['type'] === 'H')
    return UserInterface.DATETIME;
  if (field['type'] === 'T')
    return UserInterface.TIME;
  if (field['type'] === 'I')
    return UserInterface.INTEGERNUMBER;
  return undefined;
}
function buildDataType(rawType) {
  switch (rawType) {
    case 'F':
      return DataType.NUMBER;
    case 'D':
    case 'H':
      return DataType.DATE;
    case 'B':
      return DataType.OBJECT;
    case 'I':
      return DataType.NUMBER;
    default:
      return DataType.TEXT;
  }
}
export function parseLegacyTree(legacy) {
  var _a, _b;
  const structure = (_b = (_a = legacy.arvore) === null || _a === void 0 ? void 0 : _a.no) !== null && _b !== void 0 ? _b : [];
  return structure.map(parseLegacyItem);
}
function parseLegacyItem(node) {
  return Object.assign({ id: node.codigo, label: `${node.caminho} - ${node.descricao}`, bold: node.analitico === "N", props: { analitico: node.analitico === 'S' } }, (node.no && { children: node.no.map(parseLegacyItem) }));
}
export function parseGridConfig(rawConfig) {
  var _a, _b;
  const columns = [];
  const fields = (_b = (_a = rawConfig === null || rawConfig === void 0 ? void 0 : rawConfig.gridConfig) === null || _a === void 0 ? void 0 : _a.field) !== null && _b !== void 0 ? _b : {};
  Object.keys(fields).forEach(key => columns.push(fields[key]));
  if (!columns.length)
    return undefined;
  return { columns };
}
export var PESQUISA_VIEW_MODE;
(function (PESQUISA_VIEW_MODE) {
  PESQUISA_VIEW_MODE[PESQUISA_VIEW_MODE["GRID"] = 0] = "GRID";
  PESQUISA_VIEW_MODE[PESQUISA_VIEW_MODE["CARDS"] = 1] = "CARDS";
  PESQUISA_VIEW_MODE[PESQUISA_VIEW_MODE["TREE"] = 2] = "TREE";
})(PESQUISA_VIEW_MODE || (PESQUISA_VIEW_MODE = {}));
