/* *
 *
 *  Grid Querying Controller class
 *
 *  (c) 2020-2026 Highsoft AS
 *
 *  Integration of this software requires a license.
 *  - For commercial use, see www.highcharts.com/license
 *  - For non-commercial, see www.highcharts.com/license-eula
 *
 *
 *  Authors:
 *  - Dawid Draguła
 *
 * */
'use strict';
import SortingController from './SortingController.js';
import FilteringController from './FilteringController.js';
import PaginationController from './PaginationController.js';
/* *
 *
 *  Class
 *
 * */
/**
 * Class that manage data modification of the visible data in the data grid.
 * It manages the modifiers that are applied to the data table.
 */
class QueryingController {
    /* *
    *
    *  Constructor
    *
    * */
    constructor(grid) {
        /**
         * This flag should be set to `true` if the modifiers should reapply to the
         * data table due to some data change or other important reason.
         */
        this.shouldBeUpdated = false;
        this.grid = grid;
        this.filtering = new FilteringController(this);
        this.sorting = new SortingController(this);
        this.pagination = new PaginationController(this);
    }
    /* *
    *
    *  Functions
    *
    * */
    /**
     * Proceeds with the data modification if needed.
     *
     * @param force
     * If the data should be modified even if the significant options are not
     * changed.
     */
    async proceed(force = false) {
        if (force || this.shouldBeUpdated) {
            await this.modifyData();
        }
    }
    /**
     * Load all options needed to generate the modifiers.
     */
    loadOptions() {
        this.filtering.loadOptions();
        this.sorting.loadOptions();
        this.pagination.loadOptions();
    }
    /**
     * Creates a list of modifiers that should be applied to the data table.
     */
    willNotModify() {
        return (!this.sorting.modifier &&
            !this.filtering.modifier);
    }
    /**
     * Returns a list of modifiers that should be applied to the data table.
     */
    getGroupedModifiers() {
        const modifiers = [];
        if (this.sorting.modifier) {
            modifiers.push(this.sorting.modifier);
        }
        if (this.filtering.modifier) {
            modifiers.push(this.filtering.modifier);
        }
        return modifiers;
    }
    /**
     * Apply all modifiers to the data provider.
     */
    async modifyData() {
        const dataProvider = this.grid.dataProvider;
        if (!dataProvider) {
            return;
        }
        await dataProvider.applyQuery();
        this.shouldBeUpdated = false;
    }
}
/* *
 *
 *  Default Export
 *
 * */
export default QueryingController;
