"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 makeup_roving_tabindex_1 = require("makeup-roving-tabindex");
const eventUtils = __importStar(require("../../common/event-utils"));
class Tabs {
    /**
     * Handle a11y for heading
     * https://ebay.gitbooks.io/mindpatterns/content/disclosure/tabs.html
     */
    handleHeadingKeydown(dataIndex, event) {
        eventUtils.handleActionKeydown(event, () => {
            event.preventDefault();
            this._setIndex(dataIndex);
        });
        eventUtils.handleArrowsKeydown(event, () => {
            var _a;
            event.preventDefault();
            const { input, state } = this;
            const len = state.tab.length;
            const keyCode = event.charCode || event.keyCode;
            const direction = keyCode === 37 || keyCode === 38 ? -1 : 1;
            const selectedIndex = (state.selectedIndex + len + direction) % len;
            (_a = this.getEl(`tabs-${selectedIndex}`)) === null || _a === void 0 ? void 0 : _a.focus();
            if (!input.activation || input.activation === "auto") {
                this._setIndex(selectedIndex);
            }
        });
    }
    handleHeadingClick(selectedIndex) {
        this._setIndex(selectedIndex);
    }
    onCreate() {
        this.state = { selectedIndex: 0, tab: [], panel: [] };
    }
    onInput(input) {
        const { state } = this;
        state.tab = [...(input.tab || [])];
        state.panel = [...(input.panel || [])];
        if (!isNaN(input.selectedIndex)) {
            state.selectedIndex =
                parseInt(input.selectedIndex, 10) %
                    (state.tab.length || 1);
        }
    }
    onMount() {
        this._setupMakeup();
    }
    onUpdate() {
        this._setupMakeup();
    }
    onRender() {
        if (typeof window !== "undefined") {
            this._cleanupMakeup();
        }
    }
    onDestroy() {
        this._cleanupMakeup();
    }
    _setIndex(selectedIndex) {
        const { state } = this;
        if (selectedIndex !== state.selectedIndex) {
            state.selectedIndex = selectedIndex;
            this.emit("select", { selectedIndex });
        }
    }
    _setupMakeup() {
        const { input, state } = this;
        if (!input.fake) {
            this._linearRovingTabindex = (0, makeup_roving_tabindex_1.createLinear)(this.getEl("tabs"), ".tabs__item", {
                index: state.selectedIndex,
                wrap: true,
            });
        }
    }
    _cleanupMakeup() {
        if (this._linearRovingTabindex) {
            this._linearRovingTabindex.destroy();
            this._linearRovingTabindex = undefined;
        }
    }
}
module.exports = Tabs;
