"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const shared = require("./shared.js");
const index_esm = require("../../../@vant/use/dist/index.esm.js");
const useExpose = require("../composables/use-expose.js");
const useLockScroll = require("../composables/use-lock-scroll.js");
const useLazyRender = require("../composables/use-lazy-render.js");
const onPopupReopen = require("../composables/on-popup-reopen.js");
const useGlobalZIndex = require("../composables/use-global-z-index.js");
const useScopeId = require("../composables/use-scope-id.js");
const index$1 = require("../icon/index.js");
const index = require("../overlay/index.js");
const interceptor = require("../utils/interceptor.js");
const constant = require("../utils/constant.js");
const basic = require("../utils/basic.js");
const create = require("../utils/create.js");
const props = require("../utils/props.js");
const popupProps = basic.extend({}, shared.popupSharedProps, {
  round: Boolean,
  position: props.makeStringProp("center"),
  closeIcon: props.makeStringProp("cross"),
  closeable: Boolean,
  transition: String,
  iconPrefix: String,
  closeOnPopstate: Boolean,
  closeIconPosition: props.makeStringProp("top-right"),
  destroyOnClose: Boolean,
  safeAreaInsetTop: Boolean,
  safeAreaInsetBottom: Boolean
});
const [name, bem] = create.createNamespace("popup");
var stdin_default = Vue.defineComponent({
  name,
  inheritAttrs: false,
  props: popupProps,
  emits: ["open", "close", "opened", "closed", "keydown", "update:show", "clickOverlay", "clickCloseIcon"],
  setup(props2, {
    emit,
    attrs,
    slots
  }) {
    let opened;
    let shouldReopen;
    const zIndex = Vue.ref();
    const popupRef = Vue.ref();
    const lazyRender = useLazyRender.useLazyRender(() => props2.show || !props2.lazyRender);
    const style = Vue.computed(() => {
      const style2 = {
        zIndex: zIndex.value
      };
      if (basic.isDef(props2.duration)) {
        const key = props2.position === "center" ? "animationDuration" : "transitionDuration";
        style2[key] = `${props2.duration}s`;
      }
      return style2;
    });
    const open = () => {
      if (!opened) {
        opened = true;
        zIndex.value = props2.zIndex !== void 0 ? +props2.zIndex : useGlobalZIndex.useGlobalZIndex();
        emit("open");
      }
    };
    const close = () => {
      if (opened) {
        interceptor.callInterceptor(props2.beforeClose, {
          done() {
            opened = false;
            emit("close");
            emit("update:show", false);
          }
        });
      }
    };
    const onClickOverlay = (event) => {
      emit("clickOverlay", event);
      if (props2.closeOnClickOverlay) {
        close();
      }
    };
    const renderOverlay = () => {
      if (props2.overlay) {
        const overlayProps = basic.extend({
          show: props2.show,
          class: props2.overlayClass,
          zIndex: zIndex.value,
          duration: props2.duration,
          customStyle: props2.overlayStyle,
          role: props2.closeOnClickOverlay ? "button" : void 0,
          tabindex: props2.closeOnClickOverlay ? 0 : void 0
        }, props2.overlayProps);
        return Vue.createVNode(index.Overlay, Vue.mergeProps(overlayProps, useScopeId.useScopeId(), {
          "onClick": onClickOverlay
        }), {
          default: slots["overlay-content"]
        });
      }
    };
    const onClickCloseIcon = (event) => {
      emit("clickCloseIcon", event);
      close();
    };
    const renderCloseIcon = () => {
      if (props2.closeable) {
        return Vue.createVNode(index$1.Icon, {
          "role": "button",
          "tabindex": 0,
          "name": props2.closeIcon,
          "class": [bem("close-icon", props2.closeIconPosition), constant.HAPTICS_FEEDBACK],
          "classPrefix": props2.iconPrefix,
          "onClick": onClickCloseIcon
        }, null);
      }
    };
    let timer;
    const onOpened = () => {
      if (timer) clearTimeout(timer);
      timer = setTimeout(() => {
        emit("opened");
      });
    };
    const onClosed = () => emit("closed");
    const onKeydown = (event) => emit("keydown", event);
    const renderPopup = lazyRender(() => {
      var _a;
      const {
        destroyOnClose,
        round,
        position,
        safeAreaInsetTop,
        safeAreaInsetBottom,
        show
      } = props2;
      if (!show && destroyOnClose) {
        return;
      }
      return Vue.withDirectives(Vue.createVNode("div", Vue.mergeProps({
        "ref": popupRef,
        "style": style.value,
        "role": "dialog",
        "tabindex": 0,
        "class": [bem({
          round,
          [position]: position
        }), {
          "van-safe-area-top": safeAreaInsetTop,
          "van-safe-area-bottom": safeAreaInsetBottom
        }],
        "onKeydown": onKeydown
      }, attrs, useScopeId.useScopeId()), [(_a = slots.default) == null ? void 0 : _a.call(slots), renderCloseIcon()]), [[Vue.vShow, show]]);
    });
    const renderTransition = () => {
      const {
        position,
        transition,
        transitionAppear
      } = props2;
      const name2 = position === "center" ? "van-fade" : `van-popup-slide-${position}`;
      return Vue.createVNode(Vue.Transition, {
        "name": transition || name2,
        "appear": transitionAppear,
        "onAfterEnter": onOpened,
        "onAfterLeave": onClosed
      }, {
        default: renderPopup
      });
    };
    Vue.watch(() => props2.show, (show) => {
      if (show && !opened) {
        open();
        if (attrs.tabindex === 0) {
          Vue.nextTick(() => {
            var _a;
            (_a = popupRef.value) == null ? void 0 : _a.focus();
          });
        }
      }
      if (!show && opened) {
        opened = false;
        emit("close");
      }
    });
    useExpose.useExpose({
      popupRef
    });
    useLockScroll.useLockScroll(popupRef, () => props2.show && props2.lockScroll);
    index_esm.useEventListener("popstate", () => {
      if (props2.closeOnPopstate) {
        close();
        shouldReopen = false;
      }
    });
    Vue.onMounted(() => {
      if (props2.show) {
        open();
      }
    });
    Vue.onActivated(() => {
      if (shouldReopen) {
        emit("update:show", true);
        shouldReopen = false;
      }
    });
    Vue.onDeactivated(() => {
      if (props2.show && props2.teleport) {
        close();
        shouldReopen = true;
      }
    });
    Vue.provide(onPopupReopen.POPUP_TOGGLE_KEY, () => props2.show);
    return () => {
      if (props2.teleport) {
        return Vue.createVNode(Vue.Teleport, {
          "to": props2.teleport
        }, {
          default: () => [renderOverlay(), renderTransition()]
        });
      }
      return Vue.createVNode(Vue.Fragment, null, [renderOverlay(), renderTransition()]);
    };
  }
});
exports.default = stdin_default;
exports.popupProps = popupProps;
