"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomSortApiImpl = void 0;
const tslib_1 = require("tslib");
const ModuleConstants = tslib_1.__importStar(require("../../Utilities/Constants/ModuleConstants"));
const CustomSortRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/CustomSortRedux"));
const ApiBase_1 = require("./ApiBase");
const CustomSortInternalApi_1 = require("../Internal/CustomSortInternalApi");
class CustomSortApiImpl extends ApiBase_1.ApiBase {
    internalApi;
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new CustomSortInternalApi_1.CustomSortInternalApi(_adaptable);
    }
    getCustomSortState() {
        return this.getAdaptableState().CustomSort;
    }
    getCustomSorts(config) {
        return (this.handleLayoutExtensionObjects(this.getCustomSortState().CustomSorts, 'CustomSort', config) ?? []);
    }
    getCustomSortById(id, config) {
        return this.getCustomSorts(config).find((customSort) => customSort.Uuid === id);
    }
    getCustomSortByName(name) {
        return this.getCustomSorts().find((customSort) => customSort.Name === name);
    }
    getLiveCustomSorts() {
        let returnVals = [];
        const sortedColIds = this.getGridApi()
            .getColumnSorts()
            .map((c) => c.ColumnId);
        const activeCustomSorts = this.getActiveCustomSorts();
        if (sortedColIds == undefined || activeCustomSorts == undefined) {
            return returnVals;
        }
        sortedColIds.forEach((columnId) => {
            const customSort = activeCustomSorts.find((c) => c.ColumnId == columnId);
            if (customSort) {
                returnVals.push(customSort);
            }
        });
        return returnVals;
    }
    getLiveCustomSortComparers() {
        let returnComparers = [];
        const sortedColIds = this.getGridApi()
            .getColumnSorts()
            .map((c) => c.ColumnId);
        const sortedCols = this.getColumnApi().getColumnsWithColumnIds(sortedColIds);
        const customSortComparers = this.getCustomSortOptions().customSortComparers;
        if (sortedColIds == undefined || customSortComparers == undefined) {
            return returnComparers;
        }
        const scopeApi = this.getColumnScopeApi();
        sortedCols.forEach((column) => {
            const comparer = customSortComparers.find((acs) => scopeApi.isColumnInScope(column, acs.scope));
            if (comparer && !returnComparers.includes(comparer)) {
                returnComparers.push(comparer);
            }
        });
        return returnComparers;
    }
    getActiveCustomSorts(config) {
        return this.getCustomSorts(config).filter((customSort) => !customSort.IsSuspended);
    }
    getSuspendedCustomSorts(config) {
        return this.getCustomSorts(config).filter((customSort) => customSort.IsSuspended);
    }
    getCustomSortByColumn(column) {
        return this.getCustomSorts().find((cs) => cs.ColumnId == column);
    }
    getCustomSortForColumn(columnId) {
        return this.getCustomSorts().find((cs) => cs.ColumnId == columnId);
    }
    addCustomSort(customSort) {
        this.addUidToAdaptableObject(customSort);
        this.dispatchAction(CustomSortRedux.CustomSortAdd(customSort));
        return this.getCustomSortById(customSort.Uuid);
    }
    createCustomSort(params) {
        let customSort = { Name: params.name, ColumnId: params.columnId, SortedValues: params.values };
        return this.addCustomSort(customSort);
    }
    editCustomSortValues(columnId, values) {
        const previousCustomSort = this.getCustomSortForColumn(columnId);
        if (!previousCustomSort) {
            this.logWarn(`No custom sort defined for ${columnId}`);
            return;
        }
        const newCustomSort = {
            ...previousCustomSort,
            SortedValues: values,
        };
        this.dispatchAction(CustomSortRedux.CustomSortEdit(newCustomSort));
        return this.getCustomSortByColumn(columnId);
    }
    editCustomSort(customSort) {
        this.dispatchAction(CustomSortRedux.CustomSortEdit(customSort));
        return this.getCustomSortByColumn(customSort.Uuid);
    }
    deleteCustomSort(column) {
        let customSort = this.getCustomSortForColumn(column);
        if (customSort) {
            this.dispatchAction(CustomSortRedux.CustomSortDelete(customSort));
        }
    }
    suspendCustomSort(customSort) {
        this.dispatchAction(CustomSortRedux.CustomSortSuspend(customSort));
        return this.getCustomSortById(customSort.Uuid);
    }
    unSuspendCustomSort(customSort) {
        this.dispatchAction(CustomSortRedux.CustomSortUnSuspend(customSort));
        return this.getCustomSortById(customSort.Uuid);
    }
    suspendAllCustomSort() {
        this.dispatchAction(CustomSortRedux.CustomSortSuspendAll());
    }
    unSuspendAllCustomSort() {
        this.dispatchAction(CustomSortRedux.CustomSortUnSuspendAll());
    }
    openCustomSortSettingsPanel() {
        this.showModulePopup(ModuleConstants.CustomSortModuleId);
    }
}
exports.CustomSortApiImpl = CustomSortApiImpl;
