/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var iconsVueNext = require('@ldesign/icons-vue-next');
var hooks_useGlobalIcon = require('../hooks/useGlobalIcon.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var hooks_tnode = require('../hooks/tnode.js');
var hooks_useDestroyOnClose = require('../hooks/useDestroyOnClose.js');
var _common_js_utils_getScrollbarWidth = require('../_common/js/utils/getScrollbarWidth.js');
var hooks_useTeleport = require('../hooks/useTeleport.js');
var dialog_stack = require('./stack.js');
var dialog_hooks = require('./hooks.js');
var dialog_props = require('./props.js');
var configProvider_useConfig = require('../config-provider/useConfig.js');
require('lodash/isFunction');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isString');
require('lodash/isObject');
require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/isNull');
require('lodash/isUndefined');
require('lodash/isNumber');
require('lodash/isArray');
require('../utils/dom.js');
require('../_chunks/dep-7f0559c3.js');
require('../utils/easing.js');
require('@babel/runtime/helpers/typeof');
require('lodash/omit');
require('../button/index.js');
require('../utils/withInstall.js');
require('../button/button.js');
require('../loading/index.js');
require('../loading/directive.js');
require('../loading/plugin.js');
require('../loading/loading.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../_common/js/utils/set-style.js');
require('lodash/cloneDeep');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('dayjs');
require('../hooks/useRipple.js');
require('../utils/set-style.js');
require('../hooks/useKeepAnimation.js');
require('../form/hooks.js');
require('../button/props.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);

function GetCSSValue(v) {
  return Number.isNaN(Number(v)) ? v : "".concat(Number(v), "px");
}
var mousePosition;
var getClickPosition = function getClickPosition(e) {
  mousePosition = {
    x: e.clientX,
    y: e.clientY
  };
  setTimeout(function () {
    mousePosition = null;
  }, 100);
};
if (typeof window !== "undefined" && window.document && window.document.documentElement) document.documentElement.addEventListener("click", getClickPosition, true);
function InitDragEvent(dragBox) {
  var target = dragBox;
  var windowInnerWidth = window.innerWidth || document.documentElement.clientWidth;
  var windowInnerHeight = window.innerHeight || document.documentElement.clientHeight;
  target.addEventListener("mousedown", function (targetEvent) {
    var disX = targetEvent.clientX - target.offsetLeft;
    var disY = targetEvent.clientY - target.offsetTop;
    var dialogW = target.offsetWidth;
    var dialogH = target.offsetHeight;
    if (dialogW > windowInnerWidth || dialogH > windowInnerHeight) return;
    function mouseMoverHandler(documentEvent) {
      var left = documentEvent.clientX - disX;
      var top = documentEvent.clientY - disY;
      if (left < 0) left = 0;
      if (top < 0) top = 0;
      if (windowInnerWidth - target.offsetWidth - left < 0) left = windowInnerWidth - target.offsetWidth;
      if (windowInnerHeight - target.offsetHeight - top < 0) top = windowInnerHeight - target.offsetHeight;
      target.style.position = "absolute";
      target.style.left = "".concat(left, "px");
      target.style.top = "".concat(top, "px");
    }
    function mouseUpHandler() {
      document.removeEventListener("mousemove", mouseMoverHandler);
      document.removeEventListener("mouseup", mouseUpHandler);
    }
    document.addEventListener("mousemove", mouseMoverHandler);
    document.addEventListener("mouseup", mouseUpHandler);
    document.addEventListener("dragend", mouseUpHandler);
  });
}
var key = 1;
var _Dialog = vue.defineComponent({
  name: "TDialog",
  directives: {
    draggable: function draggable(el, binding) {
      if (el && binding && binding.value) InitDragEvent(el);
    }
  },
  inheritAttrs: false,
  props: dialog_props["default"],
  emits: ["update:visible"],
  setup: function setup(props2, context) {
    var _this = this;
    var COMPONENT_NAME = hooks_useConfig.usePrefixClass("dialog");
    var classPrefix = hooks_useConfig.usePrefixClass();
    var renderContent = hooks_tnode.useContent();
    var renderTNodeJSX = hooks_tnode.useTNodeJSX();
    var dialogEle = vue.ref(null);
    var _useConfig = configProvider_useConfig.useConfig("dialog"),
      globalConfig = _useConfig.globalConfig;
    var _useGlobalIcon = hooks_useGlobalIcon.useGlobalIcon({
        CloseIcon: iconsVueNext.CloseIcon,
        InfoCircleFilledIcon: iconsVueNext.InfoCircleFilledIcon,
        CheckCircleFilledIcon: iconsVueNext.CheckCircleFilledIcon,
        ErrorCircleFilledIcon: iconsVueNext.ErrorCircleFilledIcon
      }),
      CloseIcon = _useGlobalIcon.CloseIcon,
      InfoCircleFilledIcon = _useGlobalIcon.InfoCircleFilledIcon,
      CheckCircleFilledIcon = _useGlobalIcon.CheckCircleFilledIcon,
      ErrorCircleFilledIcon = _useGlobalIcon.ErrorCircleFilledIcon;
    var confirmBtnAction = function confirmBtnAction(e) {
      var _props2$onConfirm;
      (_props2$onConfirm = props2.onConfirm) === null || _props2$onConfirm === void 0 ? void 0 : _props2$onConfirm.call(props2, {
        e: e
      });
    };
    var cancelBtnAction = function cancelBtnAction(e) {
      var _props2$onCancel;
      (_props2$onCancel = props2.onCancel) === null || _props2$onCancel === void 0 ? void 0 : _props2$onCancel.call(props2, {
        e: e
      });
      emitCloseEvent({
        e: e,
        trigger: "cancel"
      });
    };
    var _useAction = dialog_hooks.useAction({
        confirmBtnAction: confirmBtnAction,
        cancelBtnAction: cancelBtnAction
      }),
      getConfirmBtn = _useAction.getConfirmBtn,
      getCancelBtn = _useAction.getCancelBtn;
    var teleportElement = hooks_useTeleport["default"](function () {
      return props2.attach;
    });
    hooks_useDestroyOnClose["default"]();
    var timer = vue.ref();
    var styleEl = vue.ref();
    var isModal = vue.computed(function () {
      return props2.mode === "modal";
    });
    var isModeLess = vue.computed(function () {
      return props2.mode === "modeless";
    });
    var isNormal = vue.computed(function () {
      return props2.mode === "normal";
    });
    var isFullScreen = vue.computed(function () {
      return props2.mode === "full-screen";
    });
    var maskClass = vue.computed(function () {
      return ["".concat(COMPONENT_NAME.value, "__mask"), !props2.showOverlay && "".concat(classPrefix.value, "-is-hidden")];
    });
    var positionClass = vue.computed(function () {
      if (isNormal.value) return [];
      if (isFullScreen.value) return ["".concat(COMPONENT_NAME.value, "__position_fullscreen")];
      return ["".concat(COMPONENT_NAME.value, "__position"), !!props2.top && "".concat(COMPONENT_NAME.value, "--top"), "".concat(props2.placement && !props2.top ? "".concat(COMPONENT_NAME.value, "--").concat(props2.placement) : "")];
    });
    var wrapClass = vue.computed(function () {
      return [!isNormal.value && "".concat(COMPONENT_NAME.value, "__wrap")];
    });
    var positionStyle = vue.computed(function () {
      if (isFullScreen.value) return {};
      var top = props2.top;
      var topStyle = {};
      if (top !== void 0) {
        var topValue = GetCSSValue(top);
        topStyle = {
          paddingTop: topValue
        };
      }
      return topStyle;
    });
    var dialogClass = vue.computed(function () {
      var dialogClass2 = ["".concat(COMPONENT_NAME.value), "".concat(COMPONENT_NAME.value, "__modal-").concat(props2.theme), isModeLess.value && props2.draggable && "".concat(COMPONENT_NAME.value, "--draggable")];
      if (isFullScreen.value) dialogClass2.push("".concat(COMPONENT_NAME.value, "__fullscreen"));else dialogClass2.push.apply(dialogClass2, ["".concat(COMPONENT_NAME.value, "--default"), "".concat(COMPONENT_NAME.value, "--").concat(props2.placement)]);
      return dialogClass2;
    });
    var dialogStyle = vue.computed(function () {
      return !isFullScreen.value ? {
        width: GetCSSValue(props2.width)
      } : {};
    });
    vue.watch(function () {
      return props2.visible;
    }, function (value) {
      if (value) {
        var _document$activeEleme;
        if (isModal.value && !props2.showInAttachedElement || isFullScreen.value) {
          if (props2.preventScrollThrough) document.head.appendChild(styleEl.value);
          vue.nextTick(function () {
            if (mousePosition && dialogEle.value) {
              dialogEle.value.style.transformOrigin = "".concat(mousePosition.x - dialogEle.value.offsetLeft, "px ").concat(mousePosition.y - dialogEle.value.offsetTop, "px");
            }
          });
        }
        (_document$activeEleme = document.activeElement) === null || _document$activeEleme === void 0 ? void 0 : _document$activeEleme.blur();
      } else {
        clearStyleFunc();
      }
      storeUid(value);
      addKeyboardEvent(value);
    });
    function destroySelf() {
      var _styleEl$value$parent, _styleEl$value$parent2;
      (_styleEl$value$parent = styleEl.value.parentNode) === null || _styleEl$value$parent === void 0 ? void 0 : (_styleEl$value$parent2 = _styleEl$value$parent.removeChild) === null || _styleEl$value$parent2 === void 0 ? void 0 : _styleEl$value$parent2.call(_styleEl$value$parent, styleEl.value);
    }
    function clearStyleFunc() {
      clearTimeout(timer.value);
      timer.value = setTimeout(function () {
        destroySelf();
      }, 150);
    }
    var instance = vue.getCurrentInstance();
    var storeUid = function storeUid(flag) {
      if (flag) dialog_stack.stack.push(instance.uid);else dialog_stack.stack.pop();
    };
    var addKeyboardEvent = function addKeyboardEvent(status) {
      if (status) {
        document.addEventListener("keydown", keyboardEvent);
        props2.confirmOnEnter && document.addEventListener("keydown", keyboardEnterEvent);
      } else {
        document.removeEventListener("keydown", keyboardEvent);
        props2.confirmOnEnter && document.removeEventListener("keydown", keyboardEnterEvent);
      }
    };
    var keyboardEnterEvent = function keyboardEnterEvent(e) {
      var _props2$onConfirm2;
      var code = e.code;
      if ((code === "Enter" || code === "NumpadEnter") && dialog_stack.stack.top === instance.uid) (_props2$onConfirm2 = props2.onConfirm) === null || _props2$onConfirm2 === void 0 ? void 0 : _props2$onConfirm2.call(props2, {
        e: e
      });
    };
    var keyboardEvent = function keyboardEvent(e) {
      if (e.code === "Escape" && dialog_stack.stack.top === instance.uid) {
        var _props2$onEscKeydown, _props2$closeOnEscKey;
        (_props2$onEscKeydown = props2.onEscKeydown) === null || _props2$onEscKeydown === void 0 ? void 0 : _props2$onEscKeydown.call(props2, {
          e: e
        });
        if ((_props2$closeOnEscKey = props2.closeOnEscKeydown) !== null && _props2$closeOnEscKey !== void 0 ? _props2$closeOnEscKey : globalConfig.value.closeOnEscKeydown) emitCloseEvent({
          e: e,
          trigger: "esc"
        });
      }
    };
    var overlayAction = function overlayAction(e) {
      var _props2$closeOnOverla;
      if (props2.showOverlay && ((_props2$closeOnOverla = props2.closeOnOverlayClick) !== null && _props2$closeOnOverla !== void 0 ? _props2$closeOnOverla : globalConfig.value.closeOnOverlayClick)) {
        var _props2$onOverlayClic;
        (_props2$onOverlayClic = props2.onOverlayClick) === null || _props2$onOverlayClic === void 0 ? void 0 : _props2$onOverlayClic.call(props2, {
          e: e
        });
        emitCloseEvent({
          e: e,
          trigger: "overlay"
        });
      }
    };
    var _useSameTarget = dialog_hooks.useSameTarget(overlayAction),
      onClick = _useSameTarget.onClick,
      onMousedown = _useSameTarget.onMousedown,
      onMouseup = _useSameTarget.onMouseup;
    var closeBtnAction = function closeBtnAction(e) {
      var _props2$onCloseBtnCli;
      (_props2$onCloseBtnCli = props2.onCloseBtnClick) === null || _props2$onCloseBtnCli === void 0 ? void 0 : _props2$onCloseBtnCli.call(props2, {
        e: e
      });
      emitCloseEvent({
        trigger: "close-btn",
        e: e
      });
    };
    var afterEnter = function afterEnter() {
      var _props2$onOpened;
      (_props2$onOpened = props2.onOpened) === null || _props2$onOpened === void 0 ? void 0 : _props2$onOpened.call(props2);
    };
    var afterLeave = function afterLeave() {
      var _props2$onClosed;
      if (isModeLess.value && props2.draggable && dialogEle.value) {
        dialogEle.value.style.position = "relative";
        dialogEle.value.style.left = "unset";
        dialogEle.value.style.top = "unset";
      }
      (_props2$onClosed = props2.onClosed) === null || _props2$onClosed === void 0 ? void 0 : _props2$onClosed.call(props2);
    };
    var emitCloseEvent = function emitCloseEvent(ctx) {
      var _props2$onClose;
      (_props2$onClose = props2.onClose) === null || _props2$onClose === void 0 ? void 0 : _props2$onClose.call(props2, ctx);
      context.emit("update:visible", false);
    };
    var hasEventOn = function hasEventOn(name) {
      var _this$_events;
      var eventFuncs = (_this$_events = _this["_events"]) === null || _this$_events === void 0 ? void 0 : _this$_events[name];
      return !!(eventFuncs !== null && eventFuncs !== void 0 && eventFuncs.length);
    };
    var getIcon = function getIcon() {
      var icon = {
        info: vue.createVNode(InfoCircleFilledIcon, {
          "class": "".concat(classPrefix.value, "-is-info")
        }, null),
        warning: vue.createVNode(ErrorCircleFilledIcon, {
          "class": "".concat(classPrefix.value, "-is-warning")
        }, null),
        danger: vue.createVNode(ErrorCircleFilledIcon, {
          "class": "".concat(classPrefix.value, "-is-error")
        }, null),
        success: vue.createVNode(CheckCircleFilledIcon, {
          "class": "".concat(classPrefix.value, "-is-success")
        }, null)
      };
      return icon[props2.theme];
    };
    var renderDialog = function renderDialog() {
      var defaultHeader = vue.createVNode("h5", {
        "class": "title"
      }, null);
      var defaultCloseBtn = vue.createVNode(CloseIcon, null, null);
      var body = renderContent("default", "body");
      var defaultFooter = vue.createVNode("div", null, [getCancelBtn({
        cancelBtn: props2.cancelBtn,
        globalCancel: globalConfig.value.cancel,
        className: "".concat(COMPONENT_NAME.value, "__cancel")
      }), getConfirmBtn({
        theme: props2.theme,
        confirmBtn: props2.confirmBtn,
        globalConfirm: globalConfig.value.confirm,
        globalConfirmBtnTheme: globalConfig.value.confirmBtnTheme,
        className: "".concat(COMPONENT_NAME.value, "__confirm")
      })]);
      var headerClassName = isFullScreen.value ? ["".concat(COMPONENT_NAME.value, "__header"), "".concat(COMPONENT_NAME.value, "__header--fullscreen")] : "".concat(COMPONENT_NAME.value, "__header");
      var closeClassName = isFullScreen.value ? ["".concat(COMPONENT_NAME.value, "__close"), "".concat(COMPONENT_NAME.value, "__close--fullscreen")] : "".concat(COMPONENT_NAME.value, "__close");
      var bodyClassName = props2.theme === "default" ? ["".concat(COMPONENT_NAME.value, "__body")] : ["".concat(COMPONENT_NAME.value, "__body__icon")];
      isFullScreen.value && bodyClassName.push("".concat(COMPONENT_NAME.value, "__body--fullscreen"));
      var footerClassName = isFullScreen.value ? ["".concat(COMPONENT_NAME.value, "__footer"), "".concat(COMPONENT_NAME.value, "__footer--fullscreen")] : "".concat(COMPONENT_NAME.value, "__footer");
      var footerContent = renderTNodeJSX("footer", defaultFooter);
      var onStopDown = function onStopDown(e) {
        if (isModeLess.value && props2.draggable) e.stopPropagation();
      };
      return vue.createVNode("div", {
        "class": wrapClass.value
      }, [vue.createVNode("div", {
        "class": positionClass.value,
        "style": positionStyle.value,
        "onClick": onClick,
        "onMousedown": onMousedown,
        "onMouseup": onMouseup
      }, [vue.withDirectives(vue.createVNode("div", {
        "key": "dialog",
        "class": dialogClass.value,
        "style": dialogStyle.value,
        "ref": dialogEle
      }, [vue.createVNode("div", {
        "class": headerClassName,
        "onMousedown": onStopDown
      }, [vue.createVNode("div", {
        "class": "".concat(COMPONENT_NAME.value, "__header-content")
      }, [getIcon(), renderTNodeJSX("header", defaultHeader)]), props2.closeBtn ? vue.createVNode("span", {
        "class": closeClassName,
        "onClick": closeBtnAction
      }, [renderTNodeJSX("closeBtn", defaultCloseBtn)]) : null]), vue.createVNode("div", {
        "class": bodyClassName,
        "onMousedown": onStopDown
      }, [body]), footerContent && vue.createVNode("div", {
        "class": footerClassName,
        "onMousedown": onStopDown
      }, [footerContent])]), [[vue.resolveDirective("draggable"), isModeLess.value && props2.draggable]])])]);
    };
    vue.onMounted(function () {
      var hasScrollBar = document.documentElement.scrollHeight > document.documentElement.clientHeight;
      var scrollWidth = hasScrollBar ? _common_js_utils_getScrollbarWidth.getScrollbarWidth() : 0;
      styleEl.value = document.createElement("style");
      styleEl.value.dataset.id = "td_dialog_".concat(+new Date(), "_").concat(key += 1);
      styleEl.value.innerHTML = "\n        html body {\n          overflow-y: hidden;\n          width: calc(100% - ".concat(scrollWidth, "px);\n        }\n      ");
    });
    vue.onBeforeUnmount(function () {
      addKeyboardEvent(false);
      destroySelf();
    });
    return {
      COMPONENT_NAME: COMPONENT_NAME,
      isModal: isModal,
      isModeLess: isModeLess,
      isFullScreen: isFullScreen,
      maskClass: maskClass,
      dialogClass: dialogClass,
      dialogStyle: dialogStyle,
      dialogEle: dialogEle,
      afterEnter: afterEnter,
      afterLeave: afterLeave,
      hasEventOn: hasEventOn,
      renderDialog: renderDialog,
      teleportElement: teleportElement
    };
  },
  render: function render() {
    var _ref,
      _this2 = this;
    var COMPONENT_NAME = this.COMPONENT_NAME;
    var maskView = (this.isModal || this.isFullScreen) && vue.createVNode("div", {
      "key": "mask",
      "class": this.maskClass
    }, null);
    var dialogView = this.renderDialog();
    var view = [maskView, dialogView];
    var ctxStyle = {
      zIndex: this.zIndex
    };
    var ctxClass = ["".concat(COMPONENT_NAME, "__ctx"), (_ref = {}, _defineProperty__default["default"](_ref, "".concat(COMPONENT_NAME, "__ctx--fixed"), this.isModal || this.isFullScreen), _defineProperty__default["default"](_ref, "".concat(COMPONENT_NAME, "__ctx--absolute"), this.isModal && this.showInAttachedElement), _defineProperty__default["default"](_ref, "".concat(COMPONENT_NAME, "__ctx--modeless"), this.isModeLess), _ref)];
    return vue.createVNode(vue.Teleport, {
      "disabled": !this.attach || !this.teleportElement,
      "to": this.teleportElement
    }, {
      "default": function _default() {
        return [vue.createVNode(vue.Transition, {
          "duration": 300,
          "name": "".concat(COMPONENT_NAME, "-zoom__vue"),
          "onAfterEnter": _this2.afterEnter,
          "onAfterLeave": _this2.afterLeave
        }, {
          "default": function _default() {
            return [(!_this2.destroyOnClose || _this2.visible) && vue.withDirectives(vue.createVNode("div", vue.mergeProps({
              "class": ctxClass,
              "style": ctxStyle
            }, _this2.$attrs), [view]), [[vue.vShow, _this2.visible]])];
          }
        })];
      }
    });
  }
});

exports["default"] = _Dialog;
//# sourceMappingURL=dialog.js.map
