import { defineComponent, ref, computed, watch, openBlock, createElementBlock, normalizeClass, createElementVNode, normalizeStyle, toDisplayString, renderSlot, createCommentVNode, createVNode } from "vue";
import SvgIcon from "./SvgIcon.vue.mjs";
import CloseIcon from "../icons/CloseIcon.vue.mjs";
import { addSuffix } from "../../../utils/utils.mjs";
const _hoisted_1 = { class: "drawer-header" };
const _hoisted_2 = { class: "drawer-header-title" };
const _hoisted_3 = { class: "drawer-content" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "Drawer",
  props: {
    title: { default: "" },
    modelValue: { type: Boolean, default: false },
    modal: { type: Boolean, default: false },
    size: { default: "400" },
    beforeClose: { type: Function, default: void 0 },
    appendToBody: { type: Boolean, default: false },
    appendToRoomContainer: { type: Boolean, default: false },
    closeOnClickModal: { type: Boolean, default: true }
  },
  emits: ["update:modelValue"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const drawerRef = ref();
    const visible = ref(false);
    const drawerContainerStyle = computed(() => `width: ${addSuffix(props.size)}`);
    watch(
      () => props.modelValue,
      (val) => {
        visible.value = val;
      }
    );
    watch(visible, (val) => {
      var _a;
      if (val) {
        if (props.appendToBody) {
          document.body.appendChild(drawerRef.value);
        } else if (props.appendToRoomContainer) {
          (_a = document.getElementById("roomContainer")) == null ? void 0 : _a.appendChild(drawerRef.value);
        }
      }
    });
    function doClose() {
      visible.value = false;
      emit("update:modelValue", false);
    }
    function handleClose() {
      if (props.beforeClose) {
        props.beforeClose(doClose);
      } else {
        doClose();
      }
    }
    let mouseDownInCurrentTarget = false;
    let mouseUpInCurrentTarget = false;
    function handleOverlayMouseUp(event) {
      mouseUpInCurrentTarget = event.target === event.currentTarget;
    }
    function handleOverlayMouseDown(event) {
      mouseDownInCurrentTarget = event.target === event.currentTarget;
    }
    function handleOverlayClick() {
      if (!props.closeOnClickModal) {
        return;
      }
      if (mouseDownInCurrentTarget && mouseUpInCurrentTarget) {
        handleClose();
        mouseDownInCurrentTarget = false;
        mouseUpInCurrentTarget = false;
      }
    }
    return (_ctx, _cache) => {
      return visible.value ? (openBlock(), createElementBlock("div", {
        key: 0,
        ref_key: "drawerRef",
        ref: drawerRef,
        class: normalizeClass(["overlay-container", [_ctx.modal && "overlay"]]),
        onMouseup: handleOverlayMouseUp,
        onMousedown: handleOverlayMouseDown,
        onClick: handleOverlayClick
      }, [
        createElementVNode("div", {
          class: "drawer-container",
          style: normalizeStyle(drawerContainerStyle.value)
        }, [
          createElementVNode("div", _hoisted_1, [
            createElementVNode("div", _hoisted_2, toDisplayString(_ctx.title), 1),
            _ctx.$slots.title ? renderSlot(_ctx.$slots, "title", { key: 0 }, void 0, true) : createCommentVNode("", true),
            createElementVNode("div", {
              class: "close",
              onClick: handleClose
            }, [
              createVNode(SvgIcon, {
                size: 16,
                icon: CloseIcon
              })
            ])
          ]),
          createElementVNode("div", _hoisted_3, [
            renderSlot(_ctx.$slots, "default", {}, void 0, true)
          ])
        ], 4)
      ], 34)) : createCommentVNode("", true);
    };
  }
});
export {
  _sfc_main as default
};
