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

<script setup>
import { computed, toRef } from "vue";
import { DialogRoot, DialogTrigger, DialogPortal, DialogOverlay, DialogContent, DialogTitle, DialogDescription, DialogClose, VisuallyHidden, useForwardPropsEmits } from "reka-ui";
import { reactivePick } from "@vueuse/core";
import { useAppConfig } from "#imports";
import { useLocale } from "../composables/useLocale";
import { usePortal } from "../composables/usePortal";
import { tv } from "../utils/tv";
import icons from "../dictionary/icons";
import B24Button from "./Button.vue";
import B24SidebarLayout from "./SidebarLayout.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: "off" },
  transition: { type: Boolean, required: false, default: true },
  side: { type: null, required: false, default: "bottom" },
  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 },
  useLightContent: { type: Boolean, required: false, default: true },
  class: { type: null, required: false },
  b24ui: { type: null, 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 { t } = useLocale();
const appConfig = useAppConfig();
const rootProps = useForwardPropsEmits(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 = ["pointerDownOutside", "interactOutside", "escapeKeyDown"];
    return events.reduce((acc, curr) => {
      acc[curr] = (e) => {
        e.preventDefault();
        emits("close:prevent");
      };
      return acc;
    }, {});
  }
  return {};
});
const b24ui = computed(() => tv({ extend: tv(theme), ...appConfig.b24ui?.slideover || {} })({
  transition: props.transition,
  side: props.side,
  overlayBlur: props.overlayBlur,
  useFooter: !!slots.footer
}));
</script>

<template>
  <DialogRoot v-slot="{ open, close }" v-bind="rootProps">
    <DialogTrigger v-if="!!slots.default" as-child :class="props.class">
      <slot :open="open" />
    </DialogTrigger>

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

      <DialogContent
        :data-side="side"
        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="
  !!slots.content && (title || !!slots.title || (description || !!slots.description)) || !slots.content && !!slots.header && (!slots.title || !slots.description)
"
        >
          <DialogTitle v-if="title || !!slots.title">
            <slot name="title">
              {{ title }}
            </slot>
          </DialogTitle>

          <DialogDescription v-if="description || !!slots.description">
            <slot name="description">
              {{ description }}
            </slot>
          </DialogDescription>
        </VisuallyHidden>

        <slot name="content" :close="close">
          <template v-if="['left', 'right', 'bottom'].includes(props?.side) && (props.close || !!slots.close)">
            <DialogClose v-if="props.close || !!slots.close" as-child>
              <slot name="close" :b24ui="b24ui">
                <!-- @todo fix this css -->
                <B24Button
                  v-if="props.close"
                  :icon="closeIcon || icons.close"
                  class="group"
                  color="air-primary"
                  :aria-label="t('slideover.close')"
                  size="lg"
                  :b24ui="{
  baseLine: 'ps-[4px] pe-[4px]',
  label: 'hidden sm:flex'
}"
                  v-bind="typeof props.close === 'object' ? props.close : {}"
                  data-slot="close"
                  :class="b24ui.close({ class: props.b24ui?.close })"
                />
              </slot>
            </DialogClose>
          </template>
          <B24SidebarLayout
            :use-light-content="props.useLightContent"
            is-inner
            :b24ui="{
  root: b24ui.sidebarLayoutRoot({ class: props.b24ui?.sidebarLayoutRoot }),
  header: b24ui.sidebarLayoutHeaderWrapper({ class: props.b24ui?.sidebarLayoutHeaderWrapper }),
  pageWrapper: b24ui.sidebarLayoutPageWrapper({ class: props.b24ui?.sidebarLayoutPageWrapper }),
  container: b24ui.sidebarLayoutContainer({ class: props.b24ui?.sidebarLayoutContainer }),
  pageBottomWrapper: b24ui.sidebarLayoutPageBottomWrapper({ class: props.b24ui?.sidebarLayoutPageBottomWrapper }),
  loadingWrapper: b24ui.sidebarLayoutLoadingWrapper({ class: props.b24ui?.sidebarLayoutLoadingWrapper }),
  loadingIcon: b24ui.sidebarLayoutLoadingIcon({ class: props.b24ui?.sidebarLayoutLoadingIcon })
}"
          >
            <template v-if="!!slots['sidebar']" #sidebar>
              <slot name="sidebar" :close="close" />
            </template>

            <template v-if="!!slots['navbar']" #navbar>
              <slot name="navbar" :close="close" />
            </template>

            <template v-if="!!slots.header || (title || !!slots.title) || (description || !!slots.description) || ['top'].includes(props?.side) && (props.close || !!slots.close)" #content-top>
              <div data-slot="header" :class="b24ui.header({ class: props.b24ui?.header })">
                <slot name="header" :close="close">
                  <div data-slot="wrapper" :class="b24ui.wrapper({ class: props.b24ui?.wrapper })">
                    <DialogTitle v-if="title || !!slots.title" data-slot="title" :class="b24ui.title({ class: props.b24ui?.title })">
                      <slot name="title">
                        {{ title }}
                      </slot>
                    </DialogTitle>

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

            <template v-if="!!slots['actions']" #content-actions>
              <slot name="actions" />
            </template>

            <template v-if="!!slots['body']" #default>
              <div data-slot="body" :class="b24ui.body({ class: props.b24ui?.body })">
                <slot name="body" :close="close" />
              </div>
            </template>

            <template v-if="!!slots.footer" #content-bottom>
              <div data-slot="footer" :class="b24ui.footer({ class: props.b24ui?.footer })">
                <slot name="footer" :close="close" />
              </div>
            </template>
          </B24SidebarLayout>
        </slot>
      </DialogContent>
    </DialogPortal>
  </DialogRoot>
</template>
