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

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var tdesignIconsVue = require('tdesign-icons-vue');
var config = require('../config.js');
var button_index = require('../button/index.js');
var dialog_actions = require('./actions.js');
var dialog_props = require('./props.js');
var utils_renderTnode = require('../utils/render-tnode.js');
var utils_mixins = require('../utils/mixins.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_transferDom = require('../utils/transfer-dom.js');
var utils_event = require('../utils/event.js');
require('../button/button.js');
require('vue');
require('../utils/classnames.js');
require('../loading/index.js');
require('../loading/loading.js');
require('../loading/icon/gradient.js');
require('../utils/set-style.js');
require('../utils/dom.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('../loading/props.js');
require('@babel/runtime/helpers/typeof');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../loading/plugin.js');
require('../button/props.js');
require('../utils/ripple.js');
require('../_chunks/dep-44e6bdf6.js');
require('lodash/isObject');
require('../config-provider/zh_CN_config.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');

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

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

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__default["default"](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(config.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 = utils_mixins["default"](dialog_actions["default"], configProvider_configReceiver["default"]("dialog")).extend({
  name: "TDialog",
  components: {
    TIconClose: tdesignIconsVue.CloseIcon,
    TIconInfoCircleFilled: tdesignIconsVue.InfoCircleFilledIcon,
    TIconCheckCircleFilled: tdesignIconsVue.CheckCircleFilledIcon,
    TIconErrorCircleFilled: tdesignIconsVue.ErrorCircleFilledIcon,
    TButton: button_index.Button
  },
  data: function data() {
    return {
      scrollWidth: 0
    };
  },
  props: _objectSpread({}, dialog_props["default"]),
  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(config.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: utils_transferDom["default"]
  },
  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") {
        utils_event.emitEvent(this, "esc-keydown", {
          e: e
        });

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

      if (this.closeOnOverlayClick) {
        this.emitCloseEvent({
          trigger: "overlay",
          e: e
        });
      }
    },
    closeBtnAcion: function closeBtnAcion(e) {
      utils_event.emitEvent(this, "close-btn-click", {
        e: e
      });
      this.emitCloseEvent({
        trigger: "close-btn",
        e: e
      });
    },
    cancelBtnAction: function cancelBtnAction(e) {
      utils_event.emitEvent(this, "cancel", {
        e: e
      });
      this.emitCloseEvent({
        trigger: "cancel",
        e: e
      });
    },
    confirmBtnAction: function confirmBtnAction(e) {
      utils_event.emitEvent(this, "confirm", {
        e: e
      });
    },
    afterEnter: function afterEnter() {
      utils_event.emitEvent(this, "opened");
    },
    afterLeave: function afterLeave() {
      utils_event.emitEvent(this, "closed");
    },
    emitCloseEvent: function emitCloseEvent(context) {
      utils_event.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(tdesignIconsVue.InfoCircleFilledIcon, {
          "class": "".concat(config.prefix, "-is-info")
        }),
        warning: h(tdesignIconsVue.ErrorCircleFilledIcon, {
          "class": "".concat(config.prefix, "-is-warning")
        }),
        danger: h(tdesignIconsVue.ErrorCircleFilledIcon, {
          "class": "".concat(config.prefix, "-is-error")
        }),
        success: h(tdesignIconsVue.CheckCircleFilledIcon, {
          "class": "".concat(config.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 = utils_renderTnode.renderContent(this, "default", "body");
      var defaultFooter = h("div", [this.getCancelBtn({
        cancelBtn: this.cancelBtn,
        globalCancel: this.global.cancel,
        className: "".concat(config.prefix, "-dialog__cancel")
      }), this.getConfirmBtn({
        theme: this.theme,
        confirmBtn: this.confirmBtn,
        globalConfirm: this.global.confirm,
        globalConfirmBtnTheme: this.global.confirmBtnTheme,
        className: "".concat(config.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(), utils_renderTnode.renderTNodeJSX(this, "header", defaultHeader)]), h("span", {
        "class": "".concat(name, "__close"),
        "on": {
          "click": this.closeBtnAcion
        }
      }, [utils_renderTnode.renderTNodeJSX(this, "closeBtn", defaultCloseBtn)]), h("div", {
        "class": bodyClassName
      }, [body]), h("div", {
        "class": "".concat(name, "__footer")
      }, [utils_renderTnode.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__default["default"]({}, "".concat(config.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])]);
  }
});

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