import DateUtils from '../../utils/DateUtils.js'; import { MaskFormatter } from '../../utils/MaskFormatter.js'; import { NumberUtils } from '../../utils/NumberUtils.js'; import { TimeFormatter } from '../../utils/TimeFormatter.js'; import { DataType, toString } from '../metadata/DataType.js'; import { FieldDescriptor, UserInterface } from '../metadata/UnitMetadata.js'; export const getFormattedValue = (value: any, descriptor?: FieldDescriptor) => { if(descriptor?.userInterface === UserInterface.FILE){ return getFileFormat(value); } if(descriptor?.dataType === DataType.OBJECT){ if(value && Object.hasOwn(value, "value")){ return getSearchFormat(value, descriptor); } if(value === null || value === undefined) return ""; return value.toString(); } if(descriptor?.userInterface === UserInterface.OPTIONSELECTOR){ return getOptionFormat(value, descriptor); } if(descriptor?.dataType === DataType.BOOLEAN){ return value ? "Sim" : "Não"; } if(descriptor?.dataType === DataType.NUMBER){ return getNumberFormat(value, descriptor); } if(descriptor?.userInterface === UserInterface.DATE){ return DateUtils.formatDate(value); } if(descriptor?.userInterface === UserInterface.TIME){ return DateUtils.formatTime(value); } if(descriptor?.userInterface === UserInterface.ELAPSEDTIME){ return TimeFormatter.prepareValue(value, true); } if(descriptor?.userInterface === UserInterface.DATETIME){ return DateUtils.formatDateTime(value); } const mask = getMask(value, descriptor); if(mask != undefined){ try{ return new MaskFormatter(mask).format(value, true); } catch(error){ console.warn(`Erro ao formatar valor: ${value}. Mascara: ${mask}. Erro: ${error}`); } } return toString(descriptor?.dataType, value); } const getNumberFormat = (value: any, descriptor: FieldDescriptor) => { if(value === undefined || value === null) return ""; if(descriptor.userInterface === UserInterface.INTEGERNUMBER){ return value; } const precision = Number(descriptor.properties?.precision || 2); const prettyPrecision = Number(descriptor.properties?.prettyPrecision || precision); return NumberUtils.format(value, precision, prettyPrecision); } const getSearchFormat = (value: any, descriptor: FieldDescriptor) => { const {value: codeValue, label} = value; if(descriptor.userInterface === UserInterface.SEARCH){ return label ? `${codeValue} - ${label}` : codeValue; } if(descriptor.userInterface === UserInterface.SEARCHPLUS){ return codeValue; } return label ? label : codeValue; } const getOptionFormat = (value: any, descriptor: FieldDescriptor) => { const prop: string | Array<{label: string, value: string}> = descriptor.properties?.options; let options: any; if (typeof prop === "string") { options = JSON.parse(prop); } else { options = {}; if(prop != undefined){ if (descriptor.properties?.switchLabelsAndValues) { prop.forEach(opt => options[opt.value] = opt.label); } prop.forEach(opt => options[opt.label] = opt.value); } } if(value && typeof value === "object" ){ value = value.value } if(options && Object.hasOwn(options, value)){ return options[value]; } return value ?? ""; } const getMask = (value: string, descriptor: FieldDescriptor | undefined): string | undefined => { if(descriptor == undefined){ return; } const mask = descriptor.properties?.mask; if(mask == undefined || !value){ return; } if(mask === "cgc_cpf"){ return value.length === 11 ? MaskFormatter.DEFAULT_MASKS.cpf : MaskFormatter.DEFAULT_MASKS.cnpj; } if(Object.hasOwn(MaskFormatter.DEFAULT_MASKS, mask)){ return MaskFormatter.DEFAULT_MASKS[mask]; } return mask; } const getFileFormat = (value: any) => { if (value == undefined) return ''; if (!value || !Array.isArray(value)) return value; if (value.length === 1) return value[0].name; return `${value.length} arquivos`; }