"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const scrollKeyPreventer = __importStar(require("makeup-prevent-scroll-keys"));
const makeup_roving_tabindex_1 = require("makeup-roving-tabindex");
const eventUtils = __importStar(require("../../common/event-utils"));
const menu_utils_1 = __importStar(require("../../common/menu-utils"));
class default_1 extends menu_utils_1.MenuUtils {
    onCreate() {
        (0, menu_utils_1.default)(this);
    }
    resetIndex() {
        this._rovingTabIndex.index = 0;
        this.lastTabIndexPosition = 0;
    }
    handleSearch(e) {
        this.state.searchTerm = e.target.value;
        this.emit("search-change", this.state.searchTerm);
    }
    clearSearch() {
        this.state.searchTerm = "";
        this.emit("search-change", this.state.searchTerm);
    }
    handleRadioClick(index, ev, itemEl) {
        this._toggleItemChecked(index, ev.originalEvent, itemEl);
    }
    handleItemClick(index, ev, itemEl) {
        const targetEv = ev.originalEvent || ev;
        if (this.input.variant !== "form" ||
            targetEv.target.tagName !== "INPUT") {
            this._toggleItemChecked(index, ev, itemEl);
        }
    }
    handleItemKeydown(index, ev, itemEl) {
        eventUtils.handleEscapeKeydown(ev, () => {
            // TODO: this event is not documented.
            // Do we need it? (it is only used by the filter-menu-button)
            this._emitComponentEvent("keydown", ev, { index });
        });
        if (this.input.variant !== "form") {
            eventUtils.handleActionKeydown(ev, () => {
                this._toggleItemChecked(index, ev, itemEl);
            });
        }
        eventUtils.handleArrowsKeydown(ev, () => {
            setTimeout(() => {
                if (this._rovingTabIndex.index !== index) {
                    this._emitComponentEvent("keydown", ev, {
                        index: this._rovingTabIndex.index,
                    });
                }
            });
        });
    }
    handleFooterButtonClick(originalEvent) {
        this._emitComponentEvent("footer-click", originalEvent);
    }
    handleFormSubmit(originalEvent) {
        this._emitComponentEvent("form-submit", originalEvent);
    }
    onInput(input) {
        this.state = Object.assign(Object.assign({}, super.getInputState(input)), { searchTerm: input.searchHeaderValue !== undefined
                ? input.searchHeaderValue
                : this.state
                    ? this.state.searchTerm
                    : "" });
    }
    onMount() {
        this._setupMakeup();
    }
    onUpdate() {
        this._setupMakeup();
    }
    onRender() {
        if (typeof window !== "undefined") {
            this._cleanupMakeup();
        }
    }
    onDestroy() {
        this._cleanupMakeup();
    }
    _toggleItemChecked(index, originalEvent, el) {
        if (this.isDisabled(index)) {
            return;
        }
        this.toggleChecked(index);
        this._emitComponentEvent("change", originalEvent, { el, index });
    }
    _emitComponentEvent(eventType, originalEvent, args) {
        const { el, index } = args || {};
        const checked = this.getCheckedValues();
        const checkedIndex = this.getCheckedIndexes();
        const currentChecked = index === undefined ? undefined : this.isChecked(index);
        this.emit(`${eventType}`, {
            el,
            checked,
            checkedIndex,
            originalEvent,
            index,
            currentChecked,
        });
    }
    _setupMakeup() {
        if (this.input.variant !== "form") {
            this._rovingTabIndex = (0, makeup_roving_tabindex_1.createLinear)(this.getEl("menu"), "div", {
                autoInit: this.lastTabIndexPosition || "interactive",
            });
            scrollKeyPreventer.add(this.getEl("menu"));
        }
    }
    _cleanupMakeup() {
        if (this._rovingTabIndex) {
            this.lastTabIndexPosition = this._rovingTabIndex.index;
            this._rovingTabIndex.destroy();
            this._rovingTabIndex = undefined;
            scrollKeyPreventer.remove(this.getEl("menu"));
        }
    }
}
module.exports = default_1;
