"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 MultiColumnSorting () {
        return MultiColumnSorting;
    },
    get PLUGIN_KEY () {
        return PLUGIN_KEY;
    },
    get PLUGIN_PRIORITY () {
        return PLUGIN_PRIORITY;
    }
});
const _columnSorting = require("../columnSorting");
const _registry = require("../columnSorting/sortService/registry");
const _utils = require("../columnSorting/utils");
const _element = require("../../helpers/dom/element");
const _rootComparator = require("./rootComparator");
const _domHelpers = require("./domHelpers");
const _shortcutContexts = require("../../shortcutContexts");
const PLUGIN_KEY = 'multiColumnSorting';
const PLUGIN_PRIORITY = 170;
const SHORTCUTS_GROUP = PLUGIN_KEY;
(0, _registry.registerRootComparator)(PLUGIN_KEY, _rootComparator.rootComparator);
class MultiColumnSorting extends _columnSorting.ColumnSorting {
    static get PLUGIN_KEY() {
        return PLUGIN_KEY;
    }
    static get PLUGIN_PRIORITY() {
        return PLUGIN_PRIORITY;
    }
    static get SETTING_KEYS() {
        return [
            PLUGIN_KEY
        ];
    }
    /**
   * Checks if the plugin is enabled in the Handsontable settings. This method is executed in {@link Hooks#beforeInit}
   * hook and if it returns `true` then the {@link MultiColumnSorting#enablePlugin} method is called.
   * When [[Options#dataProvider]] is a complete server-backed configuration, the DataProvider plugin blocks this plugin from enabling.
   *
   * @returns {boolean}
   */ isEnabled() {
        return !!this.hot.getSettings()[this.pluginKey];
    }
    /**
   * Enables the plugin functionality for this Handsontable instance.
   */ enablePlugin() {
        if (this.enabled) {
            return;
        }
        super.enablePlugin();
    }
    /**
   * Disables the plugin functionality for this Handsontable instance.
   */ disablePlugin() {
        super.disablePlugin();
    }
    /**
   * Register shortcuts responsible for toggling column sorting functionality.
   *
   * @private
   */ registerShortcuts() {
        super.registerShortcuts();
        this.hot.getShortcutManager().getContext('grid').addShortcut({
            keys: [
                [
                    'Shift',
                    'Enter'
                ]
            ],
            callback: ()=>{
                const { highlight } = this.hot.getSelectedRangeActive();
                if (highlight.row === -1 && highlight.col >= 0) {
                    this.sort(this.getNextSortConfig(highlight.col, _columnSorting.APPEND_COLUMN_CONFIG_STRATEGY));
                }
                // prevent default Enter behavior (move to the next row within a selection range)
                return false;
            },
            runOnlyIf: ()=>{
                const highlight = this.hot.getSelectedRangeActive()?.highlight;
                return highlight && this.hot.getSelectedRangeActive()?.isSingle() && this.hot.selection.isCellVisible(highlight) && highlight.isHeader();
            },
            relativeToGroup: _shortcutContexts.EDITOR_EDIT_GROUP,
            position: 'before',
            group: SHORTCUTS_GROUP
        });
    }
    /**
   * Unregister shortcuts responsible for toggling column sorting functionality.
   *
   * @private
   */ unregisterShortcuts() {
        super.unregisterShortcuts();
        this.hot.getShortcutManager().getContext('grid').removeShortcutsByGroup(SHORTCUTS_GROUP);
    }
    /**
   * Sorts the table by chosen columns and orders.
   *
   * @param {undefined|object|Array} sortConfig Single column sort configuration or full sort configuration (for all sorted columns).
   * The configuration object contains `column` and `sortOrder` properties. First of them contains visual column index, the second one contains
   * sort order (`asc` for ascending, `desc` for descending).
   *
   * **Note**: Please keep in mind that every call of `sort` function set an entirely new sort order. Previous sort configs aren't preserved.
   *
   * @example
   * ```js
   * // sort ascending first visual column
   * hot.getPlugin('multiColumnSorting').sort({ column: 0, sortOrder: 'asc' });
   *
   * // sort first two visual column in the defined sequence
   * hot.getPlugin('multiColumnSorting').sort([{
   *   column: 1, sortOrder: 'asc'
   * }, {
   *   column: 0, sortOrder: 'desc'
   * }]);
   * ```
   *
   * @fires Hooks#beforeColumnSort
   * @fires Hooks#afterColumnSort
   */ sort(sortConfig) {
        super.sort(sortConfig);
    }
    /**
   * Clear the sort performed on the table.
   */ clearSort() {
        super.clearSort();
    }
    /**
   * Checks if the table is sorted (any column have to be sorted).
   *
   * @returns {boolean}
   */ isSorted() {
        return super.isSorted();
    }
    /**
   * Get sort configuration for particular column or for all sorted columns. Objects contain `column` and `sortOrder` properties.
   *
   * **Note**: Please keep in mind that returned objects expose **visual** column index under the `column` key. They are handled by the `sort` function.
   *
   * @param {number} [column] Visual column index.
   * @returns {undefined|object|Array}
   */ getSortConfig(column) {
        return super.getSortConfig(column);
    }
    /**
   * @description
   * Warn: Useful mainly for providing server side sort implementation (see in the example below). It doesn't sort the data set. It just sets sort configuration for all sorted columns.
   * Note: Please keep in mind that this method doesn't re-render the table.
   *
   * @example
   * ```js
   * beforeColumnSort: function(currentSortConfig, destinationSortConfigs) {
   *   const columnSortPlugin = this.getPlugin('multiColumnSorting');
   *
   *   columnSortPlugin.setSortConfig(destinationSortConfigs);
   *
   *   // const newData = ... // Calculated data set, ie. from an AJAX call.
   *
   *   this.updateData(newData); // Update data set and re-render the table.
   *
   *   return false; // The blockade for the default sort action.
   * }
   * ```
   *
   * @param {undefined|object|Array} sortConfig Single column sort configuration or full sort configuration (for all sorted columns).
   * The configuration object contains `column` and `sortOrder` properties. First of them contains visual column index, the second one contains
   * sort order (`asc` for ascending, `desc` for descending).
   */ setSortConfig(sortConfig) {
        super.setSortConfig(sortConfig);
    }
    /**
   * Get normalized sort configs.
   *
   * @private
   * @param {object|Array} [sortConfig=[]] Single column sort configuration or full sort configuration (for all sorted columns).
   * The configuration object contains `column` and `sortOrder` properties. First of them contains visual column index, the second one contains
   * sort order (`asc` for ascending, `desc` for descending).
   * @returns {Array}
   */ getNormalizedSortConfigs(sortConfig = []) {
        if (Array.isArray(sortConfig)) {
            return sortConfig;
        }
        return [
            sortConfig
        ];
    }
    /**
   * Update header classes.
   *
   * @private
   * @param {HTMLElement} headerSpanElement Header span element.
   * @param {...*} args Extra arguments for helpers.
   */ updateHeaderClasses(headerSpanElement, ...args) {
        super.updateHeaderClasses(headerSpanElement, ...args);
        (0, _element.removeClass)(headerSpanElement, (0, _domHelpers.getClassesToRemove)(headerSpanElement));
        if (this.enabled !== false) {
            (0, _element.addClass)(headerSpanElement, (0, _domHelpers.getClassesToAdd)(...args));
        }
    }
    /**
   * Callback for the `onAfterOnCellMouseDown` hook.
   *
   * @private
   * @param {Event} event Event which are provided by hook.
   * @param {CellCoords} coords Visual coords of the selected cell.
   */ onAfterOnCellMouseDown(event, coords) {
        if ((0, _utils.wasHeaderClickedProperly)(coords.row, coords.col, event) === false) {
            return;
        }
        if (this.wasClickableHeaderClicked(event, coords.col)) {
            if (this.hot.getShortcutManager().isCtrlPressed()) {
                this.hot.deselectCell();
                this.hot.selectColumns(coords.col);
                this.sort(this.getNextSortConfig(coords.col, _columnSorting.APPEND_COLUMN_CONFIG_STRATEGY));
            } else {
                this.sort(this.getColumnNextConfig(coords.col));
            }
        }
    }
    constructor(...args){
        super(...args), /**
   * Main settings key designed for the plugin.
   *
   * @private
   * @type {string}
   */ this.pluginKey = PLUGIN_KEY;
    }
}
