"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const shared_esmBundler = require("../../../../../@vue/shared/dist/shared.esm-bundler.js");
const icon = require("../../../utils/vue/icon.js");
const validator = require("../../../utils/vue/validator.js");
const index$8 = require("../../../hooks/use-draggable/index.js");
const index$9 = require("../../../hooks/use-lockscreen/index.js");
const index$a = require("../../../hooks/use-same-target/index.js");
const index$7 = require("../../../hooks/use-id/index.js");
const index = require("../../icon/index.js");
const index$3 = require("../../focus-trap/index.js");
const index$2 = require("../../input/index.js");
const useGlobalConfig = require("../../config-provider/src/hooks/use-global-config.js");
const index$4 = require("../../button/index.js");
const index$5 = require("../../../directives/trap-focus/index.js");
const index$1 = require("../../overlay/index.js");
const index$6 = require("../../../../../@element-plus/icons-vue/dist/index.js");
const vue = require("vue");
var index_vue_vue_type_script_lang_default = vue.defineComponent({
  name: "ElMessageBox",
  directives: { TrapFocus: index$5.default },
  components: {
    ElButton: index$4.ElButton,
    ElFocusTrap: index$3.default,
    ElInput: index$2.ElInput,
    ElOverlay: index$1.ElOverlay,
    ElIcon: index.ElIcon,
    ...icon.TypeComponents
  },
  inheritAttrs: false,
  props: {
    buttonSize: {
      type: String,
      validator: validator.isValidComponentSize
    },
    modal: {
      type: Boolean,
      default: true
    },
    lockScroll: {
      type: Boolean,
      default: true
    },
    showClose: {
      type: Boolean,
      default: true
    },
    closeOnClickModal: {
      type: Boolean,
      default: true
    },
    closeOnPressEscape: {
      type: Boolean,
      default: true
    },
    closeOnHashChange: {
      type: Boolean,
      default: true
    },
    center: Boolean,
    draggable: Boolean,
    overflow: Boolean,
    roundButton: Boolean,
    container: {
      type: String,
      default: "body"
    },
    boxType: {
      type: String,
      default: ""
    }
  },
  emits: ["vanish", "action"],
  setup(props, { emit }) {
    const { locale, zIndex, ns, size: btnSize } = useGlobalConfig.useGlobalComponentSettings("message-box", vue.computed(() => props.buttonSize));
    const { t } = locale;
    const { nextZIndex } = zIndex;
    const visible = vue.ref(false);
    const state = vue.reactive({
      autofocus: true,
      beforeClose: null,
      callback: null,
      cancelButtonText: "",
      cancelButtonClass: "",
      confirmButtonText: "",
      confirmButtonClass: "",
      cancelButtonType: "",
      confirmButtonType: "primary",
      customClass: "",
      customStyle: {},
      dangerouslyUseHTMLString: false,
      distinguishCancelAndClose: false,
      icon: "",
      closeIcon: "",
      inputPattern: null,
      inputPlaceholder: "",
      inputType: "text",
      inputValue: "",
      inputValidator: void 0,
      inputErrorMessage: "",
      message: "",
      modalFade: true,
      modalClass: "",
      showCancelButton: false,
      showConfirmButton: true,
      type: "",
      title: void 0,
      showInput: false,
      action: "",
      confirmButtonLoading: false,
      cancelButtonLoading: false,
      confirmButtonLoadingIcon: vue.markRaw(index$6.Loading),
      cancelButtonLoadingIcon: vue.markRaw(index$6.Loading),
      confirmButtonDisabled: false,
      editorErrorMessage: "",
      validateError: false,
      zIndex: nextZIndex()
    });
    const typeClass = vue.computed(() => {
      const type = state.type;
      return { [ns.bm("icon", type)]: type && icon.TypeComponentsMap[type] };
    });
    const contentId = index$7.useId();
    const inputId = index$7.useId();
    const iconComponent = vue.computed(() => {
      const type = state.type;
      return state.icon || type && icon.TypeComponentsMap[type] || "";
    });
    const hasMessage = vue.computed(() => !!state.message);
    const rootRef = vue.ref();
    const headerRef = vue.ref();
    const focusStartRef = vue.ref();
    const inputRef = vue.ref();
    const confirmRef = vue.ref();
    const confirmButtonClasses = vue.computed(() => state.confirmButtonClass);
    vue.watch(() => state.inputValue, async (val) => {
      await vue.nextTick();
      if (props.boxType === "prompt" && val) validate();
    }, { immediate: true });
    vue.watch(() => visible.value, (val) => {
      var _a;
      if (val) {
        if (props.boxType !== "prompt") if (state.autofocus) focusStartRef.value = ((_a = confirmRef.value) == null ? void 0 : _a.$el) ?? rootRef.value;
        else focusStartRef.value = rootRef.value;
        state.zIndex = nextZIndex();
      }
      if (props.boxType !== "prompt") return;
      if (val) vue.nextTick().then(() => {
        if (inputRef.value && inputRef.value.$el) if (state.autofocus) focusStartRef.value = getInputElement() ?? rootRef.value;
        else focusStartRef.value = rootRef.value;
      });
      else {
        state.editorErrorMessage = "";
        state.validateError = false;
      }
    });
    const { isDragging } = index$8.useDraggable(rootRef, headerRef, vue.computed(() => props.draggable), vue.computed(() => props.overflow));
    vue.onMounted(async () => {
      await vue.nextTick();
      if (props.closeOnHashChange) window.addEventListener("hashchange", doClose);
    });
    vue.onBeforeUnmount(() => {
      if (props.closeOnHashChange) window.removeEventListener("hashchange", doClose);
    });
    function doClose() {
      if (!visible.value) return;
      visible.value = false;
      vue.nextTick(() => {
        if (state.action) emit("action", state.action);
      });
    }
    const handleWrapperClick = () => {
      if (props.closeOnClickModal) handleAction(state.distinguishCancelAndClose ? "close" : "cancel");
    };
    const overlayEvent = index$a.useSameTarget(handleWrapperClick);
    const handleInputEnter = (e) => {
      var _a;
      if (state.inputType !== "textarea" && !((_a = inputRef.value) == null ? void 0 : _a.isComposing)) {
        e.preventDefault();
        return handleAction("confirm");
      }
    };
    const handleAction = (action) => {
      var _a;
      if (props.boxType === "prompt" && action === "confirm" && !validate()) return;
      state.action = action;
      if (state.beforeClose) (_a = state.beforeClose) == null ? void 0 : _a.call(state, action, state, doClose);
      else doClose();
    };
    const validate = () => {
      if (props.boxType === "prompt") {
        const inputPattern = state.inputPattern;
        if (inputPattern && !inputPattern.test(state.inputValue || "")) {
          state.editorErrorMessage = state.inputErrorMessage || t("el.messagebox.error");
          state.validateError = true;
          return false;
        }
        const inputValidator = state.inputValidator;
        if (shared_esmBundler.isFunction(inputValidator)) {
          const validateResult = inputValidator(state.inputValue);
          if (validateResult === false) {
            state.editorErrorMessage = state.inputErrorMessage || t("el.messagebox.error");
            state.validateError = true;
            return false;
          }
          if (shared_esmBundler.isString(validateResult)) {
            state.editorErrorMessage = validateResult;
            state.validateError = true;
            return false;
          }
        }
      }
      state.editorErrorMessage = "";
      state.validateError = false;
      return true;
    };
    const getInputElement = () => {
      var _a;
      const inputRefs = (_a = inputRef.value) == null ? void 0 : _a.$refs;
      return (inputRefs == null ? void 0 : inputRefs.input) ?? (inputRefs == null ? void 0 : inputRefs.textarea);
    };
    const handleClose = () => {
      handleAction("close");
    };
    const onCloseRequested = () => {
      if (props.closeOnPressEscape) handleClose();
    };
    if (props.lockScroll) index$9.useLockscreen(visible, { ns });
    return {
      ...vue.toRefs(state),
      ns,
      overlayEvent,
      visible,
      hasMessage,
      typeClass,
      contentId,
      inputId,
      btnSize,
      iconComponent,
      confirmButtonClasses,
      rootRef,
      focusStartRef,
      headerRef,
      inputRef,
      isDragging,
      confirmRef,
      doClose,
      handleClose,
      onCloseRequested,
      handleWrapperClick,
      handleInputEnter,
      handleAction,
      t
    };
  }
});
exports.default = index_vue_vue_type_script_lang_default;
