"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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const makeup_expander_1 = __importDefault(require("makeup-expander"));
const eventUtils = __importStar(require("../../common/event-utils"));
const dropdown_1 = require("../../common/dropdown");
class FakeMenuButton {
    handleMenuKeydown({ el, originalEvent, index }) {
        eventUtils.handleActionKeydown(originalEvent, () => {
            this.handleMenuSelect({ index, originalEvent, el });
        });
        eventUtils.handleEscapeKeydown(originalEvent, () => {
            this.expander.expanded = false;
            this.focus();
        });
    }
    focus() {
        this.getComponent("button").el.focus();
    }
    handleButtonEscape() {
        this.expander.expanded = false;
    }
    handleExpand() {
        this.dropdownUtil.show();
        this.emitComponentEvent({ eventType: "expand" });
    }
    handleCollapse() {
        this.dropdownUtil.hide();
        this.emitComponentEvent({ eventType: "collapse" });
    }
    handleMenuSelect({ el, originalEvent, index }) {
        this.emitComponentEvent({
            eventType: "select",
            el,
            originalEvent,
            index,
        });
    }
    handleMousedown(originalEvent, el) {
        this.emitComponentEvent({ eventType: "mousedown", el, originalEvent });
    }
    emitComponentEvent({ eventType, el, originalEvent, index, }) {
        let eventObj = {
            el,
            originalEvent,
            index,
        };
        eventObj = Object.fromEntries(Object.entries(eventObj).filter(([, value]) => value !== undefined));
        this.emit(`${eventType}`, eventObj);
    }
    onRender() {
        if (typeof window !== "undefined") {
            this._cleanupMakeup();
        }
    }
    onMount() {
        this._setupMakeup();
    }
    onUpdate() {
        this._setupMakeup();
    }
    onDestroy() {
        this._cleanupMakeup();
    }
    _setupMakeup() {
        // In expander we set focusManagement to interactive for fake menu
        // This is related to setting the tabindex=-1 on menu-button div container for fake menu
        // In Safari and Firefox when clicking on a button/anchor focus is triggered on
        // the closest element with tabindex=-1 if there are any above it.
        // This would cause the menu to close before events are fired
        this.expander = new makeup_expander_1.default(this.el, {
            hostSelector: ".fake-menu-button__button",
            contentSelector: ".fake-menu-button__menu",
            focusManagement: "interactive",
            expandOnClick: true,
            autoCollapse: true,
            alwaysDoFocusManagement: true,
        });
        this.dropdownUtil = new dropdown_1.DropdownUtil(this.getEl("button"), this.getEl("content"), {
            reverse: this.input.reverse,
            strategy: this.input.strategy,
        });
    }
    _cleanupMakeup() {
        var _a;
        if (this.expander) {
            this.expander.destroy();
        }
        (_a = this.dropdownUtil) === null || _a === void 0 ? void 0 : _a.cleanup();
    }
}
module.exports = FakeMenuButton;
