<script setup lang="ts">
import { getResolvedHost } from '#layers/utils/styleUtil'
import type { PageDataResourceGroup } from '#layers/types/api/pageData'

interface Props {
  resourcesData?: PageDataResourceGroup
  objectFit?: 'contain' | 'cover'
  alt?: string
}

const props = withDefaults(defineProps<Props>(), {
  objectFit: 'contain',
  alt: 'image',
})

const imagePaths = computed(() => {
  if (!props.resourcesData?.res_path) return null

  const pc =
    props.resourcesData.res_path.path_pc ?? props.resourcesData.res_path.path_mo
  const mo =
    props.resourcesData.res_path.path_mo ?? props.resourcesData.res_path.path_pc
  return {
    pc: pc ? getResolvedHost(pc) : '',
    mo: mo ? getResolvedHost(mo) : '',
  }
})
</script>

<template>
  <picture v-if="imagePaths" ref="pictureRef">
    <source media="(min-width: 1024px)" :srcset="imagePaths.pc" />
    <source media="(max-width: 1023px)" :srcset="imagePaths.mo" />
    <img
      :src="imagePaths.pc"
      :alt="alt"
      :class="`w-full h-full object-${objectFit}`"
      loading="lazy"
    />
  </picture>
</template>
