"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const event = require("../../../utils/dom/event.js");
const rovingFocusGroup = require("./roving-focus-group.js");
const tokens = require("./tokens.js");
const utils = require("./utils.js");
const index = require("../../../../node_modules/@vueuse/core/index.js");
const Vue = require("vue");
const CURRENT_TAB_ID_CHANGE_EVT = "currentTabIdChange";
const ENTRY_FOCUS_EVT = "rovingFocusGroup.entryFocus";
const EVT_OPTS = {
  bubbles: false,
  cancelable: true
};
var roving_focus_group_impl_vue_vue_type_script_lang_default = Vue.defineComponent({
  name: "ElRovingFocusGroupImpl",
  inheritAttrs: false,
  props: rovingFocusGroup.rovingFocusGroupProps,
  emits: [CURRENT_TAB_ID_CHANGE_EVT, "entryFocus"],
  setup(props, { emit }) {
    const currentTabbedId = Vue.ref((props.currentTabId || props.defaultCurrentTabId) ?? null);
    const isBackingOut = Vue.ref(false);
    const isClickFocus = Vue.ref(false);
    const rovingFocusGroupRef = Vue.ref();
    const { getItems } = Vue.inject(rovingFocusGroup.ROVING_FOCUS_COLLECTION_INJECTION_KEY, void 0);
    const rovingFocusGroupRootStyle = Vue.computed(() => {
      return [{ outline: "none" }, props.style];
    });
    const onItemFocus = (tabbedId) => {
      emit(CURRENT_TAB_ID_CHANGE_EVT, tabbedId);
    };
    const onItemShiftTab = () => {
      isBackingOut.value = true;
    };
    const onMousedown = event.composeEventHandlers((e) => {
      var _a;
      (_a = props.onMousedown) == null ? void 0 : _a.call(props, e);
    }, () => {
      isClickFocus.value = true;
    });
    const onFocus = event.composeEventHandlers((e) => {
      var _a;
      (_a = props.onFocus) == null ? void 0 : _a.call(props, e);
    }, (e) => {
      const isKeyboardFocus = !Vue.unref(isClickFocus);
      const { target, currentTarget } = e;
      if (target === currentTarget && isKeyboardFocus && !Vue.unref(isBackingOut)) {
        const entryFocusEvt = new Event(ENTRY_FOCUS_EVT, EVT_OPTS);
        currentTarget == null ? void 0 : currentTarget.dispatchEvent(entryFocusEvt);
        if (!entryFocusEvt.defaultPrevented) {
          const items = getItems().filter((item) => item.focusable);
          utils.focusFirst([
            items.find((item) => item.active),
            items.find((item) => item.id === Vue.unref(currentTabbedId)),
            ...items
          ].filter(Boolean).map((item) => item.ref));
        }
      }
      isClickFocus.value = false;
    });
    const onBlur = event.composeEventHandlers((e) => {
      var _a;
      (_a = props.onBlur) == null ? void 0 : _a.call(props, e);
    }, () => {
      isBackingOut.value = false;
    });
    const handleEntryFocus = (...args) => {
      emit("entryFocus", ...args);
    };
    const onKeydown = (e) => {
      const focusIntent = utils.getFocusIntent(e);
      if (focusIntent) {
        e.preventDefault();
        let elements = getItems().filter((item) => item.focusable).map((item) => item.ref);
        switch (focusIntent) {
          case "last":
            elements.reverse();
            break;
          case "prev":
          case "next": {
            if (focusIntent === "prev") elements.reverse();
            const currentIdx = elements.indexOf(e.currentTarget);
            elements = props.loop ? utils.reorderArray(elements, currentIdx + 1) : elements.slice(currentIdx + 1);
            break;
          }
        }
        Vue.nextTick(() => {
          utils.focusFirst(elements);
        });
      }
    };
    Vue.provide(tokens.ROVING_FOCUS_GROUP_INJECTION_KEY, {
      currentTabbedId: Vue.readonly(currentTabbedId),
      loop: Vue.toRef(props, "loop"),
      tabIndex: Vue.computed(() => {
        return Vue.unref(isBackingOut) ? -1 : 0;
      }),
      rovingFocusGroupRef,
      rovingFocusGroupRootStyle,
      orientation: Vue.toRef(props, "orientation"),
      dir: Vue.toRef(props, "dir"),
      onItemFocus,
      onItemShiftTab,
      onBlur,
      onFocus,
      onMousedown,
      onKeydown
    });
    Vue.watch(() => props.currentTabId, (val) => {
      currentTabbedId.value = val ?? null;
    });
    index.useEventListener(rovingFocusGroupRef, ENTRY_FOCUS_EVT, handleEntryFocus);
  }
});
exports.default = roving_focus_group_impl_vue_vue_type_script_lang_default;
