/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var iconsVueNext = require('@ldesign/icons-vue-next');
var hooks_useConfig = require('../hooks/useConfig.js');
var hooks_useGlobalIcon = require('../hooks/useGlobalIcon.js');
var utils_dom = require('../utils/dom.js');
var _common_js_utils_getScrollbarWidth = require('../_common/js/utils/getScrollbarWidth.js');
var dialog_hooks = require('../dialog/hooks.js');
var hooks_tnode = require('../hooks/tnode.js');
var hooks_useTeleport = require('../hooks/useTeleport.js');
var drawer_props = require('./props.js');
var drawer_hooks = require('./hooks.js');
var configProvider_useConfig = require('../config-provider/useConfig.js');
require('../_chunks/dep-f4db2590.js');
require('lodash/isString');
require('lodash/isFunction');
require('lodash/isArray');
require('../utils/easing.js');
require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/isNull');
require('lodash/isUndefined');
require('lodash/isNumber');
require('@babel/runtime/helpers/typeof');
require('lodash/isObject');
require('lodash/omit');
require('../button/index.js');
require('../utils/withInstall.js');
require('../button/button.js');
require('../loading/index.js');
require('../loading/directive.js');
require('../loading/plugin.js');
require('../loading/loading.js');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../_common/js/utils/set-style.js');
require('lodash/cloneDeep');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('dayjs');
require('../hooks/useRipple.js');
require('../utils/set-style.js');
require('../hooks/useKeepAnimation.js');
require('../form/hooks.js');
require('../button/props.js');

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

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

