"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlusMinusWizard = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const OnePageAdaptableWizard_1 = require("../../../View/Wizard/OnePageAdaptableWizard");
const NewScopeComponent_1 = require("../../Components/NewScopeComponent");
const PlusMinusScopeWizardSection_1 = require("./PlusMinusScopeWizardSection");
const PlusMinusSettingsWizardSection_1 = require("./PlusMinusSettingsWizardSection");
const ObjectFactory_1 = tslib_1.__importDefault(require("../../../Utilities/ObjectFactory"));
const react_redux_1 = require("react-redux");
const PlusMinusRedux = tslib_1.__importStar(require("../../../Redux/ActionsReducers/PlusMinusRedux"));
const ObjectTagsWizardSection_1 = require("../../Wizard/ObjectTagsWizardSection");
const PlusMinusWizard = (props) => {
    const [plusMinus, setPlusMinus] = React.useState(() => {
        const newPlusMinus = {
            ...(props.data ? props.data : ObjectFactory_1.default.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 = (0, react_redux_1.useDispatch)();
    const handleFinish = () => {
        if (props.data) {
            dispatch(PlusMinusRedux.PlusMinusNudgeEdit(plusMinus));
        }
        else {
            dispatch(PlusMinusRedux.PlusMinusNudgeAdd(plusMinus));
        }
        props.onFinishWizard(plusMinus);
    };
    return (React.createElement(OnePageAdaptableWizard_1.OnePageAdaptableWizard, { defaultCurrentSectionName: props.defaultCurrentSectionName, moduleInfo: props.moduleInfo, data: plusMinus, onHide: props.onCloseWizard, onFinish: handleFinish, sections: [
            {
                title: 'Target',
                isValid: NewScopeComponent_1.isScopeValid,
                details: 'Specify where Plus/Minus should be applied',
                renderSummary: () => (0, NewScopeComponent_1.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(rebass_1.Box, { padding: 2 },
                    React.createElement(PlusMinusScopeWizardSection_1.PlusMinusScopeWizardSection, { onChange: setPlusMinus }))),
            },
            {
                title: 'Settings',
                isValid: (0, PlusMinusSettingsWizardSection_1.isSettingsValid)(hasCondition),
                renderSummary: () => React.createElement(PlusMinusSettingsWizardSection_1.PlusMinusSettingsSummary, { hasCondition: hasCondition }),
                render: () => (React.createElement(PlusMinusSettingsWizardSection_1.PlusMinusSettingsWizardSection, { hasCondition: hasCondition, onConditionChange: setHasCondition, onChange: setPlusMinus })),
            },
            {
                details: 'Select Plus/Minus Tags',
                title: 'Tags',
                isVisible: (_, api) => api.internalApi.shouldDisplayTagSections(),
                render: () => (React.createElement(rebass_1.Box, { padding: 2 },
                    React.createElement(ObjectTagsWizardSection_1.ObjectTagsWizardSection, { onChange: setPlusMinus }))),
                renderSummary: ObjectTagsWizardSection_1.renderObjectTagsSummary,
            },
            '-',
            {
                render: () => {
                    return (React.createElement(rebass_1.Box, { padding: 2 },
                        React.createElement(OnePageAdaptableWizard_1.OnePageWizardSummary, null)));
                },
                title: 'Summary',
            },
        ] }));
};
exports.PlusMinusWizard = PlusMinusWizard;
