<script>
import theme from "#build/b24ui/modal";
</script>

<script setup>
import { computed, toRef } from "vue";
import { DialogRoot, DialogTrigger, DialogPortal, DialogOverlay, DialogContent, DialogTitle, DialogDescription, DialogClose, VisuallyHidden } from "reka-ui";
import { reactivePick, createReusableTemplate } from "@vueuse/core";
import { useAppConfig } from "#imports";
import { useComponentProps } from "../composables/useComponentProps";
import { useForwardProps } from "../composables/useForwardProps";
import { FieldGroupReset } from "../composables/useFieldGroup";
import { useLocale } from "../composables/useLocale";
import { usePortal } from "../composables/usePortal";
import { pointerDownOutside } from "../utils/overlay";
import { tv } from "../utils/tv";
import icons from "../dictionary/icons";
import B24Button from "./Button.vue";
const _props = defineProps({
  title: { type: String, required: false },
  description: { type: String, required: false },
  content: { type: Object, required: false },
  overlay: { type: Boolean, required: false, default: true },
  overlayBlur: { type: null, required: false, default: "auto" },
  scrollable: { type: Boolean, required: false },
  transition: { type: Boolean, required: false, default: true },
  fullscreen: { type: Boolean, required: false },
  portal: { type: [Boolean, String], required: false, skipCheck: true, default: true },
  close: { type: [Boolean, Object], required: false, default: true },
  closeIcon: { type: [Function, Object], required: false },
  dismissible: { type: Boolean, required: false, default: true },
  scrollbarThin: { type: Boolean, required: false, default: true },
  class: { type: null, required: false },
  b24ui: { type: Object, required: false },
  open: { type: Boolean, required: false },
  defaultOpen: { type: Boolean, required: false },
  modal: { type: Boolean, required: false, default: true }
});
const emits = defineEmits(["after:leave", "after:enter", "close:prevent", "update:open"]);
const slots = defineSlots();
const props = useComponentProps("modal", _props);
const { t } = useLocale();
const appConfig = useAppConfig();
const rootProps = useForwardProps(reactivePick(props, "open", "defaultOpen", "modal"), emits);
const portalProps = usePortal(toRef(() => props.portal));
const contentProps = toRef(() => props.content);
const contentEvents = computed(() => {
  if (!props.dismissible) {
    const events = ["interactOutside", "escapeKeyDown"];
    return events.reduce((acc, curr) => {
      acc[curr] = (e) => {
        e.preventDefault();
        emits("close:prevent");
      };
      return acc;
    }, {});
  }
  return {
    pointerDownOutside: (e) => pointerDownOutside(e, { scrollable: props.scrollable })
  };
});
const [DefineContentTemplate, ReuseContentTemplate] = createReusableTemplate();
const b24ui = computed(() => tv({ extend: tv(theme), ...appConfig.b24ui?.modal || {} })({
  transition: props.transition,
  fullscreen: props.fullscreen,
  overlayBlur: props.overlayBlur,
  overlay: props.overlay,
  scrollable: props.scrollable
}));
</script>

<template>
  <DialogRoot v-slot="{ open, close }" v-bind="rootProps">
    <DefineContentTemplate>
      <DialogContent
        data-slot="content"
        :class="b24ui.content({ class: [!slots.default && props.class, props.b24ui?.content] })"
        v-bind="contentProps"
        @after-enter="emits('after:enter')"
        @after-leave="emits('after:leave')"
        v-on="contentEvents"
      >
        <VisuallyHidden v-if="!props.title && !slots.title || !props.description && !slots.description || !!slots.content">
          <DialogTitle v-if="!props.title && !slots.title" />
          <DialogTitle v-else-if="!!slots.content">
            <slot name="title">
              {{ props.title }}
            </slot>
          </DialogTitle>

          <DialogDescription v-if="!props.description && !slots.description" />
          <DialogDescription v-else-if="!!slots.content">
            <slot name="description">
              {{ props.description }}
            </slot>
          </DialogDescription>
        </VisuallyHidden>

        <slot name="content" :close="close">
          <div
            v-if="!!slots.header || (props.title || !!slots.title) || (props.description || !!slots.description) || (props.close || !!slots.close) || !!slots.body"
            data-slot="contentWrapper"
            :class="b24ui.contentWrapper({ class: props.b24ui?.contentWrapper })"
          >
            <div v-if="!!slots.header || (props.title || !!slots.title) || (props.description || !!slots.description) || (props.close || !!slots.close)" data-slot="header" :class="b24ui.header({ class: props.b24ui?.header })">
              <slot name="header" :close="close">
                <div
                  v-if="props.title || !!slots.title || props.description || !!slots.description"
                  data-slot="wrapper"
                  :class="b24ui.wrapper({ class: props.b24ui?.wrapper })"
                >
                  <DialogTitle v-if="!props.title && !slots.title" />
                  <DialogTitle v-else data-slot="title" :class="b24ui.title({ class: props.b24ui?.title })">
                    <slot name="title">
                      {{ props.title }}
                    </slot>
                  </DialogTitle>

                  <DialogDescription v-if="!props.description && !slots.description" />
                  <DialogDescription v-else data-slot="description" :class="b24ui.description({ class: props.b24ui?.description })">
                    <slot name="description">
                      {{ props.description }}
                    </slot>
                  </DialogDescription>
                </div>

                <slot name="actions" />

                <DialogClose v-if="props.close || !!slots.close" as-child>
                  <slot name="close" :b24ui="b24ui">
                    <B24Button
                      v-if="props.close"
                      :icon="props.closeIcon || icons.close"
                      size="md"
                      color="air-tertiary-no-accent"
                      :aria-label="t('modal.close')"
                      v-bind="typeof props.close === 'object' ? props.close : {}"
                      data-slot="close"
                      :class="b24ui.close({ class: props.b24ui?.close })"
                    />
                  </slot>
                </DialogClose>
              </slot>
            </div>

            <div v-if="!!slots.body" data-slot="body" :class="b24ui.body({ class: props.b24ui?.body, scrollbarThin: Boolean(props.scrollbarThin) })">
              <slot name="body" :close="close" />
            </div>
          </div>

          <div v-if="!!slots.footer" data-slot="footer" :class="b24ui.footer({ class: props.b24ui?.footer })">
            <slot name="footer" :close="close" />
          </div>
        </slot>
      </DialogContent>
    </DefineContentTemplate>

    <DialogTrigger v-if="!!slots.default" as-child :class="props.class">
      <slot :open="open" />
    </DialogTrigger>

    <DialogPortal v-bind="portalProps">
      <FieldGroupReset>
        <template v-if="props.scrollable">
          <DialogOverlay data-slot="overlay" :class="b24ui.overlay({ class: props.b24ui?.overlay })">
            <ReuseContentTemplate />
          </DialogOverlay>
        </template>

        <template v-else>
          <DialogOverlay v-if="props.overlay" data-slot="overlay" :class="b24ui.overlay({ class: props.b24ui?.overlay })" />

          <ReuseContentTemplate />
        </template>
      </FieldGroupReset>
    </DialogPortal>
  </DialogRoot>
</template>
