/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import { h as helper } from '../../_chunks/dep-b13ef886.js';
import _typeof from '@babel/runtime/helpers/typeof';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { CloseIcon } from '@ldesign/icons-vue';
import { Button } from '../button/index.js';
import props from './props.js';
import { renderContent, renderLNodeJSX } from '../../utils/render-node.js';
import mixins from '../../utils/mixins.js';
import getConfigReceiverMixins, { getGlobalIconMixins } from '../config-provider/config-receiver.js';
import TransferDom from '../../utils/transfer-dom.js';
import { emitEvent } from '../../utils/event.js';
import { addClass, removeClass } from '../../utils/dom.js';
import ActionMixin from '../dialog/mixins/action.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import 'vue';
import '../../_chunks/dep-33b15d9d.js';
import '../../_chunks/dep-85e8b23b.js';
import '../../_chunks/dep-fc383775.js';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/slicedToArray';
import '../../config.js';
import '../loading/props.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../_chunks/dep-a0f2090c.js';
import '../../utils/with-install.js';
import '../loading/plugin.js';
import '../button/props.js';
import '../../utils/ripple.js';
import '../../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'lodash/isObject';

var _Drawer = mixins(ActionMixin, getConfigReceiverMixins("drawer"), getGlobalIconMixins()).extend({
  name: "LDrawer",
  components: {
    TButton: Button
  },
  props: props,
  directives: {
    TransferDom: TransferDom
  },
  data: function data() {
    return {
      isSizeDragging: false,
      draggedSizeValue: null
    };
  },
  computed: {
    drawerClasses: function drawerClasses() {
      var _ref;

      return [this.componentName, "".concat(this.componentName, "--").concat(this.placement), (_ref = {}, _defineProperty(_ref, "".concat(this.componentName, "--open"), this.visible), _defineProperty(_ref, "".concat(this.componentName, "--attach"), this.showInAttachedElement), _defineProperty(_ref, "".concat(this.componentName, "--without-mask"), !this.showOverlay), _ref)];
    },
    sizeValue: function sizeValue() {
      var _this$size;

      if (this.draggedSizeValue) return this.draggedSizeValue;
      var size = (_this$size = this.size) !== null && _this$size !== void 0 ? _this$size : this.global.size;
      var defaultSize = isNaN(Number(size)) ? size : "".concat(size, "px");
      return {
        small: "300px",
        medium: "500px",
        large: "760px"
      }[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(this.componentName, "__content-wrapper"), "".concat(this.componentName, "__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(_ref2, oppositeMap[this.placement], 0), _defineProperty(_ref2, "width", this.isHorizontal ? "16px" : "100%"), _defineProperty(_ref2, "height", this.isHorizontal ? "100%" : "16px"), _defineProperty(_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 _this$useGlobalIcon = this.useGlobalIcon({
      CloseIcon: CloseIcon
    }),
        CloseIcon$1 = _this$useGlobalIcon.CloseIcon;

    var defaultCloseBtn = h(CloseIcon$1, {
      "class": "".concat(this.classPrefix, "-submenu-icon")
    });
    var body = 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(this.componentName, "__mask"),
      "on": {
        "click": this.handleWrapperClick
      }
    }), h("div", {
      "class": this.wrapperClasses,
      "style": this.wrapperStyles
    }, [this.header !== false ? h("div", {
      "class": "".concat(this.componentName, "__header")
    }, [renderLNodeJSX(this, "header", h("div"))]) : null, this.closeBtn !== false ? h("div", {
      "class": "".concat(this.componentName, "__close-btn"),
      "on": {
        "click": this.handleCloseBtnClick
      }
    }, [renderLNodeJSX(this, "closeBtn", defaultCloseBtn)]) : null, h("div", {
      "class": "".concat(this.componentName, "__body")
    }, [body]), this.footer !== false ? h("div", {
      "class": "".concat(this.componentName, "__footer")
    }, [renderLNodeJSX(this, "footer", defaultFooter)]) : null, this.sizeDraggable && h("div", {
      "style": this.draggableLineStyles,
      "on": {
        "mousedown": this.enableDrag
      }
    })])]);
  },
  methods: {
    enableDrag: function enableDrag() {
      document.addEventListener("mouseup", this.handleMouseup, true);
      document.addEventListener("mousemove", this.handleMousemove, true);
      this.isSizeDragging = true;
    },
    handleMouseup: function handleMouseup() {
      document.removeEventListener("mouseup", this.handleMouseup, true);
      document.removeEventListener("mousemove", this.handleMousemove, true);
      this.isSizeDragging = false;
    },
    handleMousemove: function handleMousemove(e) {
      var x = e.x,
          y = e.y;
      var maxHeight = document.documentElement.clientHeight;
      var maxWidth = document.documentElement.clientWidth;
      var offsetHeight = 8;
      var offsetWidth = 8;

      if (this.isSizeDragging && this.sizeDraggable) {
        if (this.placement === "right") {
          var moveLeft = Math.min(Math.max(maxWidth - x + offsetWidth, offsetWidth), maxWidth);
          this.draggedSizeValue = "".concat(moveLeft, "px");
        }

        if (this.placement === "left") {
          var moveRight = Math.min(Math.max(x + offsetWidth, offsetWidth), maxWidth);
          this.draggedSizeValue = "".concat(moveRight, "px");
        }

        if (this.placement === "top") {
          var moveBottom = Math.min(Math.max(y + offsetHeight, offsetHeight), maxHeight);
          this.draggedSizeValue = "".concat(moveBottom, "px");
        }

        if (this.placement === "bottom") {
          var moveTop = Math.min(Math.max(maxHeight - y + offsetHeight, offsetHeight), maxHeight);
          this.draggedSizeValue = "".concat(moveTop, "px");
        }
      }
    },
    handleScrollThrough: function handleScrollThrough(visible) {
      if (!document || !document.body || !this.preventScrollThrough) return;

      if (visible && !this.showInAttachedElement) {
        this.preventScrollThrough && addClass(document.body, "".concat(this.componentName, "--lock"));
      } else {
        this.preventScrollThrough && removeClass(document.body, "".concat(this.componentName, "--lock"));
      }
    },
    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(btnApi) === "object";
      return h("t-button", helper([{
        "attrs": {
          "theme": theme
        },
        "on": {
          "click": clickAction
        }
      }, {
        "props": isApiObject ? btnApi : {}
      }, {
        "class": "".concat(this.componentName, "-").concat(btnType)
      }]), [btnApi && _typeof(btnApi) === "object" ? btnApi.content : btnApi]);
    },
    isUseDefault: function isUseDefault(btnApi) {
      var baseTypes = ["string", "object"];
      return Boolean(btnApi && baseTypes.includes(_typeof(btnApi)));
    },
    getDefaultFooter: function getDefaultFooter() {
      var h = this.$createElement;
      var confirmBtn = this.getConfirmBtn({
        confirmBtn: this.confirmBtn,
        globalConfirm: this.global.confirm,
        className: "".concat(this.componentName, "__confirm")
      });
      var cancelBtn = this.getCancelBtn({
        cancelBtn: this.cancelBtn,
        globalCancel: this.global.cancel,
        className: "".concat(this.componentName, "__cancel")
      });
      return h("div", {
        "style": this.footerStyle
      }, [this.placement === "right" ? confirmBtn : null, cancelBtn, this.placement !== "right" ? confirmBtn : null]);
    },
    handleCloseBtnClick: function handleCloseBtnClick(e) {
      emitEvent(this, "close-btn-click", {
        e: e
      });
      this.closeDrawer({
        trigger: "close-btn",
        e: e
      });
    },
    handleWrapperClick: function handleWrapperClick(e) {
      var _this$closeOnOverlayC;

      emitEvent(this, "overlay-click", {
        e: e
      });

      if ((_this$closeOnOverlayC = this.closeOnOverlayClick) !== null && _this$closeOnOverlayC !== void 0 ? _this$closeOnOverlayC : this.global.closeOnOverlayClick) {
        this.closeDrawer({
          trigger: "overlay",
          e: e
        });
      }
    },
    onKeyDown: function onKeyDown(e) {
      if (e.key === "Escape") {
        var _this$closeOnEscKeydo;

        emitEvent(this, "esc-keydown", {
          e: e
        });

        if ((_this$closeOnEscKeydo = this.closeOnEscKeydown) !== null && _this$closeOnEscKeydo !== void 0 ? _this$closeOnEscKeydo : this.global.closeOnEscKeydown) {
          this.closeDrawer({
            trigger: "esc",
            e: e
          });
        }
      }
    },
    confirmBtnAction: function confirmBtnAction(e) {
      emitEvent(this, "confirm", {
        e: e
      });
    },
    cancelBtnAction: function cancelBtnAction(e) {
      emitEvent(this, "cancel", {
        e: e
      });
      this.closeDrawer({
        trigger: "cancel",
        e: e
      });
    },
    closeDrawer: function closeDrawer(params) {
      emitEvent(this, "close", params);
      this.$emit("update:visible", false);
    }
  }
});

export { _Drawer as default };
//# sourceMappingURL=drawer.js.map
