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

<script setup>
import { computed } from "vue";
import { Primitive } from "reka-ui";
import { useAppConfig } from "#imports";
import { useComponentProps } from "../composables/useComponentProps";
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: Object, required: false }
});
const emits = defineEmits(["update:open"]);
const slots = defineSlots();
const props = useComponentProps("alert", _props);
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="props.as" :data-orientation="props.orientation" data-slot="root" :class="b24ui.root({ class: [props.b24ui?.root, props.class] })">
    <slot name="leading" :b24ui="b24ui">
      <Component
        :is="props.icon"
        v-if="props.icon"
        data-slot="icon"
        :class="b24ui.icon({ class: props.b24ui?.icon })"
      />
      <B24Avatar v-else-if="props.avatar" :size="props.b24ui?.avatarSize || b24ui.avatarSize()" v-bind="props.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="props.title || !!slots.title" data-slot="title" :class="b24ui.title({ class: props.b24ui?.title })">
        <slot name="title">
          {{ props.title }}
        </slot>
      </div>
      <div v-if="props.description || !!slots.description" data-slot="description" :class="b24ui.description({ class: props.b24ui?.description })">
        <slot name="description">
          {{ props.description }}
        </slot>
      </div>

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

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

      <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('alert.close')"
          v-bind="typeof props.close === 'object' ? props.close : {}"
          data-slot="close"
          :class="b24ui.close({ class: props.b24ui?.close })"
          @click="emits('update:open', false)"
        />
      </slot>
    </div>
  </Primitive>
</template>
