"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomSortColumnWizardSection = exports.isValidCustomSortColumn = exports.renderCustomSortColumn = 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 renderCustomSortColumn = (data) => {
    const { api } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    return (React.createElement(rebass_1.Text, { pr: 2, py: 2, fontSize: 2 },
        "Custom sort column: ",
        React.createElement(Tag_1.Tag, null, api.columnApi.getFriendlyNameForColumnId(data.ColumnId))));
};
exports.renderCustomSortColumn = renderCustomSortColumn;
const isValidCustomSortColumn = (data) => {
    if (!data.ColumnId) {
        return 'You have to select a Column for Custom Sort';
    }
    return true;
};
exports.isValidCustomSortColumn = isValidCustomSortColumn;
const CustomSortColumnWizardSection = (props) => {
    const { data, api } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    const sortableCols = React.useMemo(() => {
        const sortableColumns = api.columnApi.getSortableColumns();
        const nonComparerSortableColumns = sortableColumns.filter((c) => {
            return api.customSortApi.internalApi.columnHasCustomSortComparer(c.columnId);
        });
        const customSorts = api.customSortApi.getCustomSorts();
        const columnSortComparers = api.optionsApi.getCustomSortOptions().customSortComparers || [];
        const usedColumnIds = [
            ...customSorts.map((customSort) => customSort.ColumnId),
            ...columnSortComparers.map((comparer) => api.columnScopeApi.getColumnIdsInScope(comparer.scope)),
        ];
        // filter out used colum ids, but include the current one
        return sortableColumns.filter((column) => {
            if (api.customSortApi.internalApi.columnHasCustomSortComparer(column.columnId)) {
                return false;
            }
            if (!props.isNew && column.columnId === data?.ColumnId) {
                return true;
            }
            return usedColumnIds.every((usedColumnId) => {
                return column.columnId !== usedColumnId;
            });
        });
    }, []);
    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: (ids) => {
                    props.onChange({
                        ...data,
                        SortedValues: [],
                        ColumnId: ids[0],
                    });
                }, allowReorder: false }))));
};
exports.CustomSortColumnWizardSection = CustomSortColumnWizardSection;