var key = 1;
var _Drawer = vue.defineComponent({
  name: "TDrawer",
  inheritAttrs: false,
  props: drawer_props["default"],
  emits: ["update:visible"],
  setup: function setup(props2, context) {
    var destroyOnCloseVisible = vue.ref(false);
    var isVisible = vue.ref(false);
    var styleEl = vue.ref();
    var styleTimer = vue.ref();
    var _useConfig = configProvider_useConfig.useConfig("drawer"),
      globalConfig = _useConfig.globalConfig;
    var _useGlobalIcon = hooks_useGlobalIcon.useGlobalIcon({
        CloseIcon: iconsVueNext.CloseIcon
      }),
      CloseIcon = _useGlobalIcon.CloseIcon;
    var renderTNodeJSX = hooks_tnode.useTNodeJSX();
    var renderContent = hooks_tnode.useContent();
    var COMPONENT_NAME = hooks_useConfig.usePrefixClass("drawer");
    var _useDrag = drawer_hooks.useDrag(props2),
      draggedSizeValue = _useDrag.draggedSizeValue,
      enableDrag = _useDrag.enableDrag,
      draggableLineStyles = _useDrag.draggableLineStyles;
    var teleportElement = hooks_useTeleport["default"](function () {
      return props2.attach;
    });
    var confirmBtnAction = function confirmBtnAction(e) {
      var _props2$onConfirm;
      (_props2$onConfirm = props2.onConfirm) === null || _props2$onConfirm === void 0 ? void 0 : _props2$onConfirm.call(props2, {
        e: e
      });
    };
    var cancelBtnAction = function cancelBtnAction(e) {
      var _props2$onCancel;
      (_props2$onCancel = props2.onCancel) === null || _props2$onCancel === void 0 ? void 0 : _props2$onCancel.call(props2, {
        e: e
      });
      closeDrawer({
        trigger: "cancel",
        e: e
      });
    };
    var _useAction = dialog_hooks.useAction({
        confirmBtnAction: confirmBtnAction,
        cancelBtnAction: cancelBtnAction
      }),
      getConfirmBtn = _useAction.getConfirmBtn,
      getCancelBtn = _useAction.getCancelBtn;
    var drawerEle = vue.ref(null);
    var drawerClasses = vue.computed(function () {
      var _ref;
      return [COMPONENT_NAME.value, "".concat(COMPONENT_NAME.value, "--").concat(props2.placement), (_ref = {}, _defineProperty__default["default"](_ref, "".concat(COMPONENT_NAME.value, "--open"), isVisible.value), _defineProperty__default["default"](_ref, "".concat(COMPONENT_NAME.value, "--attach"), props2.showInAttachedElement), _defineProperty__default["default"](_ref, "".concat(COMPONENT_NAME.value, "--without-mask"), !props2.showOverlay), _ref)];
    });
    var sizeValue = vue.computed(function () {
      var _props2$size;
      if (draggedSizeValue.value) return draggedSizeValue.value;
      var size = (_props2$size = props2.size) !== null && _props2$size !== void 0 ? _props2$size : globalConfig.value.size;
      var defaultSize = isNaN(Number(size)) ? size : "".concat(size, "px");
      return {
        small: "300px",
        medium: "500px",
        large: "760px"
      }[size] || defaultSize;
    });
    var wrapperStyles = vue.computed(function () {
      return {
        transform: isVisible.value ? "translateX(0)" : void 0,
        width: ["left", "right"].includes(props2.placement) ? sizeValue.value : "",
        height: ["top", "bottom"].includes(props2.placement) ? sizeValue.value : ""
      };
    });
    var wrapperClasses = vue.computed(function () {
      return ["".concat(COMPONENT_NAME.value, "__content-wrapper"), "".concat(COMPONENT_NAME.value, "__content-wrapper--").concat(props2.placement)];
    });
    var parentNode = vue.computed(function () {
      return drawerEle.value && drawerEle.value.parentNode;
    });
    var modeAndPlacement = vue.computed(function () {
      return [props2.mode, props2.placement].join();
    });
    var footerStyle = vue.computed(function () {
      return {
        display: "flex",
        justifyContent: props2.placement === "right" ? "flex-start" : "flex-end"
      };
    });
    var clearStyleFunc = function clearStyleFunc() {
      clearTimeout(styleTimer.value);
      styleTimer.value = setTimeout(function () {
        var _styleEl$value, _styleEl$value$parent, _styleEl$value$parent2;
        (_styleEl$value = styleEl.value) === null || _styleEl$value === void 0 ? void 0 : (_styleEl$value$parent = _styleEl$value.parentNode) === null || _styleEl$value$parent === void 0 ? void 0 : (_styleEl$value$parent2 = _styleEl$value$parent.removeChild) === null || _styleEl$value$parent2 === void 0 ? void 0 : _styleEl$value$parent2.call(_styleEl$value$parent, styleEl.value);
      }, 150);
    };
    var handlePushMode = function handlePushMode() {
      if (props2.mode !== "push") return;
      vue.nextTick(function () {
        if (!parentNode.value) return;
        parentNode.value.style.cssText = "transition: margin 300ms cubic-bezier(0.7, 0.3, 0.1, 1) 0s;";
      });
    };
    var updatePushMode = function updatePushMode() {
      if (!parentNode.value) return;
      if (props2.mode !== "push" || !parentNode.value) return;
      var marginStr = {
        left: "margin: 0 0 0 ".concat(sizeValue.value),
        right: "margin: 0 0 0 -".concat(sizeValue.value),
        top: "margin: ".concat(sizeValue.value, " 0 0 0"),
        bottom: "margin: -".concat(sizeValue.value, " 0 0 0")
      }[props2.placement];
      if (isVisible.value) parentNode.value.style.cssText += marginStr;else parentNode.value.style.cssText = parentNode.value.style.cssText.replace(/margin:.+;/, "");
    };
    var getDefaultFooter = function getDefaultFooter() {
      var confirmBtn = getConfirmBtn({
        confirmBtn: props2.confirmBtn,
        globalConfirm: globalConfig.value.confirm,
        className: "".concat(COMPONENT_NAME.value, "__confirm")
      });
      var cancelBtn = getCancelBtn({
        cancelBtn: props2.cancelBtn,
        globalCancel: globalConfig.value.cancel,
        className: "".concat(COMPONENT_NAME.value, "__cancel")
      });
      return vue.createVNode("div", {
        "style": footerStyle.value
      }, [props2.placement === "right" ? confirmBtn : null, cancelBtn, props2.placement !== "right" ? confirmBtn : null]);
    };
    vue.watch(modeAndPlacement, function () {
      handlePushMode();
    }, {
      immediate: true
    });
    vue.watch(function () {
      return props2.visible;
    }, function (value) {
      if (utils_dom.isServer) return;
      if (value) {
        if (!props2.showInAttachedElement && props2.preventScrollThrough) styleEl.value && document.head.appendChild(styleEl.value);
      } else {
        clearStyleFunc();
      }
      if (props2.destroyOnClose) {
        if (value) {
          destroyOnCloseVisible.value = false;
          setTimeout(function () {
            isVisible.value = true;
          });
        } else {
          isVisible.value = false;
          setTimeout(function () {
            destroyOnCloseVisible.value = true;
          }, 200);
        }
      } else if (destroyOnCloseVisible.value === true && value) {
        destroyOnCloseVisible.value = false;
        setTimeout(function () {
          isVisible.value = true;
        });
      } else {
        isVisible.value = value;
      }
    }, {
      immediate: true
    });
    var handleCloseBtnClick = function handleCloseBtnClick(e) {
      var _props2$onCloseBtnCli;
      (_props2$onCloseBtnCli = props2.onCloseBtnClick) === null || _props2$onCloseBtnCli === void 0 ? void 0 : _props2$onCloseBtnCli.call(props2, {
        e: e
      });
      closeDrawer({
        trigger: "close-btn",
        e: e
      });
    };
    var handleWrapperClick = function handleWrapperClick(e) {
      var _props2$onOverlayClic, _props2$closeOnOverla;
      (_props2$onOverlayClic = props2.onOverlayClick) === null || _props2$onOverlayClic === void 0 ? void 0 : _props2$onOverlayClic.call(props2, {
        e: e
      });
      if ((_props2$closeOnOverla = props2.closeOnOverlayClick) !== null && _props2$closeOnOverla !== void 0 ? _props2$closeOnOverla : globalConfig.value.closeOnOverlayClick) closeDrawer({
        trigger: "overlay",
        e: e
      });
    };
    var onKeyDown = function onKeyDown(e) {
      var _props2$closeOnEscKey;
      if (((_props2$closeOnEscKey = props2.closeOnEscKeydown) !== null && _props2$closeOnEscKey !== void 0 ? _props2$closeOnEscKey : globalConfig.value.closeOnEscKeydown) && e.key === "Escape") {
        var _props2$onEscKeydown;
        (_props2$onEscKeydown = props2.onEscKeydown) === null || _props2$onEscKeydown === void 0 ? void 0 : _props2$onEscKeydown.call(props2, {
          e: e
        });
        closeDrawer({
          trigger: "esc",
          e: e
        });
      }
    };
    var closeDrawer = function closeDrawer(params) {
      var _props2$onClose;
      (_props2$onClose = props2.onClose) === null || _props2$onClose === void 0 ? void 0 : _props2$onClose.call(props2, params);
      context.emit("update:visible", false);
    };
    vue.onUpdated(function () {
      updatePushMode();
    });
    vue.onMounted(function () {
      var hasScrollBar = document.documentElement.scrollHeight > document.documentElement.clientHeight;
      var scrollWidth = hasScrollBar ? _common_js_utils_getScrollbarWidth.getScrollbarWidth() : 0;
      styleEl.value = document.createElement("style");
      styleEl.value.dataset.id = "td_drawer_".concat(+new Date(), "_").concat(key += 1);
      styleEl.value.innerHTML = "\n        html body {\n          overflow-y: hidden;\n          transition: margin 300ms cubic-bezier(0.7, 0.3, 0.1, 1) 0s;\n          ".concat(props2.mode === "push" ? "" : "width: calc(100% - ".concat(scrollWidth, "px);"), "\n        }\n      ");
      if (isVisible.value && !props2.showInAttachedElement && props2.preventScrollThrough) document.head.appendChild(styleEl.value);
    });
    vue.onBeforeUnmount(function () {
      clearStyleFunc();
    });
    return function () {
      if (destroyOnCloseVisible.value) return;
      var body = renderContent("body", "default");
      var headerContent = renderTNodeJSX("header");
      var defaultFooter = getDefaultFooter();
      return vue.createVNode(vue.Teleport, {
        "disabled": !props2.attach || !teleportElement.value,
        "to": teleportElement.value
      }, {
        "default": function _default() {
          return [vue.createVNode("div", vue.mergeProps({
            "ref": drawerEle,
            "class": drawerClasses.value,
            "style": {
              zIndex: props2.zIndex
            },
            "onKeydown": onKeyDown,
            "tabindex": 0
          }, context.attrs), [props2.showOverlay && vue.createVNode("div", {
            "class": "".concat(COMPONENT_NAME.value, "__mask"),
            "onClick": handleWrapperClick
          }, null), vue.createVNode("div", {
            "class": wrapperClasses.value,
            "style": wrapperStyles.value
          }, [headerContent && vue.createVNode("div", {
            "class": "".concat(COMPONENT_NAME.value, "__header")
          }, [headerContent]), props2.closeBtn && vue.createVNode("div", {
            "class": "".concat(COMPONENT_NAME.value, "__close-btn"),
            "onClick": handleCloseBtnClick
          }, [renderTNodeJSX("closeBtn", vue.createVNode(CloseIcon, null, null))]), vue.createVNode("div", {
            "class": ["".concat(COMPONENT_NAME.value, "__body"), "narrow-scrollbar"]
          }, [body]), props2.footer && vue.createVNode("div", {
            "class": "".concat(COMPONENT_NAME.value, "__footer")
          }, [renderTNodeJSX("footer", defaultFooter)]), props2.sizeDraggable && vue.createVNode("div", {
            "style": draggableLineStyles.value,
            "onMousedown": enableDrag
          }, null)])])];
        }
      });
    };
  }
});

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