{"version":3,"file":"DynamicTransactionDomainFormFields.mjs","names":["React","Field","AmbientAutoComplete","AmbientTextField","FormControlLabel","Switch","useTranslation","useTransactionEngine","EditTransactionDetailsFormFields","props","transaction","fieldAttributes","transactionTypeId","t","getHiddenFields","getRequiredFields","getDisabledFields","fieldComponentMap","string","datetime","number","bool","array","getEntityName","optionValuesArr","entityId","matchingEntity","find","entity","id","name","numberDataTypes","removeTimeFormatDate","date","parsedDate","Date","getTime","year","getFullYear","month","getMonth","day","getDate","toString","padStart","addSpacingInHeaderName","headerName","formattedHeaderName","i","length","toUpperCase","match","dataTypeComponentMap","attribute","fieldName","fieldDataType","type","toLowerCase","includes","isAsscoiationLabel","associationLabel","fieldComponentProps","initialValue","entityBusinessObject","InputProps","inputProps","min","options","map","option","getOptionLabelFunction","formattedFieldName","formLabel","defaultValue","field","isRequired","required","nullable","isHidden","isPrimaryKey","extendedProperties","defaultVisible","hidden","isDisabled","isReadOnly","fieldComponent"],"sources":["../../../../../src/udp/pages/UdpTransactionsPage/components/DynamicTransactionDomainFormFields.jsx"],"sourcesContent":["import React from 'react';\nimport { Field } from '../../../../utilities/form';\nimport {\n  AmbientAutoComplete,\n  AmbientTextField,\n} from '../../../../UI/inputs/textField';\nimport { FormControlLabel, Switch } from '@material-ui/core';\nimport { useTranslation } from 'react-i18next';\nimport { useTransactionEngine } from '../../../../utilities/transactions/useTransactionEngine';\n\n\nconst EditTransactionDetailsFormFields = (props) => {\n  const { transaction, fieldAttributes, transactionTypeId } = props;\n  const { t } = useTranslation();\n\n  const { getHiddenFields, getRequiredFields, getDisabledFields } =\n    useTransactionEngine(transactionTypeId);\n\n  const fieldComponentMap = {\n    string: AmbientTextField,\n    datetime: AmbientTextField,\n    number: AmbientTextField,\n    bool: Switch,\n    array: AmbientAutoComplete\n  };\n\n  const getEntityName = (optionValuesArr, entityId) => {\n    const matchingEntity = optionValuesArr?.find(\n      (entity) => entity.id == entityId\n    );\n    return matchingEntity?.name;\n  };\n\n  const numberDataTypes = ['int', 'short', 'long'];\n\n  const removeTimeFormatDate = (date) => {\n    if(date){\n        const parsedDate = new Date(date);\n        if (parsedDate.getTime()) {\n          const year = parsedDate.getFullYear();\n          //Month is zero based 0 = Jan so need to add 1\n          const month = parsedDate.getMonth() + 1;\n          const day = parsedDate.getDate().toString().padStart(2, '0');\n          return `${year}-${month.toString().padStart(2, '0')}-${day}`;\n        }\n        return parsedDate;\n    }\n    return date\n  };\n\n  const addSpacingInHeaderName = (headerName) => {\n  let formattedHeaderName = '';\n  for (var i = 0; i < headerName.length; i++) {\n      if(i == 0){\n        formattedHeaderName += headerName[i].toUpperCase();\n      }else if (i !== 0 && headerName[i].match(/[A-Z]/) && i != headerName.length-1) {\n        formattedHeaderName += ' ' + headerName[i];\n      } else {\n        formattedHeaderName += headerName[i];\n      }\n    }\n    return formattedHeaderName;\n  };\n\n  const dataTypeComponentMap = (transaction, attribute) => {\n    const fieldName = attribute.name;\n    let fieldDataType = attribute.type.toLowerCase();\n    fieldDataType = numberDataTypes.includes(fieldDataType) ? \"number\" : fieldDataType\n    const isAsscoiationLabel = attribute.associationLabel;\n    let fieldComponentProps = null;\n    switch (fieldDataType) {\n      case 'number':\n        fieldComponentProps = {\n          initialValue: transaction?.entityBusinessObject[fieldName],\n          type: 'number',\n          InputProps: { inputProps: { min: 0 } }\n        };\n        break;\n      case 'datetime':\n        fieldComponentProps = {\n          initialValue: removeTimeFormatDate(transaction?.entityBusinessObject[fieldName]),\n          type: \"date\"\n        };\n        break;\n      case 'array':\n        fieldComponentProps = {\n          options: attribute?.options?.map((option) => option.id),\n          getOptionLabelFunction: (option) =>\n            getEntityName(attribute?.options, option) || option,\n          initialValue: transaction?.entityBusinessObject[fieldName]\n        };\n\n        break;\n      case 'bool':\n        fieldComponentProps = {\n          type: 'checkbox',\n          initialValue: false\n        };\n        break;\n      default:\n        fieldComponentProps = {\n          initialValue: transaction?.entityBusinessObject[fieldName]\n        };\n    }\n    const formattedFieldName = addSpacingInHeaderName(fieldName);\n    const formLabel = isAsscoiationLabel\n      ? t('transactions.sidesheet.form.fieldNameLabel', {\n          defaultValue: '{{field}} Name',\n          field: formattedFieldName\n        })\n      : formattedFieldName;\n    const isRequired = getRequiredFields(transaction).includes(fieldName) || attribute.required || !attribute.nullable;\n    const isHidden = getHiddenFields(transaction).includes(fieldName) || attribute.isPrimaryKey || !attribute.extendedProperties.defaultVisible || attribute?.hidden\n    const isDisabled = getDisabledFields(transaction).includes(fieldName) || attribute.isReadOnly\n\n    let fieldComponent = (\n      <Field\n        component={fieldComponentMap[fieldDataType] ?? AmbientTextField}\n        label={t(formLabel)}\n        name={fieldName}\n        fullWidth\n        variant='outlined'\n        margin='normal'\n        disabled={isDisabled}\n        required={isRequired && fieldDataType != 'bool'}\n        {...fieldComponentProps}\n      />\n    );\n    return (\n      <>\n        { !isHidden &&\n          (fieldDataType == 'bool' ? (\n            <FormControlLabel control={fieldComponent} label={formLabel} />\n          ) : (\n            fieldComponent\n          ))\n        }\n      </>\n    );\n  };\n\n  return (\n    <>\n        {fieldAttributes.map((attribute) => {\n          return <>{dataTypeComponentMap(transaction, attribute)}</>;\n        })}\n    </>\n  );\n};\n\nexport default EditTransactionDetailsFormFields;\n"],"mappings":";;;;;;;;AAWA,MAAMQ,oCAAoCC,UAAU;CAClD,MAAM,EAAEC,aAAaC,iBAAiBC,sBAAsBH;CAC5D,MAAM,EAAEI,MAAMP,gBAAgB;CAE9B,MAAM,EAAEQ,iBAAiBC,mBAAmBC,sBAC1CT,qBAAqBK,kBAAkB;CAEzC,MAAMK,oBAAoB;EACxBC,QAAQf;EACRgB,UAAUhB;EACViB,QAAQjB;EACRkB,MAAMhB;EACNiB,OAAOpB;EACR;CAED,MAAMqB,iBAAiBC,iBAAiBC,aAAa;AAInD,UAHuBD,iBAAiBG,MACrCC,WAAWA,OAAOC,MAAMJ,SAC1B,GACsBK;;CAGzB,MAAMC,kBAAkB;EAAC;EAAO;EAAS;EAAO;CAEhD,MAAMC,wBAAwBC,SAAS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