"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LayoutWizard = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const LayoutRedux = tslib_1.__importStar(require("../../../Redux/ActionsReducers/LayoutRedux"));
const OnePageAdaptableWizard_1 = require("../../Wizard/OnePageAdaptableWizard");
const ObjectFactory_1 = tslib_1.__importDefault(require("../../../Utilities/ObjectFactory"));
const Helper_1 = require("../../../Utilities/Helpers/Helper");
const SettingsSection_1 = require("./sections/SettingsSection");
const rebass_1 = require("rebass");
const AdaptableContext_1 = require("../../AdaptableContext");
const react_redux_1 = require("react-redux");
const ColumnsSection_1 = require("./sections/ColumnsSection");
const PivotColumnsSection_1 = require("./sections/PivotColumnsSection");
const RowGroupingSection_1 = require("./sections/RowGroupingSection");
const AggregationsSection_1 = require("./sections/AggregationsSection");
const SortSection_1 = require("./sections/SortSection");
const FilterSection_1 = require("./sections/FilterSection");
const GridFilterSection_1 = require("./sections/GridFilterSection");
const RowSummarySection_1 = require("./sections/RowSummarySection");
const RowSummary_1 = require("../../../PredefinedConfig/Common/RowSummary");
const LayoutWizard = (props) => {
    var _a, _b;
    const dispatch = (0, react_redux_1.useDispatch)();
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const allLayouts = adaptable.api.layoutApi.getLayouts();
    const initialLayout = (_a = props.data) !== null && _a !== void 0 ? _a : (_b = props.popupParams) === null || _b === void 0 ? void 0 : _b.value;
    const [layout, setLayout] = React.useState(() => {
        var _a;
        let preparedLayout = null;
        if (initialLayout) {
            preparedLayout = (0, Helper_1.cloneObject)(initialLayout);
            if (((_a = props === null || props === void 0 ? void 0 : props.popupParams) === null || _a === void 0 ? void 0 : _a.action) === 'Clone') {
                preparedLayout.Name = '';
                delete preparedLayout.Uuid;
            }
        }
        else {
            preparedLayout = ObjectFactory_1.default.CreateEmptyLayout({ Name: '' });
        }
        if (preparedLayout.SuppressAggFuncInHeader === undefined) {
            preparedLayout.SuppressAggFuncInHeader = !!adaptable.agGridAdapter
                .getAgGridApi()
                .getGridOption('suppressAggFuncInHeader');
        }
        return preparedLayout;
    });
    const handleFinish = () => {
        var _a;
        let action = (_a = props === null || props === void 0 ? void 0 : props.popupParams) === null || _a === void 0 ? void 0 : _a.action;
        if (!action) {
            action = initialLayout ? 'Edit' : 'New';
        }
        switch (action) {
            case 'Edit':
                const currentLayout = adaptable.api.layoutApi.getCurrentLayout();
                dispatch(LayoutRedux.LayoutSave(layout));
                if (currentLayout.Uuid === layout.Uuid) {
                    dispatch(LayoutRedux.LayoutSelect(layout.Name));
                }
                break;
            case 'Clone':
                const clonedLayout = Object.assign({}, layout);
                delete clonedLayout.Uuid;
                dispatch(LayoutRedux.LayoutAdd(clonedLayout));
                dispatch(LayoutRedux.LayoutSelect(clonedLayout.Name));
                break;
            case 'New':
                dispatch(LayoutRedux.LayoutAdd(layout));
                dispatch(LayoutRedux.LayoutSelect(layout.Name));
        }
        props.onFinishWizard(layout);
    };
    const layoutSupportedFeatures = adaptable.api.layoutApi.internalApi.getLayoutSupportedFeatures();
    return (React.createElement(OnePageAdaptableWizard_1.OnePageAdaptableWizard, { titleContainerStyle: { width: 180 }, defaultCurrentSectionName: props.defaultCurrentSectionName, moduleInfo: props.moduleInfo, data: layout, onHide: props.onCloseWizard, onFinish: handleFinish, sections: [
            {
                title: 'Settings',
                details: 'Configure Layout',
                isValid: () => {
                    if (!layout.Name) {
                        return 'Layout name cannot be blank';
                    }
                    if (allLayouts.some((layoutItem) => layoutItem.Name === layout.Name && layoutItem.Uuid !== layout.Uuid)) {
                        return 'A Layout already exists with that name';
                    }
                    return true;
                },
                renderSummary: () => React.createElement(SettingsSection_1.SettingsSectionSummary, null),
                render: () => (React.createElement(rebass_1.Box, { padding: 2 },
                    React.createElement(SettingsSection_1.SettingsSection, { onChange: (newLayout) => setLayout(newLayout) }))),
            },
            {
                title: 'Columns',
                details: 'Select Columns',
                isVisible: () => !(layout === null || layout === void 0 ? void 0 : layout.EnablePivot),
                renderSummary: () => React.createElement(ColumnsSection_1.ColumnsSectionSummary, null),
                render: () => (React.createElement(rebass_1.Box, { p: 2, style: { height: '100%' } },
                    React.createElement(ColumnsSection_1.ColumnsSection, { onChange: setLayout }))),
            },
            {
                title: 'Pivot Columns',
                details: 'Select Pivot Columns',
                isVisible: () => (layout === null || layout === void 0 ? void 0 : layout.EnablePivot) && layoutSupportedFeatures.PivotColumns,
                renderSummary: () => React.createElement(PivotColumnsSection_1.PivotColumnsSectionSummary, null),
                render: () => (React.createElement(rebass_1.Box, { p: 2, style: { height: '100%' } },
                    React.createElement(PivotColumnsSection_1.PivotColumnsSection, { onChange: (newLayout) => setLayout(newLayout) }))),
            },
            {
                title: 'Row Groups',
                isVisible: () => layoutSupportedFeatures.RowGroupedColumns,
                isValid: RowSummarySection_1.areSummaryRowsValid,
                details: 'Configure Row Grouping',
                renderSummary: () => React.createElement(RowGroupingSection_1.RowGroupingSectionSummary, null),
                render: () => (React.createElement(rebass_1.Box, { p: 2, style: { height: '100%' } },
                    React.createElement(RowGroupingSection_1.RowGroupingSection, { onChange: setLayout }))),
            },
            {
                title: 'Aggregations',
                isVisible: () => layoutSupportedFeatures.AggregationColumns,
                details: 'Select Column Aggregations',
                renderSummary: () => React.createElement(AggregationsSection_1.AggregationsSectionSummary, null),
                isValid: (data) => (0, AggregationsSection_1.isAggregationsSectionValid)(data),
                render: () => (React.createElement(rebass_1.Box, { p: 2, style: { height: '100%' } },
                    React.createElement(AggregationsSection_1.AggregationsSection, { onChange: (layout) => {
                            let newLayout = (0, Helper_1.cloneObject)(layout);
                            // if we do not have an weighted avg col, we need to clear the row summary if one exists
                            if (newLayout.RowSummaries) {
                                newLayout.RowSummaries = newLayout.RowSummaries.map((rowSummary) => {
                                    return Object.assign(Object.assign({}, rowSummary), { ColumnsMap: Object.entries(rowSummary.ColumnsMap).reduce((acc, [columnId, aggFunc]) => {
                                            if (
                                            // see if it is weighted avg
                                            aggFunc === RowSummary_1.WEIGHTED_AVERAGE_AGGREATED_FUNCTION &&
                                                // see if we have a weight in the agg columns
                                                newLayout.AggregationColumns[columnId] &&
                                                (typeof newLayout.AggregationColumns[columnId] !== 'object' ||
                                                    newLayout.AggregationColumns[columnId].weightColumnId)) {
                                                // need to remove the row summary
                                                return acc;
                                            }
                                            acc[columnId] = aggFunc;
                                            return acc;
                                        }, {}) });
                                });
                            }
                            setLayout(newLayout);
                        } }))),
            },
            {
                title: 'Row Summaries',
                details: 'Configure Row Summaries',
                renderSummary: () => React.createElement(RowGroupingSection_1.RowGroupingSectionSummary, null),
                isVisible: () => layoutSupportedFeatures.RowSummaries,
                render: () => (React.createElement(rebass_1.Box, { p: 2, style: { height: '100%' } },
                    React.createElement(RowSummarySection_1.RowSummarySection, { onChange: setLayout }))),
            },
            {
                title: 'Sort',
                isVisible: () => layoutSupportedFeatures.ColumnSorts,
                renderSummary: () => React.createElement(SortSection_1.SortSectionSummary, null),
                details: 'Configure Column Sorts',
                render: () => (React.createElement(rebass_1.Box, { p: 2, style: { height: '100%' } },
                    React.createElement(SortSection_1.SortSection, { onChange: (newLayout) => setLayout(newLayout) }))),
            },
            {
                title: 'Column Filters',
                isVisible: () => layoutSupportedFeatures.ColumnFilters,
                isValid: (layout) => (0, FilterSection_1.isColumnFiltersValid)(layout),
                details: 'View Column Filters',
                renderSummary: () => React.createElement(FilterSection_1.FilterSectionSummary, null),
                render: () => (React.createElement(rebass_1.Box, { p: 2, style: { height: '100%' } },
                    React.createElement(FilterSection_1.FilterSection, { onChange: (newLayout) => setLayout(newLayout) }))),
            },
            {
                title: 'Grid Filter',
                isVisible: () => layoutSupportedFeatures.GridFilter,
                isValid: (layout) => (0, GridFilterSection_1.isGridFiltersValid)(layout, adaptable.api),
                details: 'View Grid Filter',
                renderSummary: () => React.createElement(GridFilterSection_1.GridFilterSectionSummary, null),
                render: () => (React.createElement(rebass_1.Box, { p: 2, style: { height: '100%' } },
                    React.createElement(GridFilterSection_1.GridFilterSection, { onChange: (newLayout) => setLayout(newLayout) }))),
            },
            '-',
            {
                details: 'Review your Layout',
                render: () => (React.createElement(rebass_1.Box, { padding: 2 },
                    React.createElement(OnePageAdaptableWizard_1.OnePageWizardSummary, null))),
                title: 'Summary',
            },
        ] }));
};
exports.LayoutWizard = LayoutWizard;
