"use strict";
/* TODO auto-generated eslint ignore, please fix! */
/* eslint @typescript-eslint/no-inferrable-types: "off", no-var: "off" */
/**
 *  Grid Group UI
 *
 *  Copyright (C) 2010-2020 R-T Specialty, LLC.
 *
 *  This file is part of liza.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.GridGroupUi = void 0;
var GroupUi_1 = require("./GroupUi");
var GridGroupUi = /** @class */ (function (_super) {
    __extends(GridGroupUi, _super);
    function GridGroupUi() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        /**
         * Categories pertaining to the group
         */
        _this._categories = [];
        /**
         * Inner container
         */
        _this._box = null;
        /**
         * Details pane
         */
        _this._details = null;
        /**
         * Reference to the group's marker on the x-axis
         */
        _this._x_type = null;
        /**
         * If group is selected
         */
        _this._is_selected = false;
        /**
         * Bucket key for currently selected group
         */
        _this._selected_current_key = null;
        /**
         * Bucket key for list of selected group
         */
        _this._selected_list_key = null;
        /**
         * Selectd bucket value of group
         */
        _this._selected_value = null;
        /**
         * If the group is visible
         */
        _this._is_visible = false;
        /**
         * Number of ancestors back to sync styles with
         */
        _this._relevant_style_ancestor = 4;
        return _this;
    }
    /**
     * Get the group's visibility
     *
     * @return if the group is visibile
     */
    GridGroupUi.prototype.isVisible = function () {
        return this._is_visible;
    };
    /**
     * Read the x-type of this group
     *
     * The x-type is an identifier for the column that this group belongs to.
     *
     * @return the x-type of the group
     */
    GridGroupUi.prototype.getXType = function () {
        return this._x_type;
    };
    /**
     * Read the categories of this group
     *
     * Categories are tags on a group that imply behavior.
     *
     * @return the categories of the group
     */
    GridGroupUi.prototype.getCategories = function () {
        return this._categories;
    };
    /**
     * Called when the group is visited
     */
    GridGroupUi.prototype.visit = function () {
        this._processDataAttributes();
        this._processClasses();
        this._setState();
        return this;
    };
    /**
     * Get selected value
     */
    GridGroupUi.prototype.getSelectedValue = function () {
        return this._selected_value;
    };
    /**
     * Determine if the group is selected
     *
     * @return if the group is selected
     */
    GridGroupUi.prototype.isSelected = function () {
        return this._is_selected;
    };
    /**
     * Select the group
     *
     * @param selected_values selected values (optional)
     */
    GridGroupUi.prototype.select = function (selected_values) {
        this._is_selected = true;
        this.content.classList.remove('deselected');
        this.content.classList.add('selected');
        if (selected_values !== undefined) {
            this._setSelectedData(selected_values);
        }
    };
    /**
     * Deselect the group
     *
     * @param selected_values selected values (optional)
     */
    GridGroupUi.prototype.deselect = function (selected_values) {
        if (this.isSelected()) {
            this._is_selected = false;
            this.content.classList.remove('selected');
            this.content.classList.add('deselected');
            if (selected_values !== undefined) {
                this._setSelectedData(selected_values);
            }
        }
    };
    /**
     * Set selected if selected value is already set in the bucket
     */
    GridGroupUi.prototype._setSelectedStatus = function () {
        var _a;
        if (this._selected_list_key === null || this._selected_value === null) {
            return;
        }
        var current_values = (_a = this._quote) === null || _a === void 0 ? void 0 : _a.getDataByName(this._selected_list_key);
        if (Array.isArray(current_values) &&
            current_values.indexOf(this._selected_value) > -1) {
            this.select();
        }
        else {
            this.deselect();
        }
    };
    /**
     * Set deselected if required due to a change in state
     */
    GridGroupUi.prototype._setDeSelectedStatus = function () {
        var _this = this;
        var _a;
        if (this.isSelected() === false ||
            this._selected_list_key === null ||
            this._selected_value === null) {
            return;
        }
        var current_values = (_a = this._quote) === null || _a === void 0 ? void 0 : _a.getDataByName(this._selected_list_key);
        if (Array.isArray(current_values) &&
            current_values.indexOf(this._selected_value) > -1) {
            // Remove group from current values
            var selected_values = current_values.filter(function (item) { return item !== _this._selected_value; });
            this.deselect(selected_values);
        }
    };
    /**
     * Update the selected value in the bucket
     *
     * @param selected_values selected values
     */
    GridGroupUi.prototype._setSelectedData = function (selected_values) {
        var _a;
        var _b, _c;
        // Do not continue if any data is not valid
        if (Array.isArray(selected_values) === false ||
            this._selected_current_key === null ||
            this._selected_list_key === null ||
            this._selected_value === null) {
            return;
        }
        var set_current = [];
        if (this.isSelected()) {
            // Add the group value
            selected_values.push(this._selected_value);
            set_current = [this._selected_value];
        }
        else {
            // Get current selected value
            var current_select = ((_b = this._quote) === null || _b === void 0 ? void 0 : _b.getDataByName(this._selected_current_key)) || [];
            if (!Array.isArray(current_select) ||
                current_select.indexOf(this._selected_value) === -1) {
                // If selected value not set, keep current value
                set_current = current_select;
            }
        }
        // Remove empty values
        selected_values = selected_values.filter(function (item) { return item !== ''; });
        // Force entire array to be overwritten
        selected_values.push(null);
        (_c = this._quote) === null || _c === void 0 ? void 0 : _c.setData((_a = {},
            _a[this._selected_list_key] = selected_values,
            _a[this._selected_current_key] = set_current,
            _a));
    };
    /**
     * Determine if the details pane is open
     *
     * @return if the details pane is open
     */
    GridGroupUi.prototype.areDetailsOpen = function () {
        if (this._details === null) {
            return false;
        }
        return this.content.classList.contains('details-open');
    };
    /**
     * Open the details pane
     *
     * @param stylers
     */
    GridGroupUi.prototype.openDetails = function (stylers) {
        var _this = this;
        if (this._details !== null) {
            this.content.classList.add('details-open');
            stylers.forEach(function (styler) {
                return styler.style(_this._details, _this._relevant_style_ancestor);
            });
        }
    };
    /**
     * Close the details pane
     *
     * @param stylers
     */
    GridGroupUi.prototype.closeDetails = function (stylers) {
        var _this = this;
        if (this.areDetailsOpen()) {
            this.content.classList.remove('details-open');
            stylers.forEach(function (styler) {
                return styler.style(_this._details, _this._relevant_style_ancestor);
            });
        }
    };
    /**
     * Process content of the group
     *
     * @param quote - target quote
     */
    GridGroupUi.prototype.processContent = function (quote) {
        var _this = this;
        this._box = this._getBox();
        this._details = this._getDetails();
        this._quote = quote;
        quote.visitData(function (bucket) {
            _this._bucket = bucket;
            _this._bucket.on('stagingUpdate', function () { return _this._setState(); });
        });
        this.fieldContentParent[0] = this.content.querySelector('dl');
        this.context.createFieldCache();
    };
    /**
     * Get the targeted inner div
     *
     * @return inner div
     */
    GridGroupUi.prototype._getBox = function () {
        return this.content.querySelector('div');
    };
    /**
     * Get the targeted div of the details pane
     *
     * @return details pane div
     */
    GridGroupUi.prototype._getDetails = function () {
        return this.content.querySelector('.details-pane');
    };
    /**
     * Set the current state of the group
     */
    GridGroupUi.prototype._setState = function () {
        this._setPending(this._state_manager.is('pending', this._bucket));
        if (this._state_manager.observes('disabled')) {
            this._setDisabled(this._state_manager.is('disabled', this._bucket));
        }
    };
    /**
     * Read all data attributes
     */
    GridGroupUi.prototype._processDataAttributes = function () {
        var _a, _b, _c, _d, _e;
        this._x_type = ((_a = this._box) === null || _a === void 0 ? void 0 : _a.getAttribute('data-x-type')) || null;
        this._selected_current_key =
            ((_b = this._box) === null || _b === void 0 ? void 0 : _b.getAttribute('data-selected-current-key')) || null;
        this._selected_list_key =
            ((_c = this._box) === null || _c === void 0 ? void 0 : _c.getAttribute('data-selected-list-key')) || null;
        this._selected_value =
            ((_d = this._box) === null || _d === void 0 ? void 0 : _d.getAttribute('data-selected-value')) || null;
        this._setSelectedStatus();
        this._state_manager.processDataAttributes(this._box);
        var categories = (_e = this._box) === null || _e === void 0 ? void 0 : _e.getAttribute('data-categories');
        if (categories) {
            this._categories = categories.split(/\s+/);
        }
        this._state_manager.processDataAttributes(this._box);
    };
    /**
     * Process class-related data
     */
    GridGroupUi.prototype._processClasses = function () {
        this._is_visible = this.content.classList.contains('is-visible');
        var operation = this._is_visible ? 'remove' : 'add';
        this.content.classList[operation]('hidden');
        if (!this._is_visible) {
            this._setDeSelectedStatus();
        }
    };
    /**
     * Apply the pending state to this group
     *
     * @param isPending if the group is pending
     */
    GridGroupUi.prototype._setPending = function (isPending) {
        var operation = isPending ? 'add' : 'remove';
        this.content.classList[operation]('pending');
    };
    /**
     * Apply the disabled state to this group
     *
     * @param isDisabled if the group is disabled
     */
    GridGroupUi.prototype._setDisabled = function (isDisabled) {
        var operation = isDisabled ? 'add' : 'remove';
        this.content.classList[operation]('disabled');
        if (isDisabled && !this.group.isInternal()) {
            // Ensure the group is deselected
            this._setDeSelectedStatus();
        }
    };
    /**
     * This group does not support multiple indexes
     *
     * @return false
     */
    GridGroupUi.prototype.supportsMultipleIndex = function () {
        return false;
    };
    /**
     * Permit adding only a single index
     *
     * @param index - index that has been added
     */
    GridGroupUi.prototype.addIndex = function (index) {
        if (index > 0) {
            return this;
        }
        return _super.prototype.addIndex.call(this, index);
    };
    /**
     * Permit removing only the first index
     *
     * This follows from #addIndex, since only one will ever exist.
     *
     * @param index - index that has been removed
     */
    GridGroupUi.prototype.removeIndex = function (index) {
        if (index > 0) {
            return this;
        }
        return _super.prototype.removeIndex.call(this, index);
    };
    /**
     * Called on quote classification
     */
    GridGroupUi.prototype.onClassify = function () {
        // TODO: This is only necessary because this class was implemented
        // before group visibility classifications. The custom css class
        // should be cleaned up along with the custom class when that is
        // implemented on the program to support this.
        //
        // i.e. <class name="is-visible" when="foo" />
        this._processClasses();
    };
    return GridGroupUi;
}(GroupUi_1.GroupUi));
exports.GridGroupUi = GridGroupUi;
//# sourceMappingURL=data:application/json;base64, 