"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StyledColumnWizard = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_1 = require("react");
const rebass_1 = require("rebass");
const react_redux_1 = require("react-redux");
const OnePageAdaptableWizard_1 = require("../../Wizard/OnePageAdaptableWizard");
const ObjectFactory_1 = tslib_1.__importDefault(require("../../../Utilities/ObjectFactory"));
const StyledColumnRedux = tslib_1.__importStar(require("../../../Redux/ActionsReducers/StyledColumnRedux"));
const StyledColumnWizardTypeSection_1 = require("./StyledColumnWizardTypeSection");
const StyledColumnWizardColumnSection_1 = require("./StyledColumnWizardColumnSection");
const StyledColumnWizardStyleSection_1 = require("./StyledColumnWizardStyleSection");
const Helper_1 = require("../../../Utilities/Helpers/Helper");
const StyledColumnWizardSettingsSection_1 = require("./StyledColumnWizardSettingsSection");
const StyledColumnSparklineSettingsSection_1 = require("./StyledColumnSparklineSettingsSection");
const ObjectTagsWizardSection_1 = require("../../Wizard/ObjectTagsWizardSection");
const StyledColumnBadgeSection_1 = require("./StyledColumnBadgeSection");
const StyledColumnWizard = (props) => {
    var _a, _b, _c, _d, _e, _f;
    const data = (_a = props.data) !== null && _a !== void 0 ? _a : (_b = props.popupParams) === null || _b === void 0 ? void 0 : _b.value;
    const popupDefaultCurrentSectionName = (_e = (_d = (_c = props === null || props === void 0 ? void 0 : props.popupParams) === null || _c === void 0 ? void 0 : _c.config) === null || _d === void 0 ? void 0 : _d.defaultCurrentSectionName) !== null && _e !== void 0 ? _e : undefined;
    const defaultCurrentSectionName = (_f = props.defaultCurrentSectionName) !== null && _f !== void 0 ? _f : popupDefaultCurrentSectionName;
    const [styledColumn, setStyledColumn] = (0, react_1.useState)(() => {
        var _a, _b;
        if (data) {
            return (0, Helper_1.cloneObject)(data);
        }
        const newData = ObjectFactory_1.default.CreateEmptyStyledColumn();
        newData.GradientStyle = {};
        if (((_a = props.popupParams) === null || _a === void 0 ? void 0 : _a.column) && ((_b = props.popupParams) === null || _b === void 0 ? void 0 : _b.action) === 'New') {
            newData.ColumnId = props.popupParams.column.columnId;
            if (props.popupParams.column.dataType === 'String') {
                newData.BadgeStyle = {
                    Badges: [ObjectFactory_1.default.CreateDefaultStyledColumnBadge()],
                };
                delete newData.GradientStyle;
            }
            else if (props.popupParams.column.dataType.includes('Array')) {
                newData.SparkLineStyle = {};
                delete newData.GradientStyle;
            }
        }
        return newData;
    });
    const dispatch = (0, react_redux_1.useDispatch)();
    const handleFinish = () => {
        var _a;
        if (((_a = props === null || props === void 0 ? void 0 : props.popupParams) === null || _a === void 0 ? void 0 : _a.action) === 'New' || !data) {
            dispatch(StyledColumnRedux.StyledColumnAdd(styledColumn));
        }
        else {
            dispatch(StyledColumnRedux.StyledColumnEdit(styledColumn));
        }
        props.onFinishWizard(styledColumn);
    };
    const AdditionalSettingSection = {
        details: 'Apply Additional Settings',
        renderSummary: StyledColumnWizardSettingsSection_1.renderStyledColumnWizardSettingsSummary,
        render: () => {
            return (React.createElement(rebass_1.Box, { padding: 2 },
                React.createElement(StyledColumnWizardSettingsSection_1.StyledColumnWizardSettingsSection, { onChange: setStyledColumn })));
        },
        title: 'Settings',
    };
    const specificSteps = [];
    if (styledColumn.SparkLineStyle) {
        specificSteps.push({
            title: 'Settings',
            render: () => (React.createElement(rebass_1.Box, { p: 2 },
                React.createElement(StyledColumnSparklineSettingsSection_1.StyledColumnSparklineSettingsSection, { onChange: setStyledColumn }))),
        });
    }
    else if (styledColumn.GradientStyle || styledColumn.PercentBarStyle) {
        specificSteps.push({
            details: 'Create the Style to apply',
            renderSummary: StyledColumnWizardStyleSection_1.renderStyledColumnStyleSummary,
            render: () => {
                return (React.createElement(rebass_1.Box, { padding: 2 },
                    React.createElement(StyledColumnWizardStyleSection_1.StyledColumnWizardStyleSection, { onChange: setStyledColumn })));
            },
            title: 'Style',
            isValid: () => {
                var _a, _b, _c;
                const columnComparison = (_b = (_a = styledColumn.GradientStyle) === null || _a === void 0 ? void 0 : _a.ColumnComparison) !== null && _b !== void 0 ? _b : (_c = styledColumn.PercentBarStyle) === null || _c === void 0 ? void 0 : _c.ColumnComparison;
                return columnComparison
                    ? columnComparison.MaxValue == undefined && columnComparison.MinValue == undefined
                        ? 'Define Min and Max Values for the Column Comparison'
                        : columnComparison.MaxValue == undefined
                            ? 'Define a Max Value for the Column Comparison'
                            : columnComparison.MinValue == undefined
                                ? 'Define a Min Value for the Column Comparison'
                                : true
                    : true;
            },
        }
        // Only available for Badge
        // AdditionalSettingSection
        );
    }
    else if (styledColumn.BadgeStyle) {
        specificSteps.push({
            title: 'Badges',
            details: 'Create Badge Styles',
            isValid: () => {
                var _a;
                if (((_a = styledColumn.BadgeStyle.Badges) === null || _a === void 0 ? void 0 : _a.length) === 0) {
                    return 'Define at least one Badge';
                }
                if (styledColumn.BadgeStyle.Badges.filter((badge) => !badge.Predicate).length > 1) {
                    return 'Only one Badge can have no Predicate';
                }
                return true;
            },
            renderSummary: () => (0, StyledColumnBadgeSection_1.renderBadgeSummary)(styledColumn),
            render: () => (React.createElement(rebass_1.Box, { p: 2 },
                React.createElement(StyledColumnBadgeSection_1.StyledColumnBadgeSection, { onChange: setStyledColumn }))),
        });
        specificSteps.push(AdditionalSettingSection);
    }
    return (React.createElement(OnePageAdaptableWizard_1.OnePageAdaptableWizard, { defaultCurrentSectionName: defaultCurrentSectionName, moduleInfo: props.moduleInfo, data: styledColumn, onFinish: handleFinish, onHide: props.onCloseWizard, sections: [
            {
                details: 'Select a Styled Column type',
                renderSummary: StyledColumnWizardTypeSection_1.renderStyledColumnTypeSummary,
                render: () => {
                    return React.createElement(StyledColumnWizardTypeSection_1.StyledColumnWizardTypeSection, { onChange: setStyledColumn });
                },
                title: 'Type',
            },
            {
                isValid: StyledColumnWizardColumnSection_1.isValidStyledColumnColumn,
                renderSummary: StyledColumnWizardColumnSection_1.renderStyledColumnColumnSummary,
                details: 'Select the Column where the Style will be applied',
                render: () => {
                    return (React.createElement(rebass_1.Box, { padding: 2, style: { height: '100%' } },
                        React.createElement(StyledColumnWizardColumnSection_1.StyledColumnWizardColumnSection, { isNew: props.isNew, onChange: setStyledColumn })));
                },
                title: 'Column',
            },
            ...specificSteps,
            {
                details: 'Select Format Column Tags',
                title: 'Tags',
                isVisible: (_, api) => api.internalApi.shouldDisplayTagSections(),
                render: () => (React.createElement(rebass_1.Box, { padding: 2 },
                    React.createElement(ObjectTagsWizardSection_1.ObjectTagsWizardSection, { onChange: setStyledColumn }))),
                renderSummary: ObjectTagsWizardSection_1.renderObjectTagsSummary,
            },
            '-',
            {
                title: 'Summary',
                details: 'Review your Styled Column',
                render: () => {
                    return (React.createElement(rebass_1.Box, { padding: 2 },
                        React.createElement(OnePageAdaptableWizard_1.OnePageWizardSummary, null)));
                },
            },
        ] }));
};
exports.StyledColumnWizard = StyledColumnWizard;
