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

import { _ as _defineProperty } from '../_chunks/dep-eb20e23f.mjs';
import { defineComponent, ref, computed, nextTick, createVNode, watch, onUpdated, onMounted, onBeforeUnmount, Teleport, mergeProps } from 'vue';
import { CloseIcon } from '@ldesign/icons-vue-next';
import { usePrefixClass } from '../hooks/useConfig.mjs';
import { useGlobalIcon } from '../hooks/useGlobalIcon.mjs';
import { isServer } from '../utils/dom.mjs';
import { getScrollbarWidth } from '../_common/js/utils/getScrollbarWidth.mjs';
import { useAction } from '../dialog/hooks.mjs';
import { useTNodeJSX, useContent } from '../hooks/tnode.mjs';
import useTeleport from '../hooks/useTeleport.mjs';
import props from './props.mjs';
import { useDrag } from './hooks.mjs';
import { useConfig } from '../config-provider/useConfig.mjs';
import '../_chunks/dep-b4041200.mjs';
import '../_chunks/dep-a0b4a044.mjs';
import '../_chunks/dep-1cf2b129.mjs';
import '../_chunks/dep-c959a6fb.mjs';
import '../_chunks/dep-e3b14d70.mjs';
import '../_chunks/dep-66d6733b.mjs';
import '../_chunks/dep-c31e1d9e.mjs';
import '../_chunks/dep-6b22dd27.mjs';
import '../_chunks/dep-0f0c89ff.mjs';
import '../utils/easing.mjs';
import '../_common/js/utils/helper.mjs';
import '../_chunks/dep-e091cfd9.mjs';
import '../_chunks/dep-cfcedf47.mjs';
import '../_chunks/dep-b6a73f93.mjs';
import '../_chunks/dep-55846619.mjs';
import '../_chunks/dep-6f89b77e.mjs';
import '../_chunks/dep-30451c48.mjs';
import '../_chunks/dep-6eea369f.mjs';
import '../_chunks/dep-b2af48ba.mjs';
import '../_chunks/dep-37ba476d.mjs';
import '../_chunks/dep-be27d184.mjs';
import '../_chunks/dep-bf74f1c2.mjs';
import '../_chunks/dep-79821ff2.mjs';
import '../_chunks/dep-0bfbf721.mjs';
import '../_chunks/dep-39cc276d.mjs';
import '../_chunks/dep-8f777748.mjs';
import '../_chunks/dep-ae357f26.mjs';
import '../_chunks/dep-352691c2.mjs';
import '../_chunks/dep-2cfd5b3a.mjs';
import '../_chunks/dep-05fa17c9.mjs';
import '../_chunks/dep-a9c8a5d5.mjs';
import '../_chunks/dep-10085c61.mjs';
import '../_chunks/dep-1b058de3.mjs';
import '../_chunks/dep-c852a61a.mjs';
import '../_chunks/dep-92e7085e.mjs';
import '../_chunks/dep-44f056cd.mjs';
import '../_chunks/dep-23a0f453.mjs';
import '../_chunks/dep-7acc11cb.mjs';
import '../_chunks/dep-59c8420d.mjs';
import '../_chunks/dep-ba3c0b2e.mjs';
import '../_chunks/dep-774ac2d0.mjs';
import '../_chunks/dep-3afa94cf.mjs';
import '../button/index.mjs';
import '../utils/withInstall.mjs';
import '../button/button.mjs';
import '../loading/index.mjs';
import '../loading/directive.mjs';
import '../loading/plugin.mjs';
import '../loading/loading.mjs';
import '../utils/render-tnode.mjs';
import '../_chunks/dep-bd03960e.mjs';
import '../_chunks/dep-f6649a46.mjs';
import '../_chunks/dep-1fe0ad42.mjs';
import '../_chunks/dep-d54f2690.mjs';
import '../_chunks/dep-74848b92.mjs';
import '../_chunks/dep-c4e1a112.mjs';
import '../loading/props.mjs';
import '../loading/icon/gradient.mjs';
import '../_common/js/loading/circle-adapter.mjs';
import '../_common/js/utils/set-style.mjs';
import '../_chunks/dep-44417261.mjs';
import '../_chunks/dep-99920870.mjs';
import '../_chunks/dep-c7e9ce16.mjs';
import '../_chunks/dep-ee57d235.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-6b4aa943.mjs';
import '../_chunks/dep-d52291a9.mjs';
import '../config-provider/type.mjs';
import '../loading/style/css.mjs';
import '../loading/type.mjs';
import '../hooks/useRipple.mjs';
import '../utils/set-style.mjs';
import '../hooks/useKeepAnimation.mjs';
import '../form/hooks.mjs';
import '../button/props.mjs';
import '../button/style/css.mjs';
import '../button/type.mjs';

