/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { CloseIcon, InfoCircleFilledIcon, CheckCircleFilledIcon, ErrorCircleFilledIcon } from 'tdesign-icons-vue';
import { prefix } from '../config.js';
import { Button } from '../button/index.js';
import ActionMixin from './actions.js';
import props from './props.js';
import { renderContent, renderTNodeJSX } from '../utils/render-tnode.js';
import mixins from '../utils/mixins.js';
import getConfigReceiverMixins from '../config-provider/config-receiver.js';
import TransferDom from '../utils/transfer-dom.js';
import { emitEvent } from '../utils/event.js';
import '../button/button.js';
import 'vue';
import '../utils/classnames.js';
import '../loading/index.js';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import '../utils/set-style.js';
import '../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../utils/easing.js';
import '../loading/props.js';
import '@babel/runtime/helpers/typeof';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import './style/css.js';
import '../loading/type.js';
import '../loading/plugin.js';
import '../button/props.js';
import '../utils/ripple.js';
import '../button/type.js';
import '../_chunks/dep-42d5c87e.js';
import 'lodash/isObject';
import '../config-provider/zh_CN_config.js';
import '../config-provider/type.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/camelCase';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var name = "".concat(prefix, "-dialog");

function getCSSValue(v) {
  return isNaN(Number(v)) ? v : "".concat(Number(v), "px");
}

function initDragEvent(dragBox) {
  var target = dragBox;
  target.addEventListener("mousedown", function (targetEvent) {
    var disX = targetEvent.clientX - target.offsetLeft;
    var disY = targetEvent.clientY - target.offsetTop;

    function mouseMoverHander(documentEvent) {
      var left = documentEvent.clientX - disX;
      var top = documentEvent.clientY - disY;
      target.style.left = "".concat(left, "px");
      target.style.top = "".concat(top, "px");
    }

    function mouseUpHandler() {
      document.removeEventListener("mousemove", mouseMoverHander);
      document.removeEventListener("mouseup", mouseUpHandler);
    }

    document.addEventListener("mousemove", mouseMoverHander);
    document.addEventListener("mouseup", mouseUpHandler);
    document.addEventListener("dragend", mouseUpHandler);
  });
}

