<template>
  <sar-overlay
    v-if="overlay"
    :visible="visible"
    :duration="duration"
    :z-index="zIndex"
    :background="background"
    :transparent="transparent"
    :root-style="overlayStyle"
    :root-class="overlayClass"
    @click="onOverlayClick"
  />
  <view
    :class="popupClass"
    :style="popupStyle"
    @transitionend="onTransitionEnd"
  >
    <slot></slot>
  </view>
</template>

<script lang="ts">
import { defineComponent as _defineComponent } from "vue";
import { computed, reactive, toRef } from "vue";
import { classNames, stringifyStyle, createBem } from "../utils";
import { useTransition, useZIndex } from "../use";
import SarOverlay from "../overlay/overlay.vue";
import { popupProps, usePopupVisibleHookProvide } from "./common";
const __default__ = {
  options: {
    virtualHost: true,
    styleIsolation: "shared"
  }
};
export default /* @__PURE__ */ _defineComponent({
  ...__default__,
  __name: "popup",
  props: popupProps,
  emits: [
    "overlay-click",
    "before-enter",
    "enter",
    "after-enter",
    "enter-cancelled",
    "before-leave",
    "leave",
    "after-leave",
    "leave-cancelled",
    "visible-hook"
  ],
  setup(__props, { expose: __expose, emit: __emit }) {
    __expose();
    const props = __props;
    const emit = __emit;
    const bem = createBem("popup");
    const [zIndex, increaseZIndex] = useZIndex();
    const callVisibleHook = usePopupVisibleHookProvide();
    const onVisibleHook = (name) => {
      callVisibleHook(name);
      emit("visible-hook", name);
      emit(name);
      if (name === "before-enter") {
        increaseZIndex();
      }
    };
    const { realVisible, transitionClass, onTransitionEnd } = useTransition(
      reactive({
        visible: toRef(props, "visible"),
        duration: toRef(props, "duration"),
        prefix: computed(() => bem.m(props.effect) + "-"),
        onVisibleHook
      })
    );
    const onOverlayClick = (event) => {
      emit("overlay-click", event);
    };
    const popupClass = computed(() => {
      return classNames(
        bem.b(),
        bem.m(props.effect),
        props.rootClass,
        transitionClass.value
      );
    });
    const popupStyle = computed(() => {
      return stringifyStyle(props.rootStyle, {
        zIndex: zIndex.value,
        display: realVisible.value ? "flex" : "none",
        transitionDuration: props.duration + "ms"
      });
    });
    const __returned__ = { props, emit, bem, zIndex, increaseZIndex, callVisibleHook, onVisibleHook, realVisible, transitionClass, onTransitionEnd, onOverlayClick, popupClass, popupStyle, SarOverlay };
    return __returned__;
  }
});
</script>

<style lang="scss">
@use './index.scss';
</style>