"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomSortValuesWizardSection = exports.renderCustomSortValuesSummary = exports.isValidCustomSortOrder = 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 Loader_1 = require("../../../components/Loader");
const Tabs_1 = require("../../../components/Tabs");
const join_1 = tslib_1.__importDefault(require("../../../components/utils/join"));
const runIfNotResolvedIn_1 = require("../../../Utilities/runIfNotResolvedIn");
const ValueSelector_1 = require("../../Components/ValueSelector");
const OnePageAdaptableWizard_1 = require("../../Wizard/OnePageAdaptableWizard");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../.././Utilities/Extensions/ArrayExtensions"));
const DateHelper_1 = require("../../../Utilities/Helpers/DateHelper");
const isValidCustomSortOrder = (data) => {
    if (!data.SortedValues || !data.SortedValues.length) {
        return 'You have to specify at least one value for Custom Sort order';
    }
    return true;
};
exports.isValidCustomSortOrder = isValidCustomSortOrder;
const renderCustomSortValuesSummary = (data) => {
    return data.SortedValues.length ? (React.createElement(rebass_1.Text, { fontSize: 2 },
        React.createElement(ValueSelector_1.ValueOptionsTags, { options: data.SortedValues, value: data.SortedValues, toIdentifier: (x) => `${x}`, toLabel: (x) => `${x}`, allowWrap: true, readOnly: true }))) : null;
};
exports.renderCustomSortValuesSummary = renderCustomSortValuesSummary;
const CustomSortValuesWizardSection = (props) => {
    const { data, api } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    const [distinctValues, setDistinctValues] = (0, react_1.useState)([]);
    const [isDistinctValuesLoading, setIsDistinctValuesLoading] = (0, react_1.useState)(false);
    const columnDataType = api.columnApi.getColumnDataTypeForColumnId(data.ColumnId);
    (0, react_1.useEffect)(() => {
        let isMounted = true;
        (async () => {
            // for sync and very fast resolving promises does not make sense to show a loader
            setDistinctValues([]);
            const newValues = (await (0, runIfNotResolvedIn_1.runIfNotResolvedIn)(api.gridApi.internalApi.getDistinctValuesForColumn(data.ColumnId), () => isMounted && setIsDistinctValuesLoading(true))) ?? [];
            if (!isMounted) {
                return;
            }
            setIsDistinctValuesLoading(false);
            setDistinctValues(newValues);
        })();
        return () => {
            isMounted = false;
        };
    }, [data.ColumnId]);
    const toIdentifier = (optionItem) => {
        return api.customSortApi.internalApi.getCustomSortValue(optionItem, columnDataType);
    };
    const toLabel = (optionItem) => {
        return `${optionItem}`;
    };
    const sortedValuesMap = (0, react_1.useMemo)(() => {
        if (!data.SortedValues || !data.SortedValues.length) {
            return new Map();
        }
        return data.SortedValues.reduce((acc, val) => {
            acc.set(val, true);
            return acc;
        }, new Map());
    }, [data.SortedValues]);
    const allowReorder = React.useCallback((option) => {
        return sortedValuesMap.has(toIdentifier(option));
    }, [sortedValuesMap]);
    const getOptionValues = (gridCells) => {
        return gridCells.map((gridCell) => {
            return columnDataType === 'date' ? (0, DateHelper_1.parseToISO)(gridCell.rawValue) : gridCell.normalisedValue;
        });
    };
    const options = (0, react_1.useMemo)(() => {
        return ArrayExtensions_1.default.sortArrayWithOrder(getOptionValues(distinctValues), data.SortedValues, {
            sortUnorderedItems: false,
        });
    }, [allowReorder, distinctValues, data.SortedValues]);
    const baseClassName = 'ab-CustomSortWizard__SortOrder';
    const className = (0, join_1.default)(baseClassName, isDistinctValuesLoading && `${baseClassName}--loading`);
    return (React.createElement(Tabs_1.Tabs, { style: { height: '100%' }, className: className },
        React.createElement(Tabs_1.Tabs.Tab, null, "Sort Order"),
        React.createElement(Tabs_1.Tabs.Content, null,
            isDistinctValuesLoading && React.createElement(Loader_1.Loader, null, "Loading"),
            React.createElement(ValueSelector_1.ValueSelector, { toIdentifier: toIdentifier, toLabel: toLabel, options: options, value: data.SortedValues, allowReorder: allowReorder, xSelectedLabel: () => {
                    return `Your Sort Order:`;
                }, onChange: (SortedValues) => {
                    props.onChange({
                        ...data,
                        SortedValues,
                    });
                } }))));
};
exports.CustomSortValuesWizardSection = CustomSortValuesWizardSection;
