import * as React from 'react';
import { Box } from 'rebass';
import { OnePageAdaptableWizard, OnePageWizardSummary, } from '../../../View/Wizard/OnePageAdaptableWizard';
import { renderScopeSummary, isScopeValid } from '../../Components/NewScopeComponent';
import { PlusMinusScopeWizardSection } from './PlusMinusScopeWizardSection';
import { PlusMinusSettingsWizardSection, PlusMinusSettingsSummary, isSettingsValid, } from './PlusMinusSettingsWizardSection';
import ObjectFactory from '../../../Utilities/ObjectFactory';
import { useDispatch } from 'react-redux';
import * as PlusMinusRedux from '../../../Redux/ActionsReducers/PlusMinusRedux';
import { ObjectTagsWizardSection, renderObjectTagsSummary, } from '../../Wizard/ObjectTagsWizardSection';
export const PlusMinusWizard = (props) => {
    const [plusMinus, setPlusMinus] = React.useState(() => {
        const newPlusMinus = {
            ...(props.data ? props.data : ObjectFactory.CreateEmptyPlusMinusNudge()),
            Scope: props?.data?.Scope ?? { All: true },
            NudgeValue: props?.data?.NudgeValue ?? 20,
        };
        if (props?.popupParams?.column) {
            newPlusMinus.Scope = {
                ColumnIds: [props.popupParams.column.columnId],
            };
        }
        return newPlusMinus;
    });
    const [hasCondition, setHasCondition] = React.useState(() => {
        // true if on edit and it has a condition
        return Boolean(props?.data?.Rule);
    });
    React.useEffect(() => {
        const preparedData = { ...plusMinus };
        if (hasCondition) {
            preparedData.Rule = preparedData.Rule ?? { BooleanExpression: '' };
        }
        else {
            delete preparedData.Rule;
        }
        setPlusMinus(preparedData);
    }, [hasCondition]);
    const dispatch = useDispatch();
    const handleFinish = () => {
        if (props.data) {
            dispatch(PlusMinusRedux.PlusMinusNudgeEdit(plusMinus));
        }
        else {
            dispatch(PlusMinusRedux.PlusMinusNudgeAdd(plusMinus));
        }
        props.onFinishWizard(plusMinus);
    };
    return (React.createElement(OnePageAdaptableWizard, { defaultCurrentSectionName: props.defaultCurrentSectionName, moduleInfo: props.moduleInfo, data: plusMinus, onHide: props.onCloseWizard, onFinish: handleFinish, sections: [
            {
                title: 'Target',
                isValid: isScopeValid,
                details: 'Specify where Plus/Minus should be applied',
                renderSummary: () => renderScopeSummary(plusMinus.Scope, {
                    scopeWholeRow: 'Plus/Minus is applied for matching rows',
                    scopeColumns: 'Plus/Minus is applied in cells in selected columns',
                    scopeDataTypes: 'Plus/Minus is applied for cells with selected data type',
                }),
                render: () => (React.createElement(Box, { padding: 2 },
                    React.createElement(PlusMinusScopeWizardSection, { onChange: setPlusMinus }))),
            },
            {
                title: 'Settings',
                isValid: isSettingsValid(hasCondition),
                renderSummary: () => React.createElement(PlusMinusSettingsSummary, { hasCondition: hasCondition }),
                render: () => (React.createElement(PlusMinusSettingsWizardSection, { hasCondition: hasCondition, onConditionChange: setHasCondition, onChange: setPlusMinus })),
            },
            {
                details: 'Select Plus/Minus Tags',
                title: 'Tags',
                isVisible: (_, api) => api.internalApi.shouldDisplayTagSections(),
                render: () => (React.createElement(Box, { padding: 2 },
                    React.createElement(ObjectTagsWizardSection, { onChange: setPlusMinus }))),
                renderSummary: renderObjectTagsSummary,
            },
            '-',
            {
                render: () => {
                    return (React.createElement(Box, { padding: 2 },
                        React.createElement(OnePageWizardSummary, null)));
                },
                title: 'Summary',
            },
        ] }));
};
