/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

'use strict';

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

var helper = require('../../chunks/helper.js');
var iconsVue = require('@ldesign/icons-vue');
var components_button_index = require('../button/index.js');
var utils_renderNode = require('../../utils/render-node.js');
var utils_mixins = require('../../utils/mixins.js');
var components_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 components_dialog_mixins_action = require('../dialog/mixins/action.js');
var components_drawer_props = require('./props.js');
require('../../utils/with-install.js');
require('vue');
require('lodash');
require('../../config.js');
require('../button/button.js');
require('../loading/index.js');
require('../loading/loading.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../../chunks/defineProperty-838e4192.js');
require('../../chunks/set-style.js');
require('../../chunks/helper2.js');
require('raf');
require('lodash/isString');
require('../../chunks/log.js');
require('../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../utils/easing.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('../../chunks/zh_CN.js');
require('../loading/plugin.js');
require('../../utils/ripple.js');
require('../button/props.js');
require('../../utils/helper.js');
require('lodash/camelCase');
require('lodash/isObject');

var _Drawer = utils_mixins["default"](components_dialog_mixins_action["default"], components_configProvider_configReceiver["default"]("drawer"), components_configProvider_configReceiver.getGlobalIconMixins()).extend({
  name: "LDrawer",
  components: {
    TButton: components_button_index.Button
  },
  props: components_drawer_props["default"],
  directives: {
    TransferDom: utils_transferDom["default"]
  },
  data() {
    return {
      isSizeDragging: false,
      draggedSizeValue: null
    };
  },
  computed: {
    drawerClasses() {
      return [this.componentName, `${this.componentName}--${this.placement}`, {
        [`${this.componentName}--open`]: this.visible,
        [`${this.componentName}--attach`]: this.showInAttachedElement,
        [`${this.componentName}--without-mask`]: !this.showOverlay
      }];
    },
    sizeValue() {
      if (this.draggedSizeValue) return this.draggedSizeValue;
      const size = this.size ?? this.global.size;
      const defaultSize = isNaN(Number(size)) ? size : `${size}px`;
      return {
        small: "300px",
        medium: "500px",
        large: "760px"
      }[size] || defaultSize;
    },
    wrapperStyles() {
      return {
        transform: this.visible ? "translateX(0)" : void 0,
        width: this.isHorizontal ? this.sizeValue : "",
        height: this.isVertical ? this.sizeValue : ""
      };
    },
    wrapperClasses() {
      return [`${this.componentName}__content-wrapper`, `${this.componentName}__content-wrapper--${this.placement}`];
    },
    parentNode() {
      return this.$el && this.$el.parentNode;
    },
    modeAndPlacement() {
      return [this.mode, this.placement].join();
    },
    footerStyle() {
      return {
        display: "flex",
        justifyContent: this.placement === "right" ? "flex-start" : "flex-end"
      };
    },
    isHorizontal() {
      return ["right", "left"].includes(this.placement);
    },
    isVertical() {
      return ["top", "bottom"].includes(this.placement);
    },
    draggableLineStyles() {
      const oppositeMap = {
        left: "right",
        right: "left",
        top: "bottom",
        bottom: "top"
      };
      return {
        zIndex: 1,
        position: "absolute",
        background: "transparent",
        [oppositeMap[this.placement]]: 0,
        width: this.isHorizontal ? "16px" : "100%",
        height: this.isHorizontal ? "100%" : "16px",
        cursor: this.isHorizontal ? "col-resize" : "row-resize"
      };
    }
  },
  watch: {
    modeAndPlacement: {
      handler() {
        this.handlePushMode();
      },
      immediate: true
    },
    visible: {
      handler(val) {
        var _this$$refs$drawerCon, _this$$refs$drawerCon2;
        if (val) (_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() {
    this.updatePushMode();
  },
  mounted() {
    this.handleScrollThrough(this.visible);
  },
  render() {
    const h = arguments[0];
    if (this.destroyOnClose && !this.visible) return;
    const {
      CloseIcon
    } = this.useGlobalIcon({
      CloseIcon: iconsVue.CloseIcon
    });
    const defaultCloseBtn = h(CloseIcon, {
      "class": `${this.classPrefix}-submenu-icon`
    });
    const body = utils_renderNode.renderContent(this, "default", "body");
    const 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": `${this.componentName}__mask`,
      "on": {
        "click": this.handleWrapperClick
      }
    }), h("div", {
      "class": this.wrapperClasses,
      "style": this.wrapperStyles
    }, [this.header !== false ? h("div", {
      "class": `${this.componentName}__header`
    }, [utils_renderNode.renderLNodeJSX(this, "header", h("div"))]) : null, this.closeBtn !== false ? h("div", {
      "class": `${this.componentName}__close-btn`,
      "on": {
        "click": this.handleCloseBtnClick
      }
    }, [utils_renderNode.renderLNodeJSX(this, "closeBtn", defaultCloseBtn)]) : null, h("div", {
      "class": `${this.componentName}__body`
    }, [body]), this.footer !== false ? h("div", {
      "class": `${this.componentName}__footer`
    }, [utils_renderNode.renderLNodeJSX(this, "footer", defaultFooter)]) : null, this.sizeDraggable && h("div", {
      "style": this.draggableLineStyles,
      "on": {
        "mousedown": this.enableDrag
      }
    })])]);
  },
  methods: {
    enableDrag() {
      document.addEventListener("mouseup", this.handleMouseup, true);
      document.addEventListener("mousemove", this.handleMousemove, true);
      this.isSizeDragging = true;
    },
    handleMouseup() {
      document.removeEventListener("mouseup", this.handleMouseup, true);
      document.removeEventListener("mousemove", this.handleMousemove, true);
      this.isSizeDragging = false;
    },
    handleMousemove(e) {
      const {
        x,
        y
      } = e;
      const maxHeight = document.documentElement.clientHeight;
      const maxWidth = document.documentElement.clientWidth;
      const offsetHeight = 8;
      const offsetWidth = 8;
      if (this.isSizeDragging && this.sizeDraggable) {
        if (this.placement === "right") {
          const moveLeft = Math.min(Math.max(maxWidth - x + offsetWidth, offsetWidth), maxWidth);
          this.draggedSizeValue = `${moveLeft}px`;
        }
        if (this.placement === "left") {
          const moveRight = Math.min(Math.max(x + offsetWidth, offsetWidth), maxWidth);
          this.draggedSizeValue = `${moveRight}px`;
        }
        if (this.placement === "top") {
          const moveBottom = Math.min(Math.max(y + offsetHeight, offsetHeight), maxHeight);
          this.draggedSizeValue = `${moveBottom}px`;
        }
        if (this.placement === "bottom") {
          const moveTop = Math.min(Math.max(maxHeight - y + offsetHeight, offsetHeight), maxHeight);
          this.draggedSizeValue = `${moveTop}px`;
        }
      }
    },
    handleScrollThrough(visible) {
      if (!document || !document.body || !this.preventScrollThrough) return;
      if (visible && !this.showInAttachedElement) this.preventScrollThrough && utils_dom.addClass(document.body, `${this.componentName}--lock`);else this.preventScrollThrough && utils_dom.removeClass(document.body, `${this.componentName}--lock`);
    },
    handlePushMode() {
      if (this.mode !== "push") return;
      this.$nextTick(() => {
        if (!this.parentNode) return;
        this.parentNode.style.cssText = "transition: margin 300ms cubic-bezier(0.7, 0.3, 0.1, 1) 0s;";
      });
    },
    updatePushMode() {
      if (!this.parentNode) return;
      if (this.mode !== "push" || !this.parentNode) return;
      const marginStr = {
        left: `margin: 0 0 0 ${this.sizeValue}`,
        right: `margin: 0 0 0 -${this.sizeValue}`,
        top: `margin: ${this.sizeValue} 0 0 0`,
        bottom: `margin: -${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(btnType, btnApi) {
      const h = this.$createElement;
      const isCancel = btnType === "cancel";
      const clickAction = isCancel ? this.cancelBtnAction : this.confirmBtnAction;
      const theme = isCancel ? "default" : "primary";
      const isApiObject = typeof btnApi === "object";
      return h("t-button", helper.helper([{
        "attrs": {
          "theme": theme
        },
        "on": {
          "click": clickAction
        }
      }, {
        "props": isApiObject ? btnApi : {}
      }, {
        "class": `${this.componentName}-${btnType}`
      }]), [btnApi && typeof btnApi === "object" ? btnApi.content : btnApi]);
    },
    isUseDefault(btnApi) {
      const baseTypes = ["string", "object"];
      return Boolean(btnApi && baseTypes.includes(typeof btnApi));
    },
    getDefaultFooter() {
      const h = this.$createElement;
      const confirmBtn = this.getConfirmBtn({
        confirmBtn: this.confirmBtn,
        globalConfirm: this.global.confirm,
        className: `${this.componentName}__confirm`
      });
      const cancelBtn = this.getCancelBtn({
        cancelBtn: this.cancelBtn,
        globalCancel: this.global.cancel,
        className: `${this.componentName}__cancel`
      });
      return h("div", {
        "style": this.footerStyle
      }, [this.placement === "right" ? confirmBtn : null, cancelBtn, this.placement !== "right" ? confirmBtn : null]);
    },
    handleCloseBtnClick(e) {
      utils_event.emitEvent(this, "close-btn-click", {
        e
      });
      this.closeDrawer({
        trigger: "close-btn",
        e
      });
    },
    handleWrapperClick(e) {
      utils_event.emitEvent(this, "overlay-click", {
        e
      });
      if (this.closeOnOverlayClick ?? this.global.closeOnOverlayClick) this.closeDrawer({
        trigger: "overlay",
        e
      });
    },
    onKeyDown(e) {
      if (e.key === "Escape") {
        utils_event.emitEvent(this, "esc-keydown", {
          e
        });
        if (this.closeOnEscKeydown ?? this.global.closeOnEscKeydown) this.closeDrawer({
          trigger: "esc",
          e
        });
      }
    },
    confirmBtnAction(e) {
      utils_event.emitEvent(this, "confirm", {
        e
      });
    },
    cancelBtnAction(e) {
      utils_event.emitEvent(this, "cancel", {
        e
      });
      this.closeDrawer({
        trigger: "cancel",
        e
      });
    },
    closeDrawer(params) {
      utils_event.emitEvent(this, "close", params);
      this.$emit("update:visible", false);
    }
  }
});

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