import React from 'react';
import { Field } from '../../../../utilities/form';
import { AmbientAutoComplete, AmbientTextField, } from '../../../../UI/inputs/textField';
import { FormControlLabel, Switch } from '@material-ui/core';
import { useTranslation } from 'react-i18next';
import { useTransactionEngine } from '../../../../utilities/transactions/useTransactionEngine';
const EditTransactionDetailsFormFields = (props) => {
    const { transaction, fieldAttributes, transactionTypeId } = props;
    const { t } = useTranslation();
    const { getHiddenFields, getRequiredFields } = useTransactionEngine(transactionTypeId);
    const fieldComponentMap = {
        string: AmbientTextField,
        datetime: AmbientTextField,
        number: AmbientTextField,
        bool: Switch,
        array: AmbientAutoComplete
    };
    const getEntityName = (optionValuesArr, entityId) => {
        const matchingEntity = optionValuesArr === null || optionValuesArr === void 0 ? void 0 : optionValuesArr.find((entity) => entity.id == entityId);
        return matchingEntity === null || matchingEntity === void 0 ? void 0 : matchingEntity.name;
    };
    const numberDataTypes = ['int', 'short', 'long'];
    const removeTimeFormatDate = (date) => {
        if (date) {
            const parsedDate = new Date(date);
            if (parsedDate.getTime()) {
                const year = parsedDate.getFullYear();
                //Month is zero based 0 = Jan so need to add 1
                const month = parsedDate.getMonth() + 1;
                const day = parsedDate.getDate().toString().padStart(2, '0');
                return `${year}-${month.toString().padStart(2, '0')}-${day}`;
            }
            return parsedDate;
        }
        return date;
    };
    const addSpacingInHeaderName = (headerName) => {
        let formattedHeaderName = '';
        for (var i = 0; i < headerName.length; i++) {
            if (i == 0) {
                formattedHeaderName += headerName[i].toUpperCase();
            }
            else if (i !== 0 && headerName[i].match(/[A-Z]/) && i != headerName.length - 1) {
                formattedHeaderName += ' ' + headerName[i];
            }
            else {
                formattedHeaderName += headerName[i];
            }
        }
        return formattedHeaderName;
    };
    const dataTypeComponentMap = (transaction, attribute) => {
        var _a;
        var _b;
        const fieldName = attribute.name;
        let fieldDataType = attribute.type.toLowerCase();
        fieldDataType = numberDataTypes.includes(fieldDataType) ? "number" : fieldDataType;
        const isAsscoiationLabel = attribute.associationLabel;
        let fieldComponentProps = null;
        switch (fieldDataType) {
            case 'number':
                fieldComponentProps = {
                    initialValue: transaction === null || transaction === void 0 ? void 0 : transaction.entityBusinessObject[fieldName],
                    type: 'number',
                    InputProps: { inputProps: { min: 0 } }
                };
                break;
            case 'datetime':
                fieldComponentProps = {
                    initialValue: removeTimeFormatDate(transaction === null || transaction === void 0 ? void 0 : transaction.entityBusinessObject[fieldName]),
                    type: "date"
                };
                break;
            case 'array':
                fieldComponentProps = {
                    options: (_a = attribute === null || attribute === void 0 ? void 0 : attribute.options) === null || _a === void 0 ? void 0 : _a.map((option) => option.id),
                    getOptionLabelFunction: (option) => getEntityName(attribute === null || attribute === void 0 ? void 0 : attribute.options, option) || option,
                    initialValue: transaction === null || transaction === void 0 ? void 0 : transaction.entityBusinessObject[fieldName]
                };
                break;
            case 'bool':
                fieldComponentProps = {
                    type: 'checkbox',
                    initialValue: false
                };
                break;
            default:
                fieldComponentProps = {
                    initialValue: transaction === null || transaction === void 0 ? void 0 : transaction.entityBusinessObject[fieldName]
                };
        }
        const formattedFieldName = addSpacingInHeaderName(fieldName);
        const formLabel = isAsscoiationLabel ? formattedFieldName + ' Name' : formattedFieldName;
        const isRequired = getRequiredFields(transaction).includes(fieldName) || attribute.required || !attribute.nullable;
        const isHidden = getHiddenFields(transaction).includes(fieldName) || attribute.isPrimaryKey || !attribute.extendedProperties.defaultVisible || (attribute === null || attribute === void 0 ? void 0 : attribute.hidden);
        let fieldComponent = (React.createElement(Field, Object.assign({ component: (_b = fieldComponentMap[fieldDataType]) !== null && _b !== void 0 ? _b : AmbientTextField, label: t(formLabel), name: fieldName, fullWidth: true, variant: "outlined", margin: "normal", disabled: attribute.isReadOnly, required: isRequired && fieldDataType != 'bool' }, fieldComponentProps)));
        return (React.createElement(React.Fragment, null, !isHidden &&
            (fieldDataType == 'bool' ? (React.createElement(FormControlLabel, { control: fieldComponent, label: formLabel })) : (fieldComponent))));
    };
    return (React.createElement(React.Fragment, null, fieldAttributes.map((attribute) => {
        return React.createElement(React.Fragment, null, dataTypeComponentMap(transaction, attribute));
    })));
};
export default EditTransactionDetailsFormFields;
//# sourceMappingURL=DynamicTransactionDomainFormFields.js.map