'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
var Content = require('./Content.cjs');
var IDialogPropTypes = require('./IDialogPropTypes.cjs');
var Mask = require('./Mask.cjs');
var util = require('./util.cjs');
var initDefaultProps = require('../../../utils/vue/initDefaultProps.cjs');
var contains = require('../../../utils/dom/contains.cjs');
var KeyCode = require('../../../utils/KeyCode.cjs');
var pickAttrs = require('../../../utils/lang/pickAttrs.cjs');
var _class = require('../../../utils/dom/class.cjs');
var omit = require('../../../utils/lang/omit.cjs');

var Dialog = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "VcDialog",
  inheritAttrs: false,
  props: initDefaultProps.initDefaultProps({
    ...IDialogPropTypes.dialogPropTypes(),
    getOpenCount: Function,
    scrollLocker: Object
  }, {
    mask: true,
    visible: false,
    keyboard: true,
    closable: true,
    maskClosable: true,
    destroyOnClose: false,
    prefixCls: "rc-dialog",
    getOpenCount: () => null,
    focusTriggerAfterClose: true
  }),
  setup(props, {
    attrs,
    slots
  }) {
    const lastOutSideActiveElementRef = vue.shallowRef();
    const wrapperRef = vue.shallowRef();
    const contentRef = vue.shallowRef();
    const animatedVisible = vue.shallowRef(props.visible);
    const ariaIdRef = vue.shallowRef(`vcDialogTitle${util.getUUID()}`);
    const onDialogVisibleChanged = (newVisible) => {
      var _a, _b;
      if (newVisible) {
        if (!contains.contains(wrapperRef.value, document.activeElement)) {
          lastOutSideActiveElementRef.value = document.activeElement;
          (_a = contentRef.value) == null ? void 0 : _a.focus();
        }
      } else {
        const preAnimatedVisible = animatedVisible.value;
        animatedVisible.value = false;
        if (props.mask && lastOutSideActiveElementRef.value && props.focusTriggerAfterClose) {
          try {
            lastOutSideActiveElementRef.value.focus({
              preventScroll: true
            });
          } catch (e) {
          }
          lastOutSideActiveElementRef.value = null;
        }
        if (preAnimatedVisible)
          (_b = props.afterClose) == null ? void 0 : _b.call(props);
      }
    };
    const onInternalClose = (e) => {
      var _a;
      (_a = props.onClose) == null ? void 0 : _a.call(props, e);
    };
    const contentClickRef = vue.shallowRef(false);
    const contentTimeoutRef = vue.shallowRef();
    const onContentMouseDown = () => {
      clearTimeout(contentTimeoutRef.value);
      contentClickRef.value = true;
    };
    const onContentMouseUp = () => {
      contentTimeoutRef.value = setTimeout(() => {
        contentClickRef.value = false;
      });
    };
    const onWrapperClick = (e) => {
      if (!props.maskClosable)
        return null;
      if (contentClickRef.value)
        contentClickRef.value = false;
      else if (wrapperRef.value === e.target)
        onInternalClose(e);
    };
    const onWrapperKeyDown = (e) => {
      if (props.keyboard && e.keyCode === KeyCode.KeyCode.ESC) {
        e.stopPropagation();
        onInternalClose(e);
        return;
      }
      if (props.visible) {
        if (e.keyCode === KeyCode.KeyCode.TAB)
          contentRef.value.changeActive(!e.shiftKey);
      }
    };
    vue.watch(() => props.visible, () => {
      if (props.visible)
        animatedVisible.value = true;
    }, {
      flush: "post"
    });
    vue.onBeforeUnmount(() => {
      var _a;
      clearTimeout(contentTimeoutRef.value);
      (_a = props.scrollLocker) == null ? void 0 : _a.unLock();
    });
    vue.watchEffect(() => {
      var _a, _b;
      (_a = props.scrollLocker) == null ? void 0 : _a.unLock();
      if (animatedVisible.value)
        (_b = props.scrollLocker) == null ? void 0 : _b.lock();
    });
    return () => {
      const {
        prefixCls,
        mask,
        visible,
        maskTransitionName,
        maskAnimation,
        zIndex,
        wrapClassName,
        rootClassName,
        wrapStyle,
        closable,
        maskProps,
        maskStyle,
        transitionName,
        animation,
        wrapProps,
        title = slots.title
      } = props;
      const {
        style,
        class: className
      } = attrs;
      return vue.createVNode("div", {
        "class": [`${prefixCls}-root`, rootClassName],
        ...pickAttrs.pickAttrs(props, {
          data: true
        })
      }, [vue.createVNode(Mask.default, {
        "prefixCls": prefixCls,
        "visible": mask && visible,
        "motionName": util.getMotionName(prefixCls, maskTransitionName, maskAnimation),
        "style": {
          zIndex,
          ...maskStyle
        },
        "maskProps": maskProps
      }, null), vue.createVNode("div", {
        "tabIndex": -1,
        "onKeydown": onWrapperKeyDown,
        "class": _class.classNames(`${prefixCls}-wrap`, wrapClassName),
        "ref": wrapperRef,
        "onClick": onWrapperClick,
        "role": "dialog",
        "aria-labelledby": title ? ariaIdRef.value : null,
        "style": {
          zIndex,
          ...wrapStyle,
          display: !animatedVisible.value ? "none" : null
        },
        ...wrapProps
      }, [vue.createVNode(Content.default, {
        ...omit.omit(props, ["scrollLocker"]),
        "style": style,
        "class": className,
        "onMousedown": onContentMouseDown,
        "onMouseup": onContentMouseUp,
        "ref": contentRef,
        "closable": closable,
        "ariaId": ariaIdRef.value,
        "prefixCls": prefixCls,
        "visible": visible,
        "onClose": onInternalClose,
        "onVisibleChanged": onDialogVisibleChanged,
        "motionName": util.getMotionName(prefixCls, transitionName, animation)
      }, slots)])]);
    };
  }
});

exports.default = Dialog;
//# sourceMappingURL=Dialog.cjs.map
