"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlertWizard = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_1 = require("react");
const OnePageAdaptableWizard_1 = require("../../Wizard/OnePageAdaptableWizard");
const Helper_1 = require("../../../Utilities/Helpers/Helper");
const AlertScopeWizardSection_1 = require("./AlertScopeWizardSection");
const AlertRulesWizardSection_1 = require("./AlertRulesWizardSection");
const isValidAlertRules_1 = require("./isValidAlertRules");
const AlertBehaviourWizardSection_1 = require("./AlertBehaviourWizardSection");
const ObjectDefaultConstants_1 = require("../../../Utilities/Constants/ObjectDefaultConstants");
const AlertMessageWizardSection_1 = require("./AlertMessageWizardSection");
const BaseAlertScopeWizardSection_1 = require("./BaseAlertScopeWizardSection");
const NewScopeComponent_1 = require("../../Components/NewScopeComponent");
const ObjectFactory_1 = tslib_1.__importDefault(require("../../../Utilities/ObjectFactory"));
const react_redux_1 = require("react-redux");
const AlertRedux = tslib_1.__importStar(require("../../../Redux/ActionsReducers/AlertRedux"));
const AdaptableContext_1 = require("../../AdaptableContext");
const ObjectTagsWizardSection_1 = require("../../Wizard/ObjectTagsWizardSection");
const AlertTypeWizardSection_1 = require("./AlertTypeWizardSection");
const getAlertType_1 = require("../Utilities/getAlertType");
const mapAlertDefinition_1 = require("../Utilities/mapAlertDefinition");
const getDefaultAlertDefinition_1 = require("../Utilities/getDefaultAlertDefinition");
const AlertNotificationWizardSection_1 = require("./AlertNotificationWizardSection");
const Flex_1 = require("../../../components/Flex");
const AlertWizard = (props) => {
    const { api } = (0, AdaptableContext_1.useAdaptable)();
    const dispatch = (0, react_redux_1.useDispatch)();
    const behaviourSpelling = api.internalApi.getCorrectEnglishVariant('Behaviour');
    const [alertDefinition, doSetAlertDefinition] = (0, react_1.useState)(() => {
        const alertDefinition = props.data
            ? (0, Helper_1.cloneObject)(props.data)
            : ObjectFactory_1.default.CreateEmptyAlertDefinition();
        if (!alertDefinition.Rule?.BooleanExpression &&
            !alertDefinition.Rule?.ObservableExpression &&
            !alertDefinition.Rule?.AggregatedBooleanExpression &&
            !alertDefinition.Rule?.Predicates) {
            alertDefinition.Rule = {
                BooleanExpression: '',
            };
        }
        if (props.popupParams?.column && props.popupParams?.action === 'New') {
            alertDefinition.Scope = {
                ColumnIds: [props.popupParams.column.columnId],
            };
        }
        alertDefinition.MessageType = alertDefinition.MessageType ?? ObjectDefaultConstants_1.ALERT_DEFAULT_MESSAGE_TYPE;
        return alertDefinition;
    });
    const setAlertDefinition = (0, react_1.useCallback)((data) => {
        doSetAlertDefinition(data);
    }, []);
    // Alert type is based initialy on data, but can be changed by the user
    // the default objects do not neccesary have enough information to determine the type
    const [alertType, setAlertType] = (0, react_1.useState)(() => {
        return (0, getAlertType_1.getAlertType)(alertDefinition) ?? getAlertType_1.AlertType.DataChange;
    });
    const handleFinish = () => {
        if (props.data) {
            dispatch(AlertRedux.AlertDefinitionEdit(alertDefinition));
        }
        else {
            dispatch(AlertRedux.AlertDefinitionAdd(alertDefinition));
        }
        props.onCloseWizard();
    };
    const updateAlertDefinition = (0, react_1.useCallback)((newAlertDefinition) => {
        doSetAlertDefinition((0, mapAlertDefinition_1.mapAlertDefinition)(api, newAlertDefinition));
    }, [api]);
    const handleAlertTypeChange = (newAlertType) => {
        setAlertType(newAlertType);
        doSetAlertDefinition((0, getDefaultAlertDefinition_1.getDefaultAlertDefinition)(alertDefinition, newAlertType));
    };
    return (React.createElement(OnePageAdaptableWizard_1.OnePageAdaptableWizard, { defaultCurrentSectionName: props.defaultCurrentSectionName, moduleInfo: props.moduleInfo, modal: props.modal, data: alertDefinition, onHide: props.onCloseWizard, onFinish: handleFinish, sections: [
            {
                title: 'Name & Type',
                isValid: AlertTypeWizardSection_1.isSettingsValid,
                details: (React.createElement(React.Fragment, null,
                    "Enter Name and select ",
                    React.createElement("b", null, "what"),
                    " type of change in AdapTable will cause the Alert to fire")),
                render: () => (React.createElement(Flex_1.Box, { className: "twa:p-2" },
                    React.createElement(AlertTypeWizardSection_1.AlertTypeWizardSection, { alertType: alertType, onAlertTypeChange: handleAlertTypeChange, onChange: setAlertDefinition }))),
                renderSummary: () => (0, AlertTypeWizardSection_1.renderAlertTypeSummary)(alertType),
            },
            {
                title: 'Trigger',
                isVisible: () => alertType === getAlertType_1.AlertType.DataChange || alertType == getAlertType_1.AlertType.Validation,
                details: (React.createElement(React.Fragment, null,
                    "Specify ",
                    React.createElement("b", null, "where"),
                    " data changes should trigger the Alert")),
                isValid: NewScopeComponent_1.isScopeValid,
                render: () => (React.createElement(AlertScopeWizardSection_1.AlertScopeWizardSection, { alertType: alertType, onChange: setAlertDefinition })),
                renderSummary: BaseAlertScopeWizardSection_1.renderScopeSummary,
            },
            {
                title: 'Rule',
                details: (0, AlertRulesWizardSection_1.getRuleStepDescription)(alertType),
                isValid: isValidAlertRules_1.isValidAlertRules,
                render: () => (React.createElement(AlertRulesWizardSection_1.AlertRulesWizardSection, { alertType: alertType, onChange: updateAlertDefinition, module: "alert" })),
                renderSummary: AlertRulesWizardSection_1.renderAlertRulesSummary,
            },
            {
                title: 'Message',
                details: "Select the Alert's Message Type and Text",
                render: () => (React.createElement(Flex_1.Box, { className: "twa:p-2" },
                    React.createElement(AlertMessageWizardSection_1.AlertMessageWizardSection, { alertType: alertType, onChange: setAlertDefinition }))),
                renderSummary: AlertMessageWizardSection_1.renderAlertMessageSummary,
            },
            {
                title: 'Notification',
                details: 'Create a Notification for the Alert',
                render: () => (React.createElement(Flex_1.Box, { className: "twa:p-2" },
                    React.createElement(AlertNotificationWizardSection_1.AlertNotificationWizardSection, { alertType: alertType, onChange: setAlertDefinition }))),
                renderSummary: AlertNotificationWizardSection_1.renderAlertNotificationSummary,
            },
            {
                title: behaviourSpelling,
                details: 'Configure ' + behaviourSpelling + 's for the Alert',
                render: () => (React.createElement(Flex_1.Box, { className: "twa:p-2" },
                    React.createElement(AlertBehaviourWizardSection_1.AlertBehaviourWizardSection, { alertType: alertType, onChange: setAlertDefinition }))),
                renderSummary: AlertBehaviourWizardSection_1.renderAlertBehaviourWizardSummary,
            },
            {
                details: 'Select Alert Tags',
                title: 'Tags',
                isVisible: () => api.internalApi.shouldDisplayTagSections(),
                render: () => (React.createElement(Flex_1.Box, { className: "twa:p-2" },
                    React.createElement(ObjectTagsWizardSection_1.ObjectTagsWizardSection, { onChange: setAlertDefinition }))),
                renderSummary: ObjectTagsWizardSection_1.renderObjectTagsSummary,
            },
            '-',
            {
                details: 'Review your Alert',
                render: () => (React.createElement(Flex_1.Box, { className: "twa:p-2" },
                    React.createElement(OnePageAdaptableWizard_1.OnePageWizardSummary, null))),
                title: 'Summary',
            },
        ] }));
};
exports.AlertWizard = AlertWizard;
