import { Img } from '../atoms/img.js'
import { SVG } from '../atoms/svg.js'
import { Video } from '../atoms/video.js'
import { Audio } from '../atoms/audio.js'
import h from '../shared/h.js'

const AUDIO_EXTENSIONS = ['.mp3', '.m4a', '.ogg', '.wav', '.aac', '.flac']

export function Asset ({ _model, _tall, _fit, ...props }) {
  const { image, video } = _model

  if (AUDIO_EXTENSIONS.some(ext => video.src?.toLowerCase().endsWith(ext))) {
    return h(Audio, { _model: video, ...props })
  }

  if (video.src) {
    return h(Video, { ...props, _model: video, _tall, _fit })
  }

  return image.src?.toLowerCase().endsWith('.svg')
    ? h(SVG, { ...props, _model: image, _tall })
    : h(Img, { ...props, _model: image, _tall, _fit })
}
