import React, { forwardRef, useCallback, useImperativeHandle, useMemo, useRef } from 'react';
import { arrayMutators, Form } from '../../../../utilities/form';
import UdpTransactionTypeTransactionActionFormFields from './UdpTransactionTypeTransactionActionFormFields';
import { UdpTransactionActionEnums } from '../../../../enums';
const baseActionsList = [
    UdpTransactionActionEnums.Send_Email,
    UdpTransactionActionEnums.Create_Reminder,
    UdpTransactionActionEnums.Custom_Client_Action
];
const UdpTransactionTypeTransactionActionForm = (props, ref) => {
    const { onSubmit, initialData, transactionActionOptions, catalogObjectProperties, actionApiOptions, pageOptions, transactionTypeStatusOptions } = props;
    const handleSubmitRef = useRef();
    const formInitialValues = useMemo(() => {
        var _a, _b, _c, _d;
        let formattedData;
        if (!initialData)
            return;
        formattedData = Object.assign(Object.assign({}, initialData), { transactionAction: transactionActionOptions === null || transactionActionOptions === void 0 ? void 0 : transactionActionOptions.find(transactionAction => (transactionAction === null || transactionAction === void 0 ? void 0 : transactionAction.transactionActionId) === (initialData === null || initialData === void 0 ? void 0 : initialData.transactionActionId)), toStatus: transactionTypeStatusOptions === null || transactionTypeStatusOptions === void 0 ? void 0 : transactionTypeStatusOptions.find(tts => {
                var _a;
                return (tts === null || tts === void 0 ? void 0 : tts.transactionTypeStatusId) === ((_a = initialData === null || initialData === void 0 ? void 0 : initialData.parameters) === null || _a === void 0 ? void 0 : _a.toStatus);
            }), mandatoryFields: (_a = initialData === null || initialData === void 0 ? void 0 : initialData.parameters) === null || _a === void 0 ? void 0 : _a.mandatoryFields, hiddenFields: (_b = initialData === null || initialData === void 0 ? void 0 : initialData.parameters) === null || _b === void 0 ? void 0 : _b.hiddenFields, actionApiId: actionApiOptions === null || actionApiOptions === void 0 ? void 0 : actionApiOptions.find(actionApi => {
                var _a;
                return (actionApi === null || actionApi === void 0 ? void 0 : actionApi.actionApiId) === ((_a = initialData === null || initialData === void 0 ? void 0 : initialData.parameters) === null || _a === void 0 ? void 0 : _a.actionApiId);
            }), pageId: pageOptions === null || pageOptions === void 0 ? void 0 : pageOptions.find(page => {
                var _a;
                return (page === null || page === void 0 ? void 0 : page.pageId) === ((_a = initialData === null || initialData === void 0 ? void 0 : initialData.parameters) === null || _a === void 0 ? void 0 : _a.pageId);
            }), domainDataToUpdate: ((_c = initialData === null || initialData === void 0 ? void 0 : initialData.parameters) === null || _c === void 0 ? void 0 : _c.domainDataToUpdate) ? Object.entries((_d = initialData === null || initialData === void 0 ? void 0 : initialData.parameters) === null || _d === void 0 ? void 0 : _d.domainDataToUpdate) : [[]] });
        return formattedData;
    }, [
        initialData,
        transactionActionOptions,
        transactionTypeStatusOptions,
        actionApiOptions,
        pageOptions
    ]);
    useImperativeHandle(ref, () => ({
        submit: () => {
            // eslint-disable-next-line no-unused-expressions
            handleSubmitRef === null || handleSubmitRef === void 0 ? void 0 : handleSubmitRef.current();
        }
    }));
    const handleSubmit = useCallback(formData => {
        onSubmit(formData, initialData);
    }, [initialData, onSubmit]);
    return (React.createElement(Form, { onSubmit: handleSubmit, mutators: Object.assign({}, arrayMutators), initialValues: Object.assign({}, formInitialValues), render: ({ handleSubmit, form }) => {
            var _a, _b, _c, _d;
            var _e;
            handleSubmitRef.current = handleSubmit;
            const push = (_e = (_a = form === null || form === void 0 ? void 0 : form.mutators) === null || _a === void 0 ? void 0 : _a.push) !== null && _e !== void 0 ? _e : (() => { });
            const transactionActionName = (_d = (_c = (_b = form === null || form === void 0 ? void 0 : form.getState()) === null || _b === void 0 ? void 0 : _b.values) === null || _c === void 0 ? void 0 : _c.transactionAction) === null || _d === void 0 ? void 0 : _d.name;
            const showMandatoryFields = transactionActionName === UdpTransactionActionEnums.Set_Mandatory_Fields;
            const showHiddenFields = transactionActionName === UdpTransactionActionEnums.Set_Fields_Hidden;
            const showToStatusField = [
                ...baseActionsList,
                UdpTransactionActionEnums.Update_Status,
            ].includes(transactionActionName);
            const showActionApiField = [...baseActionsList, UdpTransactionActionEnums.Product_Action].includes(transactionActionName);
            const showPageField = [...baseActionsList, UdpTransactionActionEnums.Product_Action].includes(transactionActionName);
            const showDomainDataToUpdateFields = [...baseActionsList, UdpTransactionActionEnums.Update_Domain].includes(transactionActionName);
            return (React.createElement(UdpTransactionTypeTransactionActionFormFields, { transactionActionOptions: transactionActionOptions, catalogObjectProperties: catalogObjectProperties, actionApiOptions: actionApiOptions, pageOptions: pageOptions, transactionTypeStatusOptions: transactionTypeStatusOptions, showMandatoryFields: showMandatoryFields, showHiddenFields: showHiddenFields, showToStatusField: showToStatusField, showActionApiField: showActionApiField, showPageField: showPageField, showDomainDataToUpdateFields: showDomainDataToUpdateFields, pushToFieldArray: push }));
        } }));
};
export default forwardRef(UdpTransactionTypeTransactionActionForm);
//# sourceMappingURL=UdpTransactionTypeTransactionActionForm.js.map