/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var helper = require('../_chunks/dep-33ee4bce.js');
var _typeof = require('@babel/runtime/helpers/typeof');
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 drawer_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');
var utils_dom = require('../utils/dom.js');
var dialog_actions = require('../dialog/actions.js');
require('../button/button.js');
require('../utils/classnames.js');
require('../loading/index.js');
require('../loading/loading.js');
require('vue');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../_common/js/utils/set-style.js');
require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('../loading/props.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('lodash/isObject');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../loading/plugin.js');
require('../button/props.js');
require('../utils/ripple.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('../utils/helper.js');
require('lodash/camelCase');

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

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

var name = "".concat(config.prefix, "-drawer");
var lockClass = "".concat(config.prefix, "-drawer--lock");
var _Drawer = utils_mixins["default"](dialog_actions["default"], configProvider_configReceiver["default"]("drawer")).extend({
  name: "TDrawer",
  components: {
    CloseIcon: tdesignIconsVue.CloseIcon,
    TButton: button_index.Button
  },
  props: drawer_props["default"],
  directives: {
    TransferDom: utils_transferDom["default"]
  },
  data: function data() {
    return {
      isSizeDragging: false,
      draggedSizeValue: null
    };
  },
  computed: {
    drawerClasses: function drawerClasses() {
      var _ref;

      return [name, "".concat(name, "--").concat(this.placement), (_ref = {}, _defineProperty__default["default"](_ref, "".concat(name, "--open"), this.visible), _defineProperty__default["default"](_ref, "".concat(name, "--attach"), this.showInAttachedElement), _defineProperty__default["default"](_ref, "".concat(name, "--without-mask"), !this.showOverlay), _ref)];
    },
    sizeValue: function sizeValue() {
      if (this.draggedSizeValue) return this.draggedSizeValue;
      var defaultSize = isNaN(Number(this.size)) ? this.size : "".concat(this.size, "px");
      return {
        small: "300px",
        medium: "500px",
        large: "760px"
      }[this.size] || defaultSize;
    },
    wrapperStyles: function wrapperStyles() {
      return {
        transform: this.visible ? "translateX(0)" : void 0,
        width: this.isHorizontal ? this.sizeValue : "",
        height: this.isVertical ? this.sizeValue : ""
      };
    },
    wrapperClasses: function wrapperClasses() {
      return ["".concat(name, "__content-wrapper"), "".concat(name, "__content-wrapper--").concat(this.placement)];
    },
    parentNode: function parentNode() {
      return this.$el && this.$el.parentNode;
    },
    modeAndPlacement: function modeAndPlacement() {
      return [this.mode, this.placement].join();
    },
    footerStyle: function footerStyle() {
      return {
        display: "flex",
        justifyContent: this.placement === "right" ? "flex-start" : "flex-end"
      };
    },
    isHorizontal: function isHorizontal() {
      return ["right", "left"].includes(this.placement);
    },
    isVertical: function isVertical() {
      return ["top", "bottom"].includes(this.placement);
    },
    draggableLineStyles: function draggableLineStyles() {
      var _ref2;

      var oppositeMap = {
        left: "right",
        right: "left",
        top: "bottom",
        bottom: "top"
      };
      return _ref2 = {
        zIndex: 1,
        position: "absolute",
        background: "transparent"
      }, _defineProperty__default["default"](_ref2, oppositeMap[this.placement], 0), _defineProperty__default["default"](_ref2, "width", this.isHorizontal ? "16px" : "100%"), _defineProperty__default["default"](_ref2, "height", this.isHorizontal ? "100%" : "16px"), _defineProperty__default["default"](_ref2, "cursor", this.isHorizontal ? "col-resize" : "row-resize"), _ref2;
    }
  },
  watch: {
    modeAndPlacement: {
      handler: function handler() {
        this.handlePushMode();
      },
      immediate: true
    },
    visible: {
      handler: function handler(val) {
        if (val) {
          var _this$$refs$drawerCon, _this$$refs$drawerCon2;

          (_this$$refs$drawerCon = this.$refs.drawerContainer) === null || _this$$refs$drawerCon === void 0 ? void 0 : (_this$$refs$drawerCon2 = _this$$refs$drawerCon.focus) === null || _this$$refs$drawerCon2 === void 0 ? void 0 : _this$$refs$drawerCon2.call(_this$$refs$drawerCon);
        }

        this.handleScrollThrough(val);
      }
    }
  },
  updated: function updated() {
    this.updatePushMode();
  },
  mounted: function mounted() {
    this.handleScrollThrough(this.visible);
  },
  render: function render() {
    var h = arguments[0];
    if (this.destroyOnClose && !this.visible) return;
    var defaultCloseBtn = h("close-icon", {
      "class": "".concat(config.prefix, "-submenu-icon")
    });
    var body = utils_renderTnode.renderContent(this, "default", "body");
    var defaultFooter = this.getDefaultFooter();
    return h("div", {
      "class": this.drawerClasses,
      "style": {
        zIndex: this.zIndex
      },
      "on": {
        "keydown": this.onKeyDown
      },
      "directives": [{
        name: "transfer-dom",
        value: this.attach
      }],
      "ref": "drawerContainer",
      "attrs": {
        "tabindex": 0
      }
    }, [this.showOverlay && h("div", {
      "class": "".concat(name, "__mask"),
      "on": {
        "click": this.handleWrapperClick
      }
    }), h("div", {
      "class": this.wrapperClasses,
      "style": this.wrapperStyles
    }, [this.header !== false ? h("div", {
      "class": "".concat(name, "__header")
    }, [utils_renderTnode.renderTNodeJSX(this, "header")]) : null, this.closeBtn !== false ? h("div", {
      "class": "".concat(name, "__close-btn"),
      "on": {
        "click": this.handleCloseBtnClick
      }
    }, [utils_renderTnode.renderTNodeJSX(this, "closeBtn", defaultCloseBtn)]) : null, h("div", {
      "class": "".concat(name, "__body")
    }, [body]), this.footer !== false ? h("div", {
      "class": "".concat(name, "__footer")
    }, [utils_renderTnode.renderTNodeJSX(this, "footer", defaultFooter)]) : null, this.sizeDraggable && h("div", {
      "style": this.draggableLineStyles,
      "on": {
        "mousedown": this.enableDrag,
        "mousemove": this.handleMousemove,
        "mouseup": this.disableDrag,
        "mouseleave": this.disableDrag
      }
    })])]);
  },
  methods: {
    enableDrag: function enableDrag() {
      this.isSizeDragging = true;
    },
    handleMousemove: function handleMousemove(e) {
      var x = e.x,
          y = e.y;

      if (this.isSizeDragging && this.sizeDraggable) {
        if (this.placement === "right") {
          this.draggedSizeValue = "".concat(document.documentElement.clientWidth - x + 8, "px");
        }

        if (this.placement === "left") {
          this.draggedSizeValue = "".concat(x + 8, "px");
        }

        if (this.placement === "top") {
          this.draggedSizeValue = "".concat(y + 8, "px");
        }

        if (this.placement === "bottom") {
          this.draggedSizeValue = "".concat(document.documentElement.clientHeight - y + 8, "px");
        }
      }
    },
    disableDrag: function disableDrag() {
      this.isSizeDragging = false;
    },
    handleScrollThrough: function handleScrollThrough(visible) {
      if (!document || !document.body || !this.preventScrollThrough) return;

      if (visible && !this.showInAttachedElement) {
        this.preventScrollThrough && utils_dom.addClass(document.body, lockClass);
      } else {
        this.preventScrollThrough && utils_dom.removeClass(document.body, lockClass);
      }
    },
    handlePushMode: function handlePushMode() {
      var _this = this;

      if (this.mode !== "push") return;
      this.$nextTick(function () {
        if (!_this.parentNode) return;
        _this.parentNode.style.cssText = "transition: margin 300ms cubic-bezier(0.7, 0.3, 0.1, 1) 0s;";
      });
    },
    updatePushMode: function updatePushMode() {
      if (!this.parentNode) return;
      if (this.mode !== "push" || !this.parentNode) return;
      var marginStr = {
        left: "margin: 0 0 0 ".concat(this.sizeValue),
        right: "margin: 0 0 0 -".concat(this.sizeValue),
        top: "margin: ".concat(this.sizeValue, " 0 0 0"),
        bottom: "margin: -".concat(this.sizeValue, " 0 0 0")
      }[this.placement];

      if (this.visible) {
        this.parentNode.style.cssText += marginStr;
      } else {
        this.parentNode.style.cssText = this.parentNode.style.cssText.replace(/margin:.+;/, "");
      }
    },
    getDefaultBtn: function getDefaultBtn(btnType, btnApi) {
      var h = this.$createElement;
      var isCancel = btnType === "cancel";
      var clickAction = isCancel ? this.cancelBtnAction : this.confirmBtnAction;
      var theme = isCancel ? "default" : "primary";
      var isApiObject = _typeof__default["default"](btnApi) === "object";
      return h("t-button", helper.helper([{
        "attrs": {
          "theme": theme
        },
        "on": {
          "click": clickAction
        }
      }, {
        "props": isApiObject ? btnApi : {}
      }, {
        "class": "".concat(name, "-").concat(btnType)
      }]), [btnApi && _typeof__default["default"](btnApi) === "object" ? btnApi.content : btnApi]);
    },
    isUseDefault: function isUseDefault(btnApi) {
      var baseTypes = ["string", "object"];
      return Boolean(btnApi && baseTypes.includes(_typeof__default["default"](btnApi)));
    },
    getDefaultFooter: function getDefaultFooter() {
      var h = this.$createElement;
      var confirmBtn = this.getConfirmBtn({
        confirmBtn: this.confirmBtn,
        globalConfirm: this.global.confirm,
        className: "".concat(config.prefix, "-drawer__confirm")
      });
      var cancelBtn = this.getCancelBtn({
        cancelBtn: this.cancelBtn,
        globalCancel: this.global.cancel,
        className: "".concat(config.prefix, "-drawer__cancel")
      });
      return h("div", {
        "style": this.footerStyle
      }, [this.placement === "right" ? confirmBtn : null, cancelBtn, this.placement !== "right" ? confirmBtn : null]);
    },
    handleCloseBtnClick: function handleCloseBtnClick(e) {
      utils_event.emitEvent(this, "close-btn-click", {
        e: e
      });
      this.closeDrawer({
        trigger: "close-btn",
        e: e
      });
    },
    handleWrapperClick: function handleWrapperClick(e) {
      utils_event.emitEvent(this, "overlay-click", {
        e: e
      });

      if (this.closeOnOverlayClick) {
        this.closeDrawer({
          trigger: "overlay",
          e: e
        });
      }
    },
    onKeyDown: function onKeyDown(e) {
      if (e.key === "Escape") {
        utils_event.emitEvent(this, "esc-keydown", {
          e: e
        });

        if (this.closeOnEscKeydown) {
          this.closeDrawer({
            trigger: "esc",
            e: e
          });
        }
      }
    },
    confirmBtnAction: function confirmBtnAction(e) {
      utils_event.emitEvent(this, "confirm", {
        e: e
      });
    },
    cancelBtnAction: function cancelBtnAction(e) {
      utils_event.emitEvent(this, "cancel", {
        e: e
      });
      this.closeDrawer({
        trigger: "cancel",
        e: e
      });
    },
    closeDrawer: function closeDrawer(params) {
      utils_event.emitEvent(this, "close", params);
      this.$emit("update:visible", false);
    }
  }
});

exports["default"] = _Drawer;
//# sourceMappingURL=drawer.js.map
