<script setup lang="ts">
import { joinURL } from 'ufo';

const runtimeConfig = useRuntimeConfig();
const cdnUrl = runtimeConfig.public.cdn_url;

const route = useRoute();

const props = defineProps<{
  mediaPath: string | null | undefined;
}>();

const media = computed(() => {
  const dotSplit = props.mediaPath?.split('.');
  const extension = dotSplit?.pop()?.toLowerCase();
  let type = null;
  if (extension === 'jpg') {
    type = 'image';
  } else if (extension === 'wmv') {
    type = 'video';
  }
  return { name: dotSplit?.join('.'), type };
});
</script>

<template>
  <div
    class="select-none flex-auto w-full *:object-contain *:w-full *:h-full *:max-h-full relative *:absolute"
    :class="route.path === '/exam' ? 'z-[-1]' : ''"
  >
    <NuxtImg
      v-if="media.type === 'image'"
      :key="`${mediaPath}-image`"
      provider="selfhost"
      :src="'/' + mediaPath"
      :alt="mediaPath ?? ''"
    />
    <video
      v-else-if="media.type === 'video'"
      :key="`${mediaPath}-video`"
      :autoplay="route.path === '/exam'"
      :controls="route.path === '/result'"
    >
      <source :src="joinURL(cdnUrl, media.name + '.mp4')" type="video/mp4" />
    </video>
    <span v-else class="text-5xl font-bold flex items-center justify-center"
      >Brak mediów</span
    >
  </div>
</template>
