<script setup lang="ts">
import type { PageDataResourceGroup } from '#layers/types/api/pageData'
import { getColorCode } from '#layers/utils/styleUtil'
import { isTypeImage, isTypeText } from '#layers/utils/dataUtil'

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

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

const imagePaths = computed(() => getResourceSrc(props.resourcesData))
const displayText = computed(() => props.resourcesData?.display?.text)
const colorName = computed(() => props.resourcesData?.display?.color_name)
const colorCode = computed(() => props.resourcesData?.display?.color_code)

// HTML 콘텐츠 정리 (줄바꿈 처리)
const sanitizedContent = computed(() => {
  return displayText.value?.replace(/\n/g, '<br/>') || ''
})
</script>

<template>
  <AtomsImg
    v-if="isTypeImage(resourcesData?.resource_type) && imagePaths"
    :src="imagePaths"
    :alt="alt || displayText"
    :class="`w-full h-full object-${objectFit}`"
  />

  <span
    v-else-if="isTypeText(resourcesData?.resource_type)"
    v-dompurify-html="sanitizedContent"
    :style="{ color: getColorCode({ colorName, colorCode }) }"
    class="block"
  />
</template>