var key = 1;
var _Drawer = defineComponent({
  name: "TDrawer",
  inheritAttrs: false,
  props: props,
  emits: ["update:visible"],
  setup: function setup(props2, context) {
    var destroyOnCloseVisible = ref(false);
    var isVisible = ref(false);
    var styleEl = ref();
    var styleTimer = ref();
    var _useConfig = useConfig("drawer"),
      globalConfig = _useConfig.globalConfig;
    var _useGlobalIcon = useGlobalIcon({
        CloseIcon: CloseIcon
      }),
      CloseIcon$1 = _useGlobalIcon.CloseIcon;
    var renderTNodeJSX = useTNodeJSX();
    var renderContent = useContent();
    var COMPONENT_NAME = usePrefixClass("drawer");
    var _useDrag = useDrag(props2),
      draggedSizeValue = _useDrag.draggedSizeValue,
      enableDrag = _useDrag.enableDrag,
      draggableLineStyles = _useDrag.draggableLineStyles;
    var teleportElement = useTeleport(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 = useAction({
        confirmBtnAction: confirmBtnAction,
        cancelBtnAction: cancelBtnAction
      }),
      getConfirmBtn = _useAction.getConfirmBtn,
      getCancelBtn = _useAction.getCancelBtn;
    var drawerEle = ref(null);
    var drawerClasses = computed(function () {
      var _ref;
      return [COMPONENT_NAME.value, "".concat(COMPONENT_NAME.value, "--").concat(props2.placement), (_ref = {}, _defineProperty(_ref, "".concat(COMPONENT_NAME.value, "--open"), isVisible.value), _defineProperty(_ref, "".concat(COMPONENT_NAME.value, "--attach"), props2.showInAttachedElement), _defineProperty(_ref, "".concat(COMPONENT_NAME.value, "--without-mask"), !props2.showOverlay), _ref)];
    });
    var sizeValue = 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 = 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 = computed(function () {
      return ["".concat(COMPONENT_NAME.value, "__content-wrapper"), "".concat(COMPONENT_NAME.value, "__content-wrapper--").concat(props2.placement)];
    });
    var parentNode = computed(function () {
      return drawerEle.value && drawerEle.value.parentNode;
    });
    var modeAndPlacement = computed(function () {
      return [props2.mode, props2.placement].join();
    });
    var footerStyle = 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;
      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 createVNode("div", {
        "style": footerStyle.value
      }, [props2.placement === "right" ? confirmBtn : null, cancelBtn, props2.placement !== "right" ? confirmBtn : null]);
    };
    watch(modeAndPlacement, function () {
      handlePushMode();
    }, {
      immediate: true
    });
    watch(function () {
      return props2.visible;
    }, function (value) {
      if (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);
    };
    onUpdated(function () {
      updatePushMode();
    });
    onMounted(function () {
      var hasScrollBar = document.documentElement.scrollHeight > document.documentElement.clientHeight;
      var scrollWidth = hasScrollBar ? 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);
    });
    onBeforeUnmount(function () {
      clearStyleFunc();
    });
    return function () {
      if (destroyOnCloseVisible.value) return;
      var body = renderContent("body", "default");
      var headerContent = renderTNodeJSX("header");
      var defaultFooter = getDefaultFooter();
      return createVNode(Teleport, {
        "disabled": !props2.attach || !teleportElement.value,
        "to": teleportElement.value
      }, {
        "default": function _default() {
          return [createVNode("div", mergeProps({
            "ref": drawerEle,
            "class": drawerClasses.value,
            "style": {
              zIndex: props2.zIndex
            },
            "onKeydown": onKeyDown,
            "tabindex": 0
          }, context.attrs), [props2.showOverlay && createVNode("div", {
            "class": "".concat(COMPONENT_NAME.value, "__mask"),
            "onClick": handleWrapperClick
          }, null), createVNode("div", {
            "class": wrapperClasses.value,
            "style": wrapperStyles.value
          }, [headerContent && createVNode("div", {
            "class": "".concat(COMPONENT_NAME.value, "__header")
          }, [headerContent]), props2.closeBtn && createVNode("div", {
            "class": "".concat(COMPONENT_NAME.value, "__close-btn"),
            "onClick": handleCloseBtnClick
          }, [renderTNodeJSX("closeBtn", createVNode(CloseIcon$1, null, null))]), createVNode("div", {
            "class": ["".concat(COMPONENT_NAME.value, "__body"), "narrow-scrollbar"]
          }, [body]), props2.footer && createVNode("div", {
            "class": "".concat(COMPONENT_NAME.value, "__footer")
          }, [renderTNodeJSX("footer", defaultFooter)]), props2.sizeDraggable && createVNode("div", {
            "style": draggableLineStyles.value,
            "onMousedown": enableDrag
          }, null)])])];
        }
      });
    };
  }
});

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