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

import _defineProperty from '@babel/runtime/helpers/defineProperty';
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.js';
import { useGlobalIcon } from '../hooks/useGlobalIcon.js';
import { isServer } from '../utils/dom.js';
import { getScrollbarWidth } from '../_common/js/utils/getScrollbarWidth.js';
import { useAction } from '../dialog/hooks.js';
import { useTNodeJSX, useContent } from '../hooks/tnode.js';
import useTeleport from '../hooks/useTeleport.js';
import props from './props.js';
import { useDrag } from './hooks.js';
import { useConfig } from '../config-provider/useConfig.js';
import '../_chunks/dep-512eb181.js';
import 'lodash/isString';
import 'lodash/isFunction';
import 'lodash/isArray';
import '../utils/easing.js';
import '../_common/js/utils/helper.js';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/isNull';
import 'lodash/isUndefined';
import 'lodash/isNumber';
import '@babel/runtime/helpers/typeof';
import 'lodash/isObject';
import 'lodash/omit';
import '../button/index.js';
import '../utils/withInstall.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/directive.js';
import '../loading/plugin.js';
import '../loading/loading.js';
import '../utils/render-tnode.js';
import 'lodash/isEmpty';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../_common/js/utils/set-style.js';
import 'lodash/cloneDeep';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import 'dayjs';
import '../_chunks/dep-ac28b67c.js';
import '../config-provider/type.js';
import '../loading/style';
import '../loading/type.js';
import '../hooks/useRipple.js';
import '../utils/set-style.js';
import '../hooks/useKeepAnimation.js';
import '../form/hooks.js';
import '../button/props.js';
import '../button/style';
import '../button/type.js';

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.js.map
