"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StyledColumnWizardStyleSection = exports.renderStyledColumnStyleSummary = exports.renderFormatColumnStyleWizardSummary = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const CheckBox_1 = require("../../../../components/CheckBox");
const ErrorBox_1 = tslib_1.__importDefault(require("../../../../components/ErrorBox"));
const FormLayout_1 = tslib_1.__importStar(require("../../../../components/FormLayout"));
const Tag_1 = require("../../../../components/Tag");
const ColorPicker_1 = require("../../../../components/ColorPicker");
const RangesComponent_1 = require("../../../Components/RangesComponent");
const StyleVisualItem_1 = require("../../../Components/StyleVisualItem");
const UIHelper_1 = require("../../../UIHelper");
const OnePageAdaptableWizard_1 = require("../../../Wizard/OnePageAdaptableWizard");
const PercentBarColumnComparisonPreview_1 = require("./Components/PercentBarColumnComparisonPreview");
const PercentBarRangesPreview_1 = require("./Components/PercentBarRangesPreview");
const DropdownButton_1 = tslib_1.__importDefault(require("../../../../components/DropdownButton"));
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../../../Utilities/Extensions/ArrayExtensions"));
const renderFormatColumnStyleWizardSummary = (data) => {
    const { api } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    return (0, exports.renderStyledColumnStyleSummary)(data, api);
};
exports.renderFormatColumnStyleWizardSummary = renderFormatColumnStyleWizardSummary;
const getRanges = (ranges, rangeValueType) => {
    let postfix = '';
    if (rangeValueType === 'Percentage') {
        postfix = '%';
    }
    return ranges.map((r, i) => (React.createElement(rebass_1.Flex, { key: i, alignItems: "center", mr: 3 },
        React.createElement(rebass_1.Box, { mr: 1 },
            r.Min,
            postfix,
            " to ",
            r.Max,
            postfix,
            ' '),
        React.createElement(StyleVisualItem_1.StyleVisualItem, { Style: {
                BackColor: r.Color,
                ForeColor: r.Color,
            } }))));
};
const getColumnComparison = (columnComparison, api) => {
    return (React.createElement(rebass_1.Flex, { key: 'cc', alignItems: "center", mr: 3 },
        React.createElement(rebass_1.Box, { mr: 1 },
            "Min Value:",
            ' ',
            isNaN(Number(columnComparison.MinValue))
                ? '[' + api.columnApi.getFriendlyNameForColumnId(String(columnComparison.MinValue)) + ']'
                : columnComparison.MinValue,
            ' ',
            ", Max Value:",
            ' ',
            isNaN(Number(columnComparison.MaxValue))
                ? '[' + api.columnApi.getFriendlyNameForColumnId(String(columnComparison.MaxValue)) + ']'
                : columnComparison.MaxValue,
            ' '),
        React.createElement(StyleVisualItem_1.StyleVisualItem, { Style: {
                BackColor: columnComparison.Color,
                ForeColor: columnComparison.Color,
            } })));
};
const renderStyledColumnStyleSummary = (data, api) => {
    if (!api) {
        const { api: adaptableApi } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
        api = adaptableApi;
    }
    if (data.GradientStyle) {
        if (data.GradientStyle.CellRanges) {
            return getRanges(data.GradientStyle.CellRanges, data.GradientStyle.RangeValueType);
        }
        if (data.GradientStyle.ColumnComparison) {
            return getColumnComparison(data.GradientStyle.ColumnComparison, api);
        }
    }
    if (data.PercentBarStyle) {
        if (data.PercentBarStyle.ColumnComparison) {
            return React.createElement(PercentBarColumnComparisonPreview_1.PercentBarColumnComparisonPreview, { percentBarStyle: data.PercentBarStyle });
        }
        if (data.PercentBarStyle.CellRanges) {
            return React.createElement(PercentBarRangesPreview_1.PercentBarPreview, { percentBarStyle: data.PercentBarStyle });
        }
    }
    return React.createElement(Tag_1.Tag, null, "No Styling Defined");
};
exports.renderStyledColumnStyleSummary = renderStyledColumnStyleSummary;
const StyledColumnWizardStyleSection = (props) => {
    const { data, api } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    const minMaxRangeValues = React.useMemo(() => {
        const columnId = data.ColumnId;
        if (!columnId) {
            return null;
        }
        const column = api.columnApi.getColumnWithColumnId(columnId);
        return {
            min: api.styledColumnApi.internalApi.getMinValueForNumericColumn(column),
            max: api.styledColumnApi.internalApi.getMaxValueForNumericColumn(column),
        };
    }, [data]);
    const scope = { ColumnIds: [data.ColumnId] }; // TODO: remove scope when format column columnStyle is removed
    // gradient
    const onUpdateGradientStyleRanges = (ranges) => {
        const gradientStyle = {
            ...data.GradientStyle,
            CellRanges: ranges,
        };
        // delete gradientStyle.ColumnComparison in case we are switching from ColumnComparison to CellRanges
        delete gradientStyle.ColumnComparison;
        props.onChange({ ...data, GradientStyle: gradientStyle });
    };
    const onUpdateGradientStyleColumnComparison = (columnComparison) => {
        const GradientStyle = {
            ColumnComparison: columnComparison,
        };
        props.onChange({ ...data, GradientStyle });
    };
    // percentbar
    const onUpdatePercentBarStyleColumnComparison = (columnComparison) => {
        const PercentBarStyle = {
            ...data.PercentBarStyle,
            ColumnComparison: columnComparison,
        };
        props.onChange({ ...data, PercentBarStyle });
    };
    const onUpdatePercentBarStyleRanges = (ranges) => {
        const PercentBarStyle = {
            ...data.PercentBarStyle,
            CellRanges: ranges,
        };
        props.onChange({ ...data, PercentBarStyle });
    };
    const onPercentBarCellTextCellValueChanged = (checked) => {
        let percentBarStyle = data?.PercentBarStyle;
        if (checked) {
            if (!percentBarStyle.CellText) {
                percentBarStyle.CellText = ['CellValue'];
            }
            else {
                percentBarStyle.CellText.push('CellValue');
            }
        }
        else {
            if (percentBarStyle.CellText && percentBarStyle.CellText.includes('PercentageValue')) {
                percentBarStyle.CellText = ['PercentageValue'];
            }
            else {
                percentBarStyle.CellText = [];
            }
        }
        props.onChange({
            ...data,
            PercentBarStyle: percentBarStyle,
        });
    };
    const onPercentBarCellTextPercentValueChanged = (checked) => {
        let percentBarStyle = data?.PercentBarStyle;
        if (checked) {
            if (!percentBarStyle.CellText) {
                percentBarStyle.CellText = ['PercentageValue'];
            }
            else {
                percentBarStyle.CellText.push('PercentageValue');
            }
        }
        else {
            if (percentBarStyle.CellText && percentBarStyle.CellText.includes('CellValue')) {
                percentBarStyle.CellText = ['CellValue'];
            }
            else {
                percentBarStyle.CellText = [];
            }
        }
        props.onChange({
            ...data,
            PercentBarStyle: percentBarStyle,
        });
    };
    const handlePercentBarCellTextPositionChange = (percentBarPosition) => {
        props.onChange({
            ...data,
            PercentBarStyle: {
                ...data.PercentBarStyle,
                CellTextPosition: percentBarPosition,
            },
        });
    };
    const onPercentBarToolTipTextCellValueChanged = (checked) => {
        let percentBarStyle = data?.PercentBarStyle;
        if (checked) {
            if (!percentBarStyle.ToolTipText) {
                percentBarStyle.ToolTipText = ['CellValue'];
            }
            else {
                percentBarStyle.ToolTipText.push('CellValue');
            }
        }
        else {
            if (percentBarStyle.ToolTipText && percentBarStyle.ToolTipText.includes('PercentageValue')) {
                percentBarStyle.ToolTipText = ['PercentageValue'];
            }
            else {
                percentBarStyle.ToolTipText = [];
            }
        }
        props.onChange({
            ...data,
            PercentBarStyle: percentBarStyle,
        });
    };
    const onPercentBarToolTipTextPercentValueChanged = (checked) => {
        let percentBarStyle = data.PercentBarStyle;
        if (checked) {
            if (!percentBarStyle.ToolTipText) {
                percentBarStyle.ToolTipText = ['PercentageValue'];
            }
            else {
                percentBarStyle.ToolTipText.push('PercentageValue');
            }
        }
        else {
            if (percentBarStyle.ToolTipText && percentBarStyle.ToolTipText.includes('CellValue')) {
                percentBarStyle.ToolTipText = ['CellValue'];
            }
            else {
                percentBarStyle.ToolTipText = [];
            }
        }
        props.onChange({
            ...data,
            PercentBarStyle: percentBarStyle,
        });
    };
    const onUsePercentStyleColorCheckChanged = (checked) => {
        let percentBarStyle = data?.PercentBarStyle;
        percentBarStyle.BackColor = checked ? (0, UIHelper_1.getHexForName)(UIHelper_1.GRAY) : null;
        props.onChange({
            ...data,
            PercentBarStyle: percentBarStyle,
        });
    };
    const onForeColorSelectChange = (color) => {
        let percentBarStyle = data.PercentBarStyle;
        percentBarStyle.BackColor = color;
        props.onChange({
            ...data,
            PercentBarStyle: percentBarStyle,
        });
    };
    const handleRangeValueTypeChange = React.useCallback((rangeValueType) => {
        const ranges = rangeValueType === 'Number'
            ? api.columnScopeApi.createCellColorRangesForScope(scope)
            : [
                {
                    Min: 0,
                    Max: 100,
                    Color: (0, UIHelper_1.getHexForName)(UIHelper_1.GRAY),
                },
            ];
        let newStyledColumn = data;
        const commonStyle = {
            CellRanges: ranges,
            RangeValueType: rangeValueType,
        };
        if ('GradientStyle' in newStyledColumn) {
            newStyledColumn = {
                ...newStyledColumn,
                GradientStyle: {
                    ...newStyledColumn.GradientStyle,
                    ...commonStyle,
                },
            };
        }
        else {
            newStyledColumn = {
                ...newStyledColumn,
                PercentBarStyle: {
                    ...newStyledColumn.PercentBarStyle,
                    ...commonStyle,
                },
            };
        }
        props.onChange(newStyledColumn);
    }, [data]);
    const disabled = !data.ColumnId;
    return (React.createElement(rebass_1.Box, null,
        data?.GradientStyle && (React.createElement(RangesComponent_1.RangesComponent, { disabled: !data.ColumnId, minMaxRangeValues: minMaxRangeValues, api: api, scope: scope, showRangeDirection: true, ranges: data.GradientStyle?.CellRanges, rangeValueType: data.GradientStyle?.RangeValueType, onRangeValueTypeChange: handleRangeValueTypeChange, columnComparison: data?.GradientStyle?.ColumnComparison, updateRanges: (ranges) => onUpdateGradientStyleRanges(ranges), updateColumnComparison: (columnComparison) => onUpdateGradientStyleColumnComparison(columnComparison) })),
        data.PercentBarStyle && (React.createElement(React.Fragment, null,
            React.createElement(RangesComponent_1.RangesComponent, { disabled: disabled, minMaxRangeValues: minMaxRangeValues, api: api, scope: scope, showRangeDirection: false, ranges: data.PercentBarStyle?.CellRanges, rangeValueType: data.PercentBarStyle?.RangeValueType, onRangeValueTypeChange: handleRangeValueTypeChange, columnComparison: data?.PercentBarStyle?.ColumnComparison, updateRanges: (ranges) => onUpdatePercentBarStyleRanges(ranges), updateColumnComparison: (columnComparison) => onUpdatePercentBarStyleColumnComparison(columnComparison) }),
            React.createElement(FormLayout_1.default, { marginLeft: 2, marginTop: 2 },
                React.createElement(FormLayout_1.FormRow, { label: "Cell Display:" },
                    React.createElement(CheckBox_1.CheckBox, { disabled: disabled, marginLeft: 2, checked: data.PercentBarStyle?.CellText?.includes('CellValue'), onChange: (checked) => onPercentBarCellTextCellValueChanged(checked) }, "Cell Value"),
                    ' ',
                    React.createElement(CheckBox_1.CheckBox, { disabled: disabled, marginLeft: 3, checked: data?.PercentBarStyle?.CellText?.includes('PercentageValue'), onChange: (checked) => onPercentBarCellTextPercentValueChanged(checked) },
                        "Percent Value",
                        ' '),
                    ' '),
                React.createElement(FormLayout_1.FormRow, { label: "Cell Display Position:" },
                    React.createElement(DropdownButton_1.default, { disabled: ArrayExtensions_1.default.IsNullOrEmpty(data.PercentBarStyle.CellText) || disabled, columns: ['label'], items: [
                            {
                                label: 'Above Bar',
                                onClick: () => handlePercentBarCellTextPositionChange('Above'),
                            },
                            {
                                label: 'Below Bar',
                                onClick: () => handlePercentBarCellTextPositionChange('Below'),
                            },
                            {
                                label: 'Merged',
                                onClick: () => handlePercentBarCellTextPositionChange('Merged'),
                            },
                        ] }, data.PercentBarStyle?.CellTextPosition ?? 'Below')),
                React.createElement(FormLayout_1.FormRow, { label: "ToolTip Display:" },
                    React.createElement(CheckBox_1.CheckBox, { disabled: disabled, marginLeft: 2, checked: data?.PercentBarStyle?.ToolTipText?.includes('CellValue'), onChange: (checked) => onPercentBarToolTipTextCellValueChanged(checked) }, "Cell Value"),
                    ' ',
                    React.createElement(CheckBox_1.CheckBox, { disabled: disabled, marginLeft: 3, checked: data?.PercentBarStyle?.ToolTipText?.includes('PercentageValue'), onChange: (checked) => onPercentBarToolTipTextPercentValueChanged(checked) },
                        "Percent Value",
                        ' '),
                    ' '),
                React.createElement(FormLayout_1.FormRow, { label: `Back ${api.internalApi.getCorrectEnglishVariant('Colour')}` },
                    React.createElement(rebass_1.Flex, { alignItems: "center", marginLeft: 2 },
                        React.createElement(CheckBox_1.CheckBox, { disabled: disabled, checked: !!data.PercentBarStyle?.BackColor, onChange: (checked) => onUsePercentStyleColorCheckChanged(checked), mr: 2 }),
                        data.PercentBarStyle?.BackColor !== undefined && (React.createElement(ColorPicker_1.ColorPicker, { disabled: disabled, api: api, value: data.PercentBarStyle?.BackColor, onChange: (x) => onForeColorSelectChange(x) }))))))),
        !data.ColumnId && React.createElement(ErrorBox_1.default, { mt: 2 }, "You need to select a column before styling.")));
};
exports.StyledColumnWizardStyleSection = StyledColumnWizardStyleSection;
