"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StyledColumnWizardColumnSection = exports.isValidStyledColumnColumn = exports.renderStyledColumnColumnSummary = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const Tabs_1 = require("../../../components/Tabs");
const Tag_1 = require("../../../components/Tag");
const ColumnSelector_1 = require("../../Components/ColumnSelector");
const OnePageAdaptableWizard_1 = require("../../Wizard/OnePageAdaptableWizard");
const renderStyledColumnColumnSummary = (data) => {
    const { api } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    return (React.createElement(rebass_1.Text, { pr: 2, py: 2 },
        "Styled Column column:",
        ' ',
        React.createElement(Tag_1.Tag, null, data.ColumnId
            ? api.columnApi.getFriendlyNameForColumnId(data.ColumnId)
            : 'No Column Selected')));
};
exports.renderStyledColumnColumnSummary = renderStyledColumnColumnSummary;
const isValidStyledColumnColumn = (data) => {
    if (!data.ColumnId) {
        return 'You have to select a Column for Styled Column';
    }
    return true;
};
exports.isValidStyledColumnColumn = isValidStyledColumnColumn;
const StyledColumnWizardColumnSection = (props) => {
    const { data, api } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    const sortableCols = React.useMemo(() => {
        let sortableColumns = [];
        if (data.SparkLineStyle) {
            sortableColumns = api.columnApi.getNumericArrayColumns();
        }
        else if (data.BadgeStyle) {
            sortableColumns = [
                ...api.columnApi.getNumericColumns(),
                ...api.columnApi.getStringColumns(),
                ...api.columnApi.getStringArrayColumns(),
                ...api.columnApi.getNumberArrayColumns(),
            ];
        }
        else {
            sortableColumns = api.columnApi.getNumericColumns();
        }
        const styledColumns = api.styledColumnApi.getStyledColumns();
        const usedColumnIds = styledColumns
            .map((styledColumn) => {
            // filter out current styled column columnID
            if (styledColumn.Uuid === data.Uuid) {
                return null;
            }
            return styledColumn.ColumnId;
        })
            .filter(Boolean);
        return sortableColumns.filter((column) => {
            return usedColumnIds.every((usedColumnId) => {
                return column.columnId !== usedColumnId;
            });
        });
    }, [data]);
    const handleColumnsChange = (columnIds) => {
        const columnId = columnIds[0];
        const emptyRanges = api.columnScopeApi.createCellColorRangesForScope({
            ColumnIds: [columnId],
        });
        const newStyledColumn = Object.assign(Object.assign({}, data), { ColumnId: columnId });
        if (newStyledColumn.PercentBarStyle) {
            newStyledColumn.PercentBarStyle = Object.assign(Object.assign({}, newStyledColumn.PercentBarStyle), { CellRanges: emptyRanges });
        }
        if (newStyledColumn.GradientStyle) {
            newStyledColumn.GradientStyle = Object.assign(Object.assign({}, newStyledColumn.GradientStyle), { CellRanges: emptyRanges });
        }
        props.onChange(newStyledColumn);
    };
    return (React.createElement(Tabs_1.Tabs, { style: { height: '100%' } },
        React.createElement(Tabs_1.Tabs.Tab, null, "Column"),
        React.createElement(Tabs_1.Tabs.Content, null,
            React.createElement(ColumnSelector_1.NewColumnSelector, { availableColumns: sortableCols, selected: data.ColumnId ? [data.ColumnId] : [], singleSelect: true, onChange: handleColumnsChange, allowReorder: false }))));
};
exports.StyledColumnWizardColumnSection = StyledColumnWizardColumnSection;