var _Dialog = mixins(ActionMixin, getConfigReceiverMixins("dialog")).extend({
  name: "TDialog",
  components: {
    TIconClose: CloseIcon,
    TIconInfoCircleFilled: InfoCircleFilledIcon,
    TIconCheckCircleFilled: CheckCircleFilledIcon,
    TIconErrorCircleFilled: ErrorCircleFilledIcon,
    TButton: Button
  },
  data: function data() {
    return {
      scrollWidth: 0
    };
  },
  props: _objectSpread({}, props),
  computed: {
    isModal: function isModal() {
      return this.mode === "modal";
    },
    isModeless: function isModeless() {
      return this.mode === "modeless";
    },
    maskClass: function maskClass() {
      return ["".concat(name, "__mask"), !this.showOverlay && "".concat(prefix, "-is-hidden")];
    },
    dialogClass: function dialogClass() {
      var dialogClass = ["".concat(name), "".concat(name, "--default"), "".concat(name, "--").concat(this.placement), "".concat(name, "__modal-").concat(this.theme)];

      if (["modeless", "modal"].includes(this.mode)) {
        dialogClass.push("".concat(name, "--fixed"));
      }

      return dialogClass;
    },
    dialogStyle: function dialogStyle() {
      var top = this.top,
          placement = this.placement;
      var topStyle = {};

      if (top) {
        var topValue = getCSSValue(top);
        topStyle = {
          top: topValue,
          transform: "translate(-50%, 0)",
          transformOrigin: "25% 25%",
          maxHeight: "calc(100% - ".concat(topValue, ")"),
          zIndex: this.zIndex
        };
      } else if (placement === "top") {
        topStyle = {
          maxHeight: "calc(100% - 20%)"
        };
      }

      return _objectSpread({
        width: getCSSValue(this.width)
      }, topStyle);
    }
  },
  watch: {
    visible: function visible(value) {
      var scrollWidth = this.scrollWidth;
      var bodyCssText = "overflow: hidden;";

      if (value) {
        if (scrollWidth > 0) {
          bodyCssText += "position: relative;width: calc(100% - ".concat(scrollWidth, "px);");
        }

        document.body.style.cssText = bodyCssText;
      } else {
        document.body.style.cssText = "";
      }

      this.disPreventScrollThrough(value);
      this.addKeyboardEvent(value);
    }
  },
  mounted: function mounted() {
    this.scrollWidth = window.innerWidth - document.body.offsetWidth;
  },
  beforeDestroy: function beforeDestroy() {
    this.disPreventScrollThrough(false);
    this.addKeyboardEvent(false);
  },
  directives: {
    draggable: function draggable(el, binding) {
      if (el && binding && binding.value) {
        initDragEvent(el);
      }
    },
    TransferDom: TransferDom
  },
  methods: {
    disPreventScrollThrough: function disPreventScrollThrough(disabled) {
      if (this.preventScrollThrough && this.isModal) {
        document.body.style.overflow = disabled ? "hidden" : "";
      }
    },
    addKeyboardEvent: function addKeyboardEvent(status) {
      if (status) {
        document.addEventListener("keydown", this.keyboardEvent);
      } else {
        document.removeEventListener("keydown", this.keyboardEvent);
      }
    },
    keyboardEvent: function keyboardEvent(e) {
      if (e.code === "Escape") {
        emitEvent(this, "esc-keydown", {
          e: e
        });

        if (this.closeOnEscKeydown) {
          this.emitCloseEvent({
            trigger: "esc",
            e: e
          });
        }
      }
    },
    overlayAction: function overlayAction(e) {
      emitEvent(this, "overlay-click", {
        e: e
      });

      if (this.closeOnOverlayClick) {
        this.emitCloseEvent({
          trigger: "overlay",
          e: e
        });
      }
    },
    closeBtnAcion: function closeBtnAcion(e) {
      emitEvent(this, "close-btn-click", {
        e: e
      });
      this.emitCloseEvent({
        trigger: "close-btn",
        e: e
      });
    },
    cancelBtnAction: function cancelBtnAction(e) {
      emitEvent(this, "cancel", {
        e: e
      });
      this.emitCloseEvent({
        trigger: "cancel",
        e: e
      });
    },
    confirmBtnAction: function confirmBtnAction(e) {
      emitEvent(this, "confirm", {
        e: e
      });
    },
    afterEnter: function afterEnter() {
      emitEvent(this, "opened");
    },
    afterLeave: function afterLeave() {
      emitEvent(this, "closed");
    },
    emitCloseEvent: function emitCloseEvent(context) {
      emitEvent(this, "close", context);
      this.$emit("update:visible", false);
    },
    hasEventOn: function hasEventOn(name2) {
      var _this$_events;

      var eventFuncs = (_this$_events = this["_events"]) === null || _this$_events === void 0 ? void 0 : _this$_events[name2];
      return !!(eventFuncs !== null && eventFuncs !== void 0 && eventFuncs.length);
    },
    getIcon: function getIcon() {
      var h = this.$createElement;
      var icon = {
        info: h(InfoCircleFilledIcon, {
          "class": "".concat(prefix, "-is-info")
        }),
        warning: h(ErrorCircleFilledIcon, {
          "class": "".concat(prefix, "-is-warning")
        }),
        danger: h(ErrorCircleFilledIcon, {
          "class": "".concat(prefix, "-is-error")
        }),
        success: h(CheckCircleFilledIcon, {
          "class": "".concat(prefix, "-is-success")
        })
      };
      return icon[this.theme];
    },
    renderDialog: function renderDialog() {
      var h = this.$createElement;
      var defaultHeader = h("h5", {
        "class": "title"
      });
      var defaultCloseBtn = h("t-icon-close");
      var body = renderContent(this, "default", "body");
      var defaultFooter = h("div", [this.getCancelBtn({
        cancelBtn: this.cancelBtn,
        globalCancel: this.global.cancel,
        className: "".concat(prefix, "-dialog__cancel")
      }), this.getConfirmBtn({
        theme: this.theme,
        confirmBtn: this.confirmBtn,
        globalConfirm: this.global.confirm,
        globalConfirmBtnTheme: this.global.confirmBtnTheme,
        className: "".concat(prefix, "-dialog__confirm")
      })]);
      var bodyClassName = this.theme === "default" ? "".concat(name, "__body") : "".concat(name, "__body__icon");
      return h("div", {
        "key": "dialog",
        "class": this.dialogClass,
        "style": this.dialogStyle,
        "directives": [{
          name: "draggable",
          value: this.isModeless && this.draggable
        }]
      }, [h("div", {
        "class": "".concat(name, "__header")
      }, [this.getIcon(), renderTNodeJSX(this, "header", defaultHeader)]), h("span", {
        "class": "".concat(name, "__close"),
        "on": {
          "click": this.closeBtnAcion
        }
      }, [renderTNodeJSX(this, "closeBtn", defaultCloseBtn)]), h("div", {
        "class": bodyClassName
      }, [body]), h("div", {
        "class": "".concat(name, "__footer")
      }, [renderTNodeJSX(this, "footer", defaultFooter)])]);
    }
  },
  render: function render() {
    var h = arguments[0];
    var maskView = this.isModal && h("div", {
      "key": "mask",
      "class": this.maskClass,
      "on": {
        "click": this.overlayAction
      }
    });
    var dialogView = this.renderDialog();
    var view = [maskView, dialogView];
    var ctxStyle = {
      zIndex: this.zIndex
    };
    var ctxClass = ["".concat(name, "__ctx"), _defineProperty({}, "".concat(prefix, "-dialog__ctx--fixed"), this.mode === "modal")];
    return h("transition", {
      "attrs": {
        "duration": 300,
        "name": "".concat(name, "-zoom__vue")
      },
      "on": {
        "afterEnter": this.afterEnter,
        "afterLeave": this.afterLeave
      }
    }, [(!this.destroyOnClose || this.visible) && h("div", {
      "directives": [{
        name: "show",
        value: this.visible
      }, {
        name: "transfer-dom",
        value: this.attach
      }],
      "class": ctxClass,
      "style": ctxStyle
    }, [view])]);
  }
});

export { _Dialog as default };
//# sourceMappingURL=dialog.js.map
