import { defineComponent, computed, ref, watch, openBlock, createElementBlock, createBlock, Teleport, createElementVNode, normalizeClass, normalizeStyle, createVNode, toDisplayString, renderSlot, createCommentVNode, withCtx } from "vue";
import SvgIcon from "../SvgIcon.vue.mjs";
import CloseIcon from "../../icons/CloseIcon.vue.mjs";
import { addSuffix } from "../../../../utils/utils.mjs";
import useZIndex from "../../../../hooks/useZIndex.mjs";
const _hoisted_1 = { key: 0 };
const _hoisted_2 = { class: "tui-dialog-header" };
const _hoisted_3 = { class: "tui-dialog-header-title" };
const _hoisted_4 = { class: "tui-dialog-header-title-content" };
const _hoisted_5 = {
  key: 1,
  class: "close"
};
const _hoisted_6 = { class: "tui-dialog-content" };
const _hoisted_7 = {
  key: 0,
  class: "tui-dialog-footer"
};
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "DialogPC",
  props: {
    title: { default: "" },
    modelValue: { type: Boolean, default: false },
    modal: { type: Boolean, default: false },
    width: { default: void 0 },
    beforeClose: { type: [Function, null], default: null },
    closeOnClickModal: { type: Boolean, default: true },
    showClose: { type: Boolean, default: true },
    appendToBody: { type: Boolean, default: false },
    appendToRoomContainer: { type: Boolean, default: false },
    confirmButton: { default: "" },
    cancelButton: { default: "" },
    titleIcon: { default: null }
  },
  emits: ["update:modelValue", "close", "confirm", "cancel"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const { nextZIndex } = useZIndex();
    const teleportDisable = computed(
      () => !props.appendToBody && !props.appendToRoomContainer
    );
    const targetName = computed(() => {
      if (props.appendToRoomContainer) {
        return "#roomContainer";
      }
      return "body";
    });
    const overlayContainerStyle = ref({});
    const visible = ref(false);
    const dialogRef = ref();
    const drawerContainerStyle = computed(() => {
      let style = "";
      if (props.width) {
        style += `--tui-dialog-width: ${addSuffix(props.width)}`;
      }
      return style;
    });
    watch(
      () => props.modelValue,
      (val) => {
        visible.value = val;
      },
      {
        immediate: true
      }
    );
    watch(visible, (val) => {
      if (val) {
        overlayContainerStyle.value = { zIndex: nextZIndex() };
      }
    });
    function doClose() {
      visible.value = false;
      emit("close");
      emit("update:modelValue", false);
    }
    function handleClose() {
      if (props.beforeClose) {
        props.beforeClose(doClose);
      } else {
        doClose();
      }
    }
    function handleOverlayClick(event) {
      if (!props.closeOnClickModal) {
        return;
      }
      if (event.target !== event.currentTarget) {
        return;
      }
      handleClose();
    }
    return (_ctx, _cache) => {
      return visible.value ? (openBlock(), createElementBlock("div", _hoisted_1, [
        (openBlock(), createBlock(Teleport, {
          to: targetName.value,
          disabled: teleportDisable.value
        }, [
          createElementVNode("div", {
            ref_key: "dialogRef",
            ref: dialogRef,
            class: normalizeClass(["overlay-container", [_ctx.modal && "overlay"]]),
            style: normalizeStyle(overlayContainerStyle.value),
            onClick: handleOverlayClick
          }, [
            createElementVNode("div", {
              class: "tui-dialog-container",
              style: normalizeStyle(drawerContainerStyle.value)
            }, [
              createElementVNode("div", _hoisted_2, [
                createElementVNode("div", _hoisted_3, [
                  createVNode(SvgIcon, { icon: _ctx.titleIcon }, null, 8, ["icon"]),
                  createElementVNode("div", _hoisted_4, toDisplayString(_ctx.title), 1)
                ]),
                _ctx.$slots.title ? renderSlot(_ctx.$slots, "title", { key: 0 }, void 0, true) : createCommentVNode("", true),
                _ctx.showClose ? (openBlock(), createElementBlock("div", _hoisted_5, [
                  createVNode(SvgIcon, {
                    size: 16,
                    onClick: handleClose
                  }, {
                    default: withCtx(() => [
                      createVNode(CloseIcon)
                    ]),
                    _: 1
                  })
                ])) : createCommentVNode("", true)
              ]),
              createElementVNode("div", _hoisted_6, [
                renderSlot(_ctx.$slots, "default", {}, void 0, true)
              ]),
              _ctx.$slots.footer ? (openBlock(), createElementBlock("div", _hoisted_7, [
                renderSlot(_ctx.$slots, "footer", {}, void 0, true)
              ])) : createCommentVNode("", true)
            ], 4)
          ], 6)
        ], 8, ["to", "disabled"]))
      ])) : createCommentVNode("", true);
    };
  }
});
export {
  _sfc_main as default
};
