"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get applyColumnSortToQueryFromPlugin () {
        return applyColumnSortToQueryFromPlugin;
    },
    get applyColumnSortingToQueryParameters () {
        return applyColumnSortingToQueryParameters;
    },
    get handleBeforeColumnSortForServer () {
        return handleBeforeColumnSortForServer;
    },
    get normalizeSortInFetchParams () {
        return normalizeSortInFetchParams;
    },
    get normalizeSortToQueryFormat () {
        return normalizeSortToQueryFormat;
    },
    get querySortToPluginSort () {
        return querySortToPluginSort;
    },
    get sortingPayloadToSort () {
        return sortingPayloadToSort;
    },
    get syncColumnSortingFromQuerySort () {
        return syncColumnSortingFromQuerySort;
    }
});
const COLUMN_SORTING_PLUGIN_KEY = 'columnSorting';
function normalizeSortToQueryFormat(sort, colToProp) {
    if (sort === null || sort === undefined || typeof sort !== 'object') {
        return null;
    }
    if (typeof sort.column === 'number' && typeof colToProp === 'function') {
        return {
            prop: colToProp(sort.column),
            order: sort.sortOrder
        };
    }
    const order = sort.order ?? sort.sortOrder;
    if (typeof sort.prop === 'string' && (order === 'asc' || order === 'desc')) {
        return {
            prop: sort.prop,
            order
        };
    }
    return null;
}
function querySortToPluginSort(sort, propToCol) {
    if (sort === null || sort === undefined || typeof sort !== 'object' || typeof sort.prop !== 'string') {
        return null;
    }
    const order = sort.order ?? sort.sortOrder;
    if (order !== 'asc' && order !== 'desc') {
        return null;
    }
    if (typeof propToCol !== 'function') {
        return null;
    }
    const column = propToCol(sort.prop);
    if (typeof column !== 'number') {
        return null;
    }
    return {
        column,
        sortOrder: order
    };
}
function applyColumnSortingToQueryParameters(columnSortingPlugin, queryParameters, colToProp) {
    if (!columnSortingPlugin?.enabled || typeof colToProp !== 'function') {
        return;
    }
    const sortConfig = columnSortingPlugin.getSortConfig();
    const first = Array.isArray(sortConfig) && sortConfig.length > 0 ? sortConfig[0] : sortConfig;
    if (first && typeof first === 'object' && typeof first.column === 'number') {
        queryParameters.sort = {
            prop: colToProp(first.column),
            order: first.sortOrder
        };
    } else {
        queryParameters.sort = null;
    }
}
function syncColumnSortingFromQuerySort(columnSortingPlugin, sort) {
    if (!columnSortingPlugin?.enabled) {
        return;
    }
    const sortConfig = sort && typeof sort === 'object' && 'column' in sort ? sort : [];
    columnSortingPlugin.setSortConfig(sortConfig);
}
function applyColumnSortToQueryFromPlugin(hot, queryParameters) {
    applyColumnSortingToQueryParameters(hot.getPlugin(COLUMN_SORTING_PLUGIN_KEY), queryParameters, (column)=>hot.colToProp(column));
}
function normalizeSortInFetchParams(params, hot) {
    params.sort = normalizeSortToQueryFormat(params.sort, (col)=>hot.colToProp(col));
}
function sortingPayloadToSort(hot, sortingPayload) {
    const pluginSort = querySortToPluginSort(sortingPayload, (prop)=>hot.propToCol(prop));
    return pluginSort && typeof pluginSort === 'object' && 'column' in pluginSort ? pluginSort : [];
}
function handleBeforeColumnSortForServer(ctx, _currentSortConfig, destinationSortConfigs, sortPossible) {
    const { hot, hasFetchFn, applyQueryParametersFromPlugins, fetchData } = ctx;
    if (!hasFetchFn() || !sortPossible) {
        return;
    }
    const columnSorting = hot.getPlugin(COLUMN_SORTING_PLUGIN_KEY);
    columnSorting.setSortConfig(destinationSortConfigs);
    applyQueryParametersFromPlugins();
    fetchData({
        skipLoading: true
    });
    return false;
}
