"use strict";
/**
 * MobileNav class
 *
 *  Copyright (C) 2010-2019 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/>.
 */
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.MobileNav = void 0;
var events_1 = require("events");
/**
 * Hooks mobile navigation bar
 */
var MobileNav = /** @class */ (function (_super) {
    __extends(MobileNav, _super);
    function MobileNav(nav_menu) {
        var _this = _super.call(this) || this;
        _this.SECTION_CLASS = 'section-item';
        _this.STEP_CLASS = 'step-item';
        _this.NAV_CLASS = 'mobile-nav';
        _this.HEADER_CLASS = 'mobile-nav-header';
        // We don't have a nav menu, don't proceed
        if (!nav_menu) {
            return _this;
        }
        _this._nav = (nav_menu.getElementsByClassName(_this.NAV_CLASS)[0]);
        _this._nav_header = (nav_menu.getElementsByClassName(_this.HEADER_CLASS)[0]);
        _this._init();
        return _this;
    }
    /**
     * Initialize onclick events and element
     */
    MobileNav.prototype._init = function () {
        var _this = this;
        if (!this._nav_header || !this._nav) {
            return;
        }
        this._nav_header.onclick = function () { return _this.toggleNav(); };
        var sections = this._nav.getElementsByClassName(this.SECTION_CLASS);
        Array.prototype.forEach.call(sections, function (elem, i) {
            elem.onclick = function () {
                Array.prototype.forEach.call(sections, function (section, j) {
                    var show = i === j && !section.classList.contains('show-children');
                    section.classList[show ? 'add' : 'remove']('show-children');
                });
            };
        });
        var step_items = this._nav.getElementsByClassName(this.STEP_CLASS);
        Array.prototype.forEach.call(step_items, function (elem, index) {
            elem.onclick = function (event) {
                event.preventDefault();
                _this.emit('click', index + 1);
            };
        });
    };
    /**
     * Notify the nav of a step change
     *
     * @param header_label - a label to set on the
     */
    MobileNav.prototype.stepChanged = function (header_label) {
        this._updateHeader(header_label);
        this.toggleNav(false);
        return this;
    };
    /**
     * Toggles the visibility of the nav
     *
     * @param visible - (optional) if supplied, set the visibility explicitly
     */
    MobileNav.prototype.toggleNav = function (visible) {
        var _a, _b;
        if (visible !== undefined) {
            (_a = this._nav) === null || _a === void 0 ? void 0 : _a.classList[visible ? 'remove' : 'add']('hidden');
            return this;
        }
        (_b = this._nav) === null || _b === void 0 ? void 0 : _b.classList.toggle('hidden');
        return this;
    };
    /**
     * Update the header text
     *
     * @param label - new text
     */
    MobileNav.prototype._updateHeader = function (label) {
        if (!this._nav_header) {
            return this;
        }
        this._nav_header.innerHTML = '<span>' + label + '</span>';
        return this;
    };
    return MobileNav;
}(events_1.EventEmitter));
exports.MobileNav = MobileNav;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiTW9iaWxlTmF2LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiTW9iaWxlTmF2LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQTs7Ozs7Ozs7Ozs7Ozs7Ozs7OztHQW1CRzs7Ozs7Ozs7Ozs7Ozs7OztBQUVILGlDQUFvQztBQUVwQzs7R0FFRztBQUNIO0lBQStCLDZCQUFZO0lBYXpDLG1CQUFZLFFBQXFCO1FBQWpDL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