"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CalculatedColumnWizard = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_1 = require("react");
const Helper_1 = require("../../../Utilities/Helpers/Helper");
const OnePageAdaptableWizard_1 = require("../../Wizard/OnePageAdaptableWizard");
const CalculatedColumnExpressionWizardSection_1 = require("./CalculatedColumnExpressionWizardSection");
const CalculatedColumnDefinitionWizardSection_1 = require("./CalculatedColumnDefinitionWizardSection");
const AdaptableContext_1 = require("../../AdaptableContext");
const ObjectFactory_1 = tslib_1.__importDefault(require("../../../Utilities/ObjectFactory"));
const react_redux_1 = require("react-redux");
const CalculatedColumnRedux = tslib_1.__importStar(require("../../../Redux/ActionsReducers/CalculatedColumnRedux"));
const ObjectTagsWizardSection_1 = require("../../Wizard/ObjectTagsWizardSection");
const CalculatedColumnSettingsWizardSection_1 = require("./CalculatedColumnSettingsWizardSection");
const utils_1 = require("../utils");
const StringExtensions_1 = require("../../../Utilities/Extensions/StringExtensions");
const Tag_1 = require("../../../components/Tag");
const CalculatedColumnTypeSection_1 = require("./CalculatedColumnTypeSection");
const Flex_1 = require("../../../components/Flex");
const CalculatedColumnWizard = (props) => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const allCalculatedColumns = (0, react_redux_1.useSelector)((state) => state.CalculatedColumn.CalculatedColumns);
    const [calculatedColumn, setCalculatedColumn] = (0, react_1.useState)(() => {
        if (props.data) {
            return (0, Helper_1.cloneObject)(props.data);
        }
        if (props.popupParams?.column) {
            const calculatedColumn = allCalculatedColumns.find((calculatedColumn) => calculatedColumn.ColumnId === props.popupParams.column.columnId);
            if (calculatedColumn) {
                return (0, Helper_1.cloneObject)(calculatedColumn);
            }
        }
        return ObjectFactory_1.default.CreateEmptyCalculatedColumn(adaptable.api.gridApi.internalApi.deriveSpecialColumnSettingsFromAgGridDefaultColDef());
    });
    const dispatch = (0, react_redux_1.useDispatch)();
    const isEdit = Boolean(props.data) || props.popupParams?.action === 'Edit';
    const handleFinish = () => {
        if (isEdit) {
            dispatch(CalculatedColumnRedux.CalculatedColumnEdit(calculatedColumn));
        }
        else {
            dispatch(CalculatedColumnRedux.CalculatedColumnAdd(calculatedColumn));
        }
        props.onCloseWizard();
    };
    const [expressionType, setExpressionType] = (0, react_1.useState)(() => {
        return (0, utils_1.getExpressionType)(calculatedColumn, adaptable.api) ?? 'ScalarExpression';
    });
    const handleExpressionTypeChange = (type) => {
        setExpressionType(type);
        if (expressionType === 'ScalarExpression') {
            setCalculatedColumn({
                ...calculatedColumn,
                Query: {
                    ScalarExpression: '',
                },
            });
        }
        else {
            // Aggregated, Cumulative & Quantile are all AggregatedScalarExpression
            setCalculatedColumn({
                ...calculatedColumn,
                Query: {
                    AggregatedScalarExpression: '',
                },
            });
        }
    };
    return (React.createElement(OnePageAdaptableWizard_1.OnePageAdaptableWizard, { defaultCurrentSectionName: props.defaultCurrentSectionName, moduleInfo: props.moduleInfo, data: calculatedColumn, onHide: props.onCloseWizard, onFinish: handleFinish, sections: [
            {
                title: 'Type',
                renderSummary: () => {
                    return (React.createElement(Flex_1.Box, { className: "twa:text-2" },
                        "Expression Type: ",
                        React.createElement(Tag_1.Tag, null, (0, StringExtensions_1.Humanize)(expressionType))));
                },
                render: () => {
                    return (React.createElement(Flex_1.Box, { className: "twa:p-2" },
                        React.createElement(CalculatedColumnTypeSection_1.CalculatedColumnTypeWizardSection, { type: expressionType, onTypeChange: handleExpressionTypeChange })));
                },
            },
            {
                title: 'Details',
                details: 'Specify Calculated Column details',
                isValid: CalculatedColumnDefinitionWizardSection_1.isValidCalculatedColumnDefinition,
                renderSummary: CalculatedColumnDefinitionWizardSection_1.renderCalculatedColumnDefinitionSummary,
                render: () => {
                    return (React.createElement(Flex_1.Box, { className: "twa:p-2" },
                        React.createElement(CalculatedColumnDefinitionWizardSection_1.CalculatedColumnDefinitionWizardSection, { isEdit: isEdit, onChange: setCalculatedColumn })));
                },
            },
            {
                title: 'Expression',
                details: 'Specify the Calculated Column Expression',
                isValid: CalculatedColumnExpressionWizardSection_1.isValidCalculatedColumnExpression,
                renderSummary: CalculatedColumnExpressionWizardSection_1.renderCalculatedColumnExpressionSummary,
                render: () => {
                    return (React.createElement(Flex_1.Box, { className: "twa:p-2 twa:h-full" },
                        React.createElement(CalculatedColumnExpressionWizardSection_1.CalculatedColumnExpressionWizardSection, { expressionType: expressionType, onChange: setCalculatedColumn })));
                },
            },
            {
                title: 'Settings',
                details: 'Specify Column properties',
                isValid: CalculatedColumnSettingsWizardSection_1.isValidCalculatedColumnSettings,
                renderSummary: CalculatedColumnSettingsWizardSection_1.renderCalculatedColumnSettingsSummary,
                render: () => {
                    return (React.createElement(Flex_1.Box, { className: "twa:p-2" },
                        React.createElement(CalculatedColumnSettingsWizardSection_1.CalculatedColumnSettingsWizardSection, { isEdit: isEdit, onChange: setCalculatedColumn })));
                },
            },
            {
                details: 'Select Calculated Column Tags',
                title: 'Tags',
                isVisible: () => adaptable.api.internalApi.shouldDisplayTagSections(),
                render: () => (React.createElement(Flex_1.Box, { className: "twa:p-2" },
                    React.createElement(ObjectTagsWizardSection_1.ObjectTagsWizardSection, { onChange: setCalculatedColumn }))),
                renderSummary: ObjectTagsWizardSection_1.renderObjectTagsSummary,
            },
            '-',
            {
                render: () => {
                    return (React.createElement(Flex_1.Box, { className: "twa:p-2" },
                        React.createElement(OnePageAdaptableWizard_1.OnePageWizardSummary, null)));
                },
                title: 'Summary',
            },
        ] }));
};
exports.CalculatedColumnWizard = CalculatedColumnWizard;
