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

<script setup>
import { computed } from "vue";
import { Primitive } from "reka-ui";
import { useAppConfig } from "#imports";
import { useLocale } from "../composables/useLocale";
import { tv } from "../utils/tv";
import icons from "../dictionary/icons";
import B24Avatar from "./Avatar.vue";
import B24Button from "./Button.vue";
const props = defineProps({
  as: { type: null, required: false },
  title: { type: String, required: false },
  description: { type: String, required: false },
  icon: { type: [Function, Object], required: false },
  avatar: { type: Object, required: false },
  color: { type: null, required: false },
  inverted: { type: Boolean, required: false, default: false },
  orientation: { type: null, required: false, default: "vertical" },
  size: { type: null, required: false },
  actions: { type: Array, required: false },
  close: { type: [Boolean, Object], required: false },
  closeIcon: { type: [Function, Object], required: false },
  class: { type: null, required: false },
  b24ui: { type: null, required: false }
});
const emits = defineEmits(["update:open"]);
const slots = defineSlots();
const { t } = useLocale();
const appConfig = useAppConfig();
const b24ui = computed(() => tv({ extend: tv(theme), ...appConfig.b24ui?.alert || {} })({
  color: props.color,
  inverted: Boolean(props.inverted),
  size: props.size,
  orientation: props.orientation,
  title: !!props.title || !!slots.title
}));
</script>

<template>
  <Primitive :as="as" :data-orientation="orientation" data-slot="root" :class="b24ui.root({ class: [props.b24ui?.root, props.class] })">
    <slot name="leading" :b24ui="b24ui">
      <Component
        :is="icon"
        v-if="icon"
        data-slot="icon"
        :class="b24ui.icon({ class: props.b24ui?.icon })"
      />
      <B24Avatar v-else-if="avatar" :size="props.b24ui?.avatarSize || b24ui.avatarSize()" v-bind="avatar" data-slot="avatar" :class="b24ui.avatar({ class: props.b24ui?.avatar })" />
    </slot>

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

      <div v-if="orientation === 'vertical' && (actions?.length || !!slots.actions)" data-slot="actions" :class="b24ui.actions({ class: props.b24ui?.actions })">
        <slot name="actions">
          <B24Button v-for="(action, index) in actions" :key="index" size="sm" v-bind="action" />
        </slot>
      </div>
    </div>

    <div v-if="orientation === 'horizontal' && (actions?.length || !!slots.actions) || close" data-slot="actions" :class="b24ui.actions({ class: props.b24ui?.actions, orientation: 'horizontal' })">
      <template v-if="orientation === 'horizontal' && (actions?.length || !!slots.actions)">
        <slot name="actions">
          <B24Button v-for="(action, index) in actions" :key="index" size="sm" v-bind="action" />
        </slot>
      </template>

      <slot name="close" :b24ui="b24ui">
        <B24Button
          v-if="close"
          :icon="closeIcon || icons.close"
          size="md"
          color="air-tertiary-no-accent"
          :aria-label="t('alert.close')"
          v-bind="typeof close === 'object' ? close : {}"
          data-slot="close"
          :class="b24ui.close({ class: props.b24ui?.close })"
          @click="emits('update:open', false)"
        />
      </slot>
    </div>
  </Primitive>
</template>
