import * as React from 'react';
import { useState, useCallback } from 'react';
import { OnePageAdaptableWizard, OnePageWizardSummary } from '../../Wizard/OnePageAdaptableWizard';
import { cloneObject } from '../../../Utilities/Helpers/Helper';
import { AlertScopeWizardSection } from './AlertScopeWizardSection';
import { AlertRulesWizardSection, getRuleStepDescription, renderAlertRulesSummary, } from './AlertRulesWizardSection';
import { isValidAlertRules } from './isValidAlertRules';
import { AlertBehaviourWizardSection, renderAlertBehaviourWizardSummary, } from './AlertBehaviourWizardSection';
import { ALERT_DEFAULT_MESSAGE_TYPE } from '../../../Utilities/Constants/ObjectDefaultConstants';
import { AlertMessageWizardSection, renderAlertMessageSummary } from './AlertMessageWizardSection';
import { renderScopeSummary } from './BaseAlertScopeWizardSection';
import { isScopeValid } from '../../Components/NewScopeComponent';
import ObjectFactory from '../../../Utilities/ObjectFactory';
import { useDispatch } from 'react-redux';
import * as AlertRedux from '../../../Redux/ActionsReducers/AlertRedux';
import { useAdaptable } from '../../AdaptableContext';
import { ObjectTagsWizardSection, renderObjectTagsSummary, } from '../../Wizard/ObjectTagsWizardSection';
import { AlertTypeWizardSection, renderAlertTypeSummary, isSettingsValid, } from './AlertTypeWizardSection';
import { AlertType, getAlertType } from '../Utilities/getAlertType';
import { mapAlertDefinition } from '../Utilities/mapAlertDefinition';
import { getDefaultAlertDefinition } from '../Utilities/getDefaultAlertDefinition';
import { AlertNotificationWizardSection, renderAlertNotificationSummary, } from './AlertNotificationWizardSection';
import { Box } from '../../../components/Flex';
export const AlertWizard = (props) => {
    const { api } = useAdaptable();
    const dispatch = useDispatch();
    const behaviourSpelling = api.internalApi.getCorrectEnglishVariant('Behaviour');
    const [alertDefinition, doSetAlertDefinition] = useState(() => {
        const alertDefinition = props.data
            ? cloneObject(props.data)
            : ObjectFactory.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 ?? ALERT_DEFAULT_MESSAGE_TYPE;
        return alertDefinition;
    });
    const setAlertDefinition = 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] = useState(() => {
        return getAlertType(alertDefinition) ?? AlertType.DataChange;
    });
    const handleFinish = () => {
        if (props.data) {
            dispatch(AlertRedux.AlertDefinitionEdit(alertDefinition));
        }
        else {
            dispatch(AlertRedux.AlertDefinitionAdd(alertDefinition));
        }
        props.onCloseWizard();
    };
    const updateAlertDefinition = useCallback((newAlertDefinition) => {
        doSetAlertDefinition(mapAlertDefinition(api, newAlertDefinition));
    }, [api]);
    const handleAlertTypeChange = (newAlertType) => {
        setAlertType(newAlertType);
        doSetAlertDefinition(getDefaultAlertDefinition(alertDefinition, newAlertType));
    };
    return (React.createElement(OnePageAdaptableWizard, { defaultCurrentSectionName: props.defaultCurrentSectionName, moduleInfo: props.moduleInfo, modal: props.modal, data: alertDefinition, onHide: props.onCloseWizard, onFinish: handleFinish, sections: [
            {
                title: 'Name & Type',
                isValid: 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(Box, { className: "twa:p-2" },
                    React.createElement(AlertTypeWizardSection, { alertType: alertType, onAlertTypeChange: handleAlertTypeChange, onChange: setAlertDefinition }))),
                renderSummary: () => renderAlertTypeSummary(alertType),
            },
            {
                title: 'Trigger',
                isVisible: () => alertType === AlertType.DataChange || alertType == AlertType.Validation,
                details: (React.createElement(React.Fragment, null,
                    "Specify ",
                    React.createElement("b", null, "where"),
                    " data changes should trigger the Alert")),
                isValid: isScopeValid,
                render: () => (React.createElement(AlertScopeWizardSection, { alertType: alertType, onChange: setAlertDefinition })),
                renderSummary: renderScopeSummary,
            },
            {
                title: 'Rule',
                details: getRuleStepDescription(alertType),
                isValid: isValidAlertRules,
                render: () => (React.createElement(AlertRulesWizardSection, { alertType: alertType, onChange: updateAlertDefinition, module: "alert" })),
                renderSummary: renderAlertRulesSummary,
            },
            {
                title: 'Message',
                details: "Select the Alert's Message Type and Text",
                render: () => (React.createElement(Box, { className: "twa:p-2" },
                    React.createElement(AlertMessageWizardSection, { alertType: alertType, onChange: setAlertDefinition }))),
                renderSummary: renderAlertMessageSummary,
            },
            {
                title: 'Notification',
                details: 'Create a Notification for the Alert',
                render: () => (React.createElement(Box, { className: "twa:p-2" },
                    React.createElement(AlertNotificationWizardSection, { alertType: alertType, onChange: setAlertDefinition }))),
                renderSummary: renderAlertNotificationSummary,
            },
            {
                title: behaviourSpelling,
                details: 'Configure ' + behaviourSpelling + 's for the Alert',
                render: () => (React.createElement(Box, { className: "twa:p-2" },
                    React.createElement(AlertBehaviourWizardSection, { alertType: alertType, onChange: setAlertDefinition }))),
                renderSummary: renderAlertBehaviourWizardSummary,
            },
            {
                details: 'Select Alert Tags',
                title: 'Tags',
                isVisible: () => api.internalApi.shouldDisplayTagSections(),
                render: () => (React.createElement(Box, { className: "twa:p-2" },
                    React.createElement(ObjectTagsWizardSection, { onChange: setAlertDefinition }))),
                renderSummary: renderObjectTagsSummary,
            },
            '-',
            {
                details: 'Review your Alert',
                render: () => (React.createElement(Box, { className: "twa:p-2" },
                    React.createElement(OnePageWizardSummary, null))),
                title: 'Summary',
            },
        ] }));
};
