<template>
  <teleport to="body">
    <transition name="widget-dialog-fade" appear>
      <div
        v-if="open"
        class="widget-dialog-backdrop"
        @click.self="emit('update:open', false)"
      >
        <div class="widget-dialog" role="dialog" aria-modal="true">
          <div class="widget-dialog-header">
            <div class="widget-dialog-title">{{ titleText }}</div>
            <button class="widget-dialog-close" type="button" @click="emit('update:open', false)">
              Close
            </button>
          </div>
          <div class="widget-dialog-body">
            <slot />
          </div>
        </div>
      </div>
    </transition>
  </teleport>
</template>

<script setup lang="ts">
import { computed } from 'vue';

const props = defineProps<{
  open: boolean;
  title?: string;
}>();

const emit = defineEmits<{
  (event: 'update:open', value: boolean): void;
}>();

const titleText = computed(() => props.title ?? 'Details');
</script>

<style lang="scss" scoped>
@import '@/styles/tokens.scss';

.widget-dialog-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: $z-index-menu;
}

.widget-dialog {
  width: min(720px, 92vw);
  max-height: 85vh;
  background: $color-surface-1;
  border-radius: $border-radius-small;
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: $shadow-lg;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.widget-dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.widget-dialog-title {
  font-size: 16px;
  font-weight: 600;
  color: $color-text-primary;
}

.widget-dialog-close {
  background: transparent;
  border: none;
  color: $color-text-secondary;
  font-size: 13px;
  cursor: pointer;
}

.widget-dialog-body {
  padding: 16px;
  color: $color-text-primary;
  overflow: auto;
}

.widget-dialog-fade-enter-active,
.widget-dialog-fade-leave-active {
  transition: opacity 200ms ease;
}

.widget-dialog-fade-enter-from,
.widget-dialog-fade-leave-to {
  opacity: 0;
}

.widget-dialog-fade-enter-active .widget-dialog,
.widget-dialog-fade-leave-active .widget-dialog {
  transition: transform 220ms ease, opacity 220ms ease;
}

.widget-dialog-fade-enter-from .widget-dialog,
.widget-dialog-fade-leave-to .widget-dialog {
  transform: scale(0.96);
  opacity: 0;
}
</style>
