import { defineComponent, ref, provide, computed, openBlock, createBlock, Teleport, createVNode, Transition, normalizeClass, unref, withCtx, withDirectives, createElementVNode, normalizeStyle, mergeProps, renderSlot, createElementBlock, createCommentVNode, vShow } from 'vue';
import { debounce } from 'lodash';
import { ElOverlay } from '../../overlay/index.mjs';
import '../../../hooks/index.mjs';
import '../../../tokens/index.mjs';
import '../../focus-trap/index.mjs';
import '../../../utils/index.mjs';
import ElDialogContent from './dialog-content2.mjs';
import { dialogProps, dialogEmits } from './dialog.mjs';
import { useDialog } from './use-dialog.mjs';
import _export_sfc from '../../../../_virtual/plugin-vue_export-helper.mjs';
import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
import { dialogInjectionKey } from '../../../tokens/dialog.mjs';
import { useSameTarget } from '../../../hooks/use-same-target/index.mjs';
import { useLocale } from '../../../hooks/use-locale/index.mjs';
import { addUnit } from '../../../utils/dom/style.mjs';
import ElFocusTrap from '../../focus-trap/src/focus-trap.mjs';

const _hoisted_1 = ["aria-label", "aria-labelledby", "aria-describedby"];
const __default__ = defineComponent({
  name: "ElDialog",
  inheritAttrs: false
});
const _sfc_main = /* @__PURE__ */ defineComponent({
  ...__default__,
  props: dialogProps,
  emits: dialogEmits,
  setup(__props, { expose }) {
    const props = __props;
    const ns = useNamespace("dialog");
    const dialogRef = ref();
    const headerRef = ref();
    const dialogContentRef = ref();
    const {
      visible,
      titleId,
      bodyId,
      style,
      overlayDialogStyle,
      rendered,
      zIndex,
      afterEnter,
      afterLeave,
      beforeLeave,
      handleClose,
      onModalClick,
      handleSubmit,
      onOpenAutoFocus,
      onCloseAutoFocus,
      onCloseRequested,
      onFocusoutPrevented
    } = useDialog(props, dialogRef);
    provide(dialogInjectionKey, {
      dialogRef,
      headerRef,
      bodyId,
      ns,
      rendered,
      style
    });
    const overlayEvent = useSameTarget(onModalClick);
    const { t } = useLocale();
    const cancelText = props.cancelText || t("el.popconfirm.cancelButtonText");
    const submitText = props.submitText || t("el.popconfirm.confirmButtonText");
    const draggable = computed(() => props.draggable && !props.fullscreen);
    const dragEl = computed(() => props.dragEl);
    const topOffest = computed(() => props.topOffest);
    const startMouseMove = debounce((directtion) => {
      const isUp = directtion === "up";
      const el = dialogRef.value;
      const rect = el.getBoundingClientRect();
      const { bottom, top, height } = rect;
      const MARGIN_TOP = -50;
      function mouseMoveTop(e) {
        if (isUp) {
          if (props.zoomMaxHeight) {
            const minY = Math.max(bottom - props.zoomMaxHeight, topOffest.value);
            if (e.clientY <= minY) {
              return;
            }
          }
          if (props.zoomMinHeight) {
            const maxY = Math.max(bottom - props.zoomMinHeight, 0);
            if (e.clientY >= maxY) {
              return;
            }
          }
          const transform = window.getComputedStyle(el).transform;
          const transformX = transform === "none" ? 0 : new WebKitCSSMatrix(transform).e;
          el.style["transform"] = `translate(${addUnit(transformX)}, ${addUnit(e.clientY + MARGIN_TOP)})`;
          el.style["height"] = `${addUnit(top + height - e.clientY)}`;
        } else {
          if (props.zoomMinHeight) {
            const minY = Math.max(top + props.zoomMinHeight, 0);
            if (e.clientY <= minY) {
              return;
            }
          }
          if (props.zoomMaxHeight) {
            const maxY = Math.max(top + props.zoomMaxHeight, 0);
            if (e.clientY >= maxY) {
              return;
            }
          }
          el.style["height"] = `${addUnit(e.clientY - top)}`;
        }
      }
      function removeDocumentMouseup() {
        el.style.userSelect = "auto";
        document.removeEventListener("mousemove", mouseMoveTop);
        document.removeEventListener("mouseup", removeDocumentMouseup);
      }
      document.addEventListener("mousemove", mouseMoveTop);
      document.addEventListener("mouseup", removeDocumentMouseup);
    }, 50, { leading: true });
    expose({
      visible,
      dialogContentRef
    });
    return (_ctx, _cache) => {
      return openBlock(), createBlock(Teleport, {
        to: "body",
        disabled: !_ctx.appendToBody
      }, [
        createVNode(Transition, {
          name: "dialog-fade",
          class: normalizeClass({
            [`${unref(ns).namespace.value}-modal-event-dialog`]: props.modalEvent
          }),
          onAfterEnter: unref(afterEnter),
          onAfterLeave: unref(afterLeave),
          onBeforeLeave: unref(beforeLeave),
          persisted: ""
        }, {
          default: withCtx(() => [
            withDirectives(createVNode(unref(ElOverlay), {
              "custom-mask-event": "",
              mask: _ctx.modal,
              "overlay-class": _ctx.modalClass,
              "z-index": unref(zIndex)
            }, {
              default: withCtx(() => [
                createElementVNode("div", {
                  role: "dialog",
                  "aria-modal": "true",
                  "aria-label": _ctx.title || void 0,
                  "aria-labelledby": !_ctx.title ? unref(titleId) : void 0,
                  "aria-describedby": unref(bodyId),
                  class: normalizeClass({
                    [`${unref(ns).namespace.value}-overlay-dialog`]: true,
                    "is-resize": props.zoomY,
                    "is-modal-event": props.modalEvent
                  }),
                  style: normalizeStyle(unref(overlayDialogStyle)),
                  onClick: _cache[2] || (_cache[2] = (...args) => unref(overlayEvent).onClick && unref(overlayEvent).onClick(...args)),
                  onMousedown: _cache[3] || (_cache[3] = (...args) => unref(overlayEvent).onMousedown && unref(overlayEvent).onMousedown(...args)),
                  onMouseup: _cache[4] || (_cache[4] = (...args) => unref(overlayEvent).onMouseup && unref(overlayEvent).onMouseup(...args))
                }, [
                  createVNode(unref(ElFocusTrap), {
                    loop: "",
                    trapped: unref(visible),
                    "focus-start-el": "container",
                    onFocusAfterTrapped: unref(onOpenAutoFocus),
                    onFocusAfterReleased: unref(onCloseAutoFocus),
                    onFocusoutPrevented: unref(onFocusoutPrevented),
                    onReleaseRequested: unref(onCloseRequested)
                  }, {
                    default: withCtx(() => [
                      unref(rendered) ? (openBlock(), createBlock(ElDialogContent, mergeProps({
                        key: 0,
                        ref_key: "dialogContentRef",
                        ref: dialogContentRef
                      }, _ctx.$attrs, {
                        "custom-class": _ctx.customClass,
                        center: _ctx.center,
                        "align-center": _ctx.alignCenter,
                        "close-icon": _ctx.closeIcon,
                        draggable: unref(draggable),
                        fullscreen: _ctx.fullscreen,
                        "show-close": _ctx.showClose,
                        "cancel-text": unref(cancelText),
                        "submit-text": unref(submitText),
                        style: unref(style),
                        size: _ctx.size,
                        title: _ctx.title,
                        "is-show-footer": _ctx.isShowFooter,
                        "is-show-header": _ctx.isShowHeader,
                        "drag-el": unref(dragEl),
                        "default-padding": _ctx.defaultPadding,
                        "top-offest": unref(topOffest),
                        onClose: unref(handleClose),
                        onHandleSubmit: unref(handleSubmit)
                      }), {
                        header: withCtx(() => [
                          !_ctx.$slots.title ? renderSlot(_ctx.$slots, "header", {
                            key: 0,
                            close: unref(handleClose),
                            titleId: unref(titleId),
                            titleClass: unref(ns).e("title")
                          }) : renderSlot(_ctx.$slots, "title", { key: 1 })
                        ]),
                        "body-prefix": withCtx(() => [
                          renderSlot(_ctx.$slots, "body-prefix")
                        ]),
                        "body-suffix": withCtx(() => [
                          renderSlot(_ctx.$slots, "body-suffix")
                        ]),
                        footer: withCtx(() => [
                          renderSlot(_ctx.$slots, "footer")
                        ]),
                        default: withCtx(() => [
                          props.zoomY ? (openBlock(), createElementBlock("div", {
                            key: 0,
                            class: normalizeClass(`${unref(ns).namespace.value}-dialog__resize-vertical-up`),
                            onMousedown: _cache[0] || (_cache[0] = ($event) => unref(startMouseMove)("up"))
                          }, null, 34)) : createCommentVNode("v-if", true),
                          props.zoomY ? (openBlock(), createElementBlock("div", {
                            key: 1,
                            class: normalizeClass(`${unref(ns).namespace.value}-dialog__resize-vertical-down`),
                            onMousedown: _cache[1] || (_cache[1] = ($event) => unref(startMouseMove)("down"))
                          }, null, 34)) : createCommentVNode("v-if", true),
                          renderSlot(_ctx.$slots, "default")
                        ]),
                        _: 3
                      }, 16, ["custom-class", "center", "align-center", "close-icon", "draggable", "fullscreen", "show-close", "cancel-text", "submit-text", "style", "size", "title", "is-show-footer", "is-show-header", "drag-el", "default-padding", "top-offest", "onClose", "onHandleSubmit"])) : createCommentVNode("v-if", true)
                    ]),
                    _: 3
                  }, 8, ["trapped", "onFocusAfterTrapped", "onFocusAfterReleased", "onFocusoutPrevented", "onReleaseRequested"])
                ], 46, _hoisted_1)
              ]),
              _: 3
            }, 8, ["mask", "overlay-class", "z-index"]), [
              [vShow, unref(visible)]
            ])
          ]),
          _: 3
        }, 8, ["class", "onAfterEnter", "onAfterLeave", "onBeforeLeave"])
      ], 8, ["disabled"]);
    };
  }
});
var Dialog = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "/home/dingdan/workspace/element-plus/packages/components/dialog/src/dialog.vue"]]);

export { Dialog as default };
//# sourceMappingURL=dialog2.mjs.map
