"use strict";
/* TODO auto-generated eslint ignore, please fix! */
/* eslint prefer-const: "off" */
/**
 * GroupStateManager
 *
 *  Copyright (C) 2010-2020 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework
 *
 *  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/>.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.GroupStateManager = exports.GroupState = void 0;
/**
 * Listing of possible states
 *
 * Pending - A pending state represents when a group's content is not ready to
 * be displayed. In the meantime, the presence of a pending state allows the
 * group to display temporary content or make other informed decisions.
 *
 * Disabled - A disabled state represents that the group is not active.
 */
var GroupState;
(function (GroupState) {
    GroupState["Pending"] = "pending";
    GroupState["Disabled"] = "disabled";
})(GroupState = exports.GroupState || (exports.GroupState = {}));
/**
 * Manage various states a group might have
 *
 * The GroupStateManager registers flags stored in data attributes of the
 * group's markup which are references to keys in the bucket.
 */
var GroupStateManager = /** @class */ (function () {
    function GroupStateManager() {
        /**
         * A legend that maps possible states to their bucket triggers
         */
        this._legend = {
            pending: [],
            disabled: [],
        };
    }
    /**
     * Determine if the state manager observes the bucket for a certain state
     *
     * @param state - state name
     *
     * @return if pending is observed by the manager
     */
    GroupStateManager.prototype.observes = function (state) {
        var _a, _b;
        return ((_b = (_a = this._legend[state]) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0;
    };
    /**
     * Read and store relevant state attributes from an HTML element
     *
     * @param target - element containing state data
     */
    GroupStateManager.prototype.processDataAttributes = function (target) {
        var pending = target.getAttribute('data-pending-when') || '';
        var disabled = target.getAttribute('data-disabled-when') || '';
        this._legend['pending'] = pending ? pending.split(' ') : [];
        this._legend['disabled'] = disabled ? disabled.split(' ') : [];
    };
    /**
     * Determine if a particular state is enabled for the group
     *
     * @param state  - state to check
     * @param bucket - bucket
     *
     * @return if a group's state is enabled
     */
    GroupStateManager.prototype.is = function (state, bucket) {
        if (!this.observes(state)) {
            return false;
        }
        for (var index in this._legend[state]) {
            var key = this._legend[state][index];
            var data = bucket.getDataByName(key) || [];
            for (var data_index in data) {
                if (+data[data_index] > 0) {
                    return true;
                }
            }
        }
        return false;
    };
    return GroupStateManager;
}());
exports.GroupStateManager = GroupStateManager;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiR3JvdXBTdGF0ZU1hbmFnZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJHcm91cFN0YXRlTWFuYWdlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsb0RBQW9EO0FBQ3BELGdDQUFnQztBQUNoQzs7Ozs7Ozs7Ozs7Ozs7Ozs7OztHQW1CRzs7O0FBRUg7Ozs7Ozs7O0dBUUc7QUFDSCxJQUFZLFVBR1g7QUFIRCxXQUFZLFVBQVU7SUFDcEIsaUNBQW1CLENBQUE7SUFDbkIsbUNBQXFCLENBQUE7QUFDdkIsQ0FBQyxFQUhXLFVBQVUsR0FBVixrQkFBVSxLQUFWLGtCQUFVLFFBR3JCO0FBYUQ7Ozs7O0dBS0c7QUFDSDtJQUFBO1FBQ0U7O1dBRUc7UUFDSyxZQUFPLEdBQWdCO1lBQzdCLE9BQU8sRUFBRSxFQUFFO1lBQ1gsUUFBUSxFQUFFLEVBQUU7U0FDYixDQUFDO0lBb0RKLENBQUM7SUFsREM7Ozs7OztPQU1HO0lBQ0ksb0NBQVEsR0FBZixVQUFnQixLQUFpQjs7UUFDL0IsT0FBTyxhQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDLDBDQUFFL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