"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NavStyler = void 0;
/**
 * Responsible for styling the navigation bar
 */
var NavStyler = /** @class */ (function () {
    /**
     * Initializes NavStyler
     *
     * @param _nav                      - nav object that styling is based on
     * @param step_selector             - a selector string for step items
     * @param section_selector          - a selector string for section items
     * @param nav_selector              - a selector string for navs
     * @param _show_other_section_steps - (optional) whether to show steps
     *                                    from non-active sections
     */
    function NavStyler(_nav, _nav_steps, _nav_sections, _navs, _show_other_section_steps) {
        var _this = this;
        if (_show_other_section_steps === void 0) { _show_other_section_steps = false; }
        this._nav = _nav;
        this._nav_steps = _nav_steps;
        this._nav_sections = _nav_sections;
        this._navs = _navs;
        this._show_other_section_steps = _show_other_section_steps;
        /**
         * Whether the current quote has been locked
         */
        this._quoteLocked = false;
        /**
         * Original step classes
         */
        this._origClasses = [];
        /**
         * Original section classes
         */
        this._origSectionClasses = {};
        this._nav_visible = false;
        // highlight the step when it changes
        this._nav.on('stepChange', function (step_id) {
            // nav is initially hidden until initialized
            _this._showNavs();
            _this.highlightStep(step_id);
        });
        this._initOrigClasses();
    }
    NavStyler.prototype._showNavs = function () {
        if (this._nav_visible || !this._navs) {
            return;
        }
        for (var i = 0; i < this._navs.length; i++) {
            this._navs[i].classList.remove('hidden');
        }
        this._nav_visible = true;
    };
    NavStyler.prototype._initOrigClasses = function () {
        if (!this._nav_steps) {
            return;
        }
        for (var i = 0; i < this._nav_steps.length; i++) {
            this._origClasses[i + 1] = this._nav_steps[i].getAttribute('class');
        }
        if (!this._nav_sections) {
            return;
        }
        for (var i = 0; i < this._nav_sections.length; i++) {
            var section = this._nav_sections[i];
            var section_id = section.getAttribute('data-section-id');
            if (section_id === null) {
                continue;
            }
            this._origSectionClasses[section_id] = this._nav_sections[i].getAttribute('class');
        }
    };
    /**
     * Highlights the current step in the navigation bar
     *
     * This method will loop through each of the step elements and explicitly
     * set their style. The system makes no attempt to figure out which steps
     * should be changed as there is very little overhead with setting the style
     * of each of the steps. The additional logic would over-complicate things.
     *
     * @param step_id id of the step to highlight
     */
    NavStyler.prototype.highlightStep = function (step_id) {
        if (!this._nav_steps) {
            return this;
        }
        // loop through each of the steps in the navigation bar and highlight
        // them in the appropriate manner
        for (var i = 0; i < this._nav_steps.length; i++) {
            // indexes are 0-based, yet our step ids are 1-based
            var cur_step = i + 1;
            // explicitly set the class to what we want (no add, remove, etc
            // because that'll get too complicated to keep track of)
            this._nav_steps[i].setAttribute('class', this._getStepClass(cur_step, step_id));
        }
        if (!this._nav_sections) {
            return this;
        }
        for (var i = 0; i < this._nav_sections.length; i++) {
            var section = this._nav_sections[i];
            var section_id = section.getAttribute('data-section-id');
            if (!section_id) {
                continue;
            }
            // explicitly set the class to what we want (no add, remove, etc
            // because that'll get too complicated to keep track of)
            section.setAttribute('class', this._getSectionClass(step_id, section_id));
        }
        return this;
    };
    /**
     * Sets whether the quote has been locked
     *
     * @param value - whether quote is locked
     */
    NavStyler.prototype.quoteLocked = function (value) {
        this._quoteLocked = !!value;
        return this;
    };
    /**
     * Determines what class to apply to each of the step elements
     *
     * @param step_id          - id of step to get the class for
     * @param relative_step_id - id of the currently selected step
     *
     * @return String class to apply to step element
     */
    NavStyler.prototype._getStepClass = function (step_id, relative_step_id) {
        var last = this._nav.getTopVisitedStepId();
        var step_class = '';
        // current step
        if (step_id === relative_step_id) {
            // return a different style if there's visited steps after the
            // current one
            step_class = step_id === last ? 'stepCurrent' : 'stepCurrentPreVisited';
        }
        // step before the current step
        else if (step_id === relative_step_id - 1) {
            step_class = 'stepVisitedPreCurrent';
        }
        // previously visited steps
        else if (step_id < relative_step_id) {
            step_class = 'stepVisitedPreVisited';
        }
        // visited step after the current step
        else if (step_id > relative_step_id && step_id < last) {
            step_class = 'stepVisitedPreVisited';
        }
        else if (step_id === last) {
            step_class = 'stepVisited';
        }
        else {
            // when it doubt, clear it out
            step_class = '';
        }
        // if the quote is locked and this is not the last step, mark it as
        // locked
        if ((this._quoteLocked && step_id !== last) ||
            step_id < this._nav.getMinStepId()) {
            step_class += ' locked';
        }
        if (!this._nav.stepIsVisible(step_id) ||
            (this._nav.hasSubsteps() &&
                !this._nav.stepWithinSection(step_id, this._nav.getCurrentSectionId()) &&
                !this._show_other_section_steps)) {
            step_class += ' hidden';
        }
        var original = this._origClasses[step_id] || '';
        return step_class + ' ' + original;
    };
    /**
     * Determines what class to apply to each of the section elements
     *
     * @param step_id    - id of the currently selected step
     * @param section_id - id of section to get the class for
     *
     * @return String class to apply to step element
     */
    NavStyler.prototype._getSectionClass = function (step_id, section_id) {
        var last = this._nav.getTopVisitedStepId();
        var section_class = '';
        if (this._nav.stepWithinSection(step_id, section_id)) {
            section_class = 'sectionCurrent';
        }
        // if the quote is locked and this is not the last step, mark it as
        // locked
        if ((this._quoteLocked && step_id !== last) ||
            step_id < this._nav.getMinStepId()) {
            section_class += ' locked';
        }
        if (!this._nav.sectionIsVisible(section_id)) {
            section_class += ' hidden';
        }
        var original = this._origSectionClasses[section_id] || '';
        return section_class + ' ' + original;
    };
    return NavStyler;
}());
exports.NavStyler = NavStyler;
//# sourceMappingURL=data:application/json;base64, 