'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var utils = require('./utils.js');
var tokens = require('./tokens.js');
var pluginVue_exportHelper = require('../../../_virtual/plugin-vue_export-helper.js');

const _sfc_main = vue.defineComponent({
  name: "PsFocusTrap",
  inheritAttrs: false,
  props: {
    loop: Boolean,
    trapped: Boolean
  },
  setup(props) {
    const focusTrapRef = vue.ref();
    const forwardRef = vue.ref(null);
    let lastFocusBeforeMounted;
    let lastFocusAfterMounted;
    const focusLayer = {
      paused: false,
      pause() {
        this.paused = true;
      },
      resume() {
        this.paused = false;
      }
    };
    vue.provide(tokens.FOCUS_TRAP_INJECTION_KEY, {
      focusTrapRef: forwardRef
    });
    const focusOnMount = (e) => {
      emit(tokens.ON_MOUNT_FOCUS_EVT, e);
    };
    const focusOnUnmount = (e) => emit(tokens.ON_UNMOUNT_FOCUS_EVT, e);
    const onFocusIn = (e) => {
      const trapContainer = vue.unref(forwardRef);
      if (focusLayer.paused || !trapContainer)
        return;
      const target = e.target;
      if (target && trapContainer.contains(target)) {
        lastFocusAfterMounted = target;
      } else {
        utils.tryFocus(lastFocusAfterMounted, true);
      }
    };
    const onFocusOut = (e) => {
      const trapContainer = vue.unref(forwardRef);
      if (focusLayer.paused || !trapContainer)
        return;
      if (!trapContainer.contains(e.relatedTarget)) {
        utils.tryFocus(lastFocusAfterMounted, true);
      }
    };
    const cleanupDocumentListeners = () => {
      document.removeEventListener("focusin", onFocusIn);
      document.removeEventListener("focusout", onFocusOut);
    };
    vue.onMounted(() => {
      const trapContainer = vue.unref(forwardRef);
      if (trapContainer) {
        utils.focusableStack.push(focusLayer);
        const prevFocusedElement = document.activeElement;
        lastFocusBeforeMounted = prevFocusedElement;
        const isPrevFocusContained = trapContainer.contains(prevFocusedElement);
        if (!isPrevFocusContained) {
          const mountEvent = new Event(tokens.FOCUS_ON_MOUNT, tokens.FOCUS_ON_MOUNT_OPTS);
          trapContainer.addEventListener(tokens.FOCUS_ON_MOUNT, focusOnMount);
          trapContainer.dispatchEvent(mountEvent);
          if (!mountEvent.defaultPrevented) {
            vue.nextTick(() => {
              utils.focusFirstDescendant(utils.obtainAllFocusableElements(trapContainer), true);
              if (document.activeElement === prevFocusedElement) {
                utils.tryFocus(trapContainer);
              }
            });
          }
        }
      }
    });
    vue.onBeforeUnmount(() => {
      cleanupDocumentListeners();
      const trapContainer = vue.unref(forwardRef);
      if (trapContainer) {
        trapContainer.removeEventListener(tokens.FOCUS_ON_MOUNT, focusOnMount);
        const unmountEvent = new Event(tokens.FOCUS_ON_UNMOUNT, tokens.FOCUS_ON_MOUNT_OPTS);
        trapContainer.addEventListener(tokens.FOCUS_ON_UNMOUNT, focusOnUnmount);
        trapContainer.dispatchEvent(unmountEvent);
        if (!unmountEvent.defaultPrevented) {
          utils.tryFocus(lastFocusBeforeMounted != null ? lastFocusBeforeMounted : document.body, true);
        }
        trapContainer.removeEventListener(tokens.FOCUS_ON_UNMOUNT, focusOnMount);
        utils.focusableStack.remove(focusLayer);
      }
    });
    return {
      focusTrapRef,
      forwardRef
    };
  }
});
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return vue.renderSlot(_ctx.$slots, "default");
}
var PsFocusTrap = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["render", _sfc_render], ["__file", "/Users/meng/workspace/element-ps/packages/components/focus-trap/src/focus-trap.vue"]]);

exports["default"] = PsFocusTrap;
//# sourceMappingURL=focus-trap.js.map
