"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 scrollKeyPreventer = __importStar(require("makeup-prevent-scroll-keys"));
const dropdown_1 = require("../../common/dropdown");
class ListboxButton {
    handleExpand() {
        this.getComponent("options").elementScroll();
        this.dropdownUtil.show();
        this.emit("expand");
    }
    handleCollapse() {
        this.getEl("button").focus();
        this.dropdownUtil.hide();
        this.emit("collapse");
    }
    handleListboxChange(event) {
        if (this.input.collapseOnSelect !== false) {
            this._expander.expanded = false;
        }
        const selectedIndex = event.index;
        this.state.selectedIndex = selectedIndex;
        this.emit("change", event);
    }
    handleListboxEscape() {
        this._expander.expanded = false;
    }
    onCreate() {
        this.state = {
            selectedIndex: -1,
        };
    }
    onInput(input) {
        input.option = input.option || [];
        this.state.selectedIndex = -1;
        let i = 0;
        for (const option of input.option || []) {
            if (option.selected) {
                this.state.selectedIndex = i;
                break;
            }
            i++;
        }
    }
    onMount() {
        this._setupMakeup();
    }
    onUpdate() {
        this._setupMakeup();
    }
    onRender() {
        if (typeof window !== "undefined") {
            this._cleanupMakeup();
        }
    }
    onDestroy() {
        this._cleanupMakeup();
    }
    _setupMakeup() {
        var _a;
        const { input } = this;
        // This `as any` is here for while `options` is coerced into an array from `marko-tag.json`.
        // After we move to the full `iterator` we can switch to `if (input.options && !input.disabled)`
        if (((_a = input.option) === null || _a === void 0 ? void 0 : _a.length) && !input.disabled) {
            const container = this.getEl("container");
            this._expander = new makeup_expander_1.default(container, {
                alwaysDoFocusManagement: true,
                autoCollapse: true,
                expandOnClick: true,
                simulateSpacebarClick: true,
                contentSelector: ".listbox-button__listbox",
                hostSelector: ".listbox-button__control",
                expandedClass: "listbox-button--expanded",
                focusManagement: "content",
                collapseOnHostReFocus: true,
            });
            scrollKeyPreventer.add(this.getEl("button"));
        }
        this.dropdownUtil = new dropdown_1.DropdownUtil(this.getEl("button"), this.getEl("options"), {
            strategy: input.strategy,
        });
    }
    _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 = ListboxButton;
