"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 dropdown_1 = require("../../common/dropdown");
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);
    }
    handleMenuKeydown({ originalEvent }) {
        eventUtils.handleEscapeKeydown(originalEvent, () => {
            this._expander.expanded = false;
            this.getEl("button").focus();
        });
    }
    handleMenuChange({ checkedIndex, el, originalEvent, index, currentChecked, }) {
        // TODO: the event data from the filter-menu should probably
        // change to include which items are checked not just the values.
        if (checkedIndex !== undefined)
            this.toggleChecked(checkedIndex);
        this._emitComponentEvent("change", originalEvent, {
            el,
            index,
            currentChecked,
        });
    }
    handleFooterButtonClick() {
        this._emitComponentEvent("footer-click");
        this._expander.expanded = false;
    }
    handleFormSubmit({ originalEvent }) {
        this._emitComponentEvent("form-submit", originalEvent);
    }
    handleExpand({ originalEvent }) {
        this.dropdownUtil.show();
        this._emitComponentEvent("expand", originalEvent);
    }
    handleCollapse({ originalEvent }) {
        this.dropdownUtil.hide();
        this._emitComponentEvent("collapse", originalEvent);
    }
    onInput(input) {
        input.item = input.item || [];
        this.state = this.getInputState(input);
    }
    onMount() {
        this._setupMakeup();
    }
    onUpdate() {
        this._setupMakeup();
    }
    onRender() {
        if (typeof window !== "undefined") {
            this._cleanupMakeup();
        }
    }
    onDestroy() {
        this._cleanupMakeup();
    }
    _emitComponentEvent(eventType, originalEvent, args) {
        const { el, index, currentChecked } = args || {};
        switch (eventType) {
            case "expand":
                this.emit(eventType);
                break;
            case "change":
            case "collapse":
            case "form-submit":
            case "footer-click": {
                const checked = this.getCheckedValues();
                this.emit(eventType, {
                    el,
                    checked: checked,
                    originalEvent,
                    index,
                    currentChecked,
                });
                break;
            }
            default:
                break;
        }
    }
    _setupMakeup() {
        this._expander = new makeup_expander_1.default(this.getEl("container"), {
            hostSelector: ".filter-menu-button__button",
            contentSelector: ".filter-menu-button__menu",
            focusManagement: "interactive",
            expandOnClick: true,
            autoCollapse: true,
            alwaysDoFocusManagement: true,
        });
        this.dropdownUtil = new dropdown_1.DropdownUtil(this.getEl("button"), this.getEl("menu"), {
            offset: 8,
        });
    }
    _cleanupMakeup() {
        var _a;
        if (this._expander) {
            this._expander.destroy();
            this._expander = undefined;
        }
        (_a = this.dropdownUtil) === null || _a === void 0 ? void 0 : _a.cleanup();
    }
}
module.exports = default_1;
