import sharp from 'sharp'

interface Props {
  size?: number
  favicon?: boolean
}

const convertLogo = async (props: Props) => {
  const inputSrc = 'src/assets/images/logo.png'
  const webpImage = 'fwt/images/logo.webp'
  const avifImage = 'fwt/images/logo.avif'

  const avifBuffer = await sharp(inputSrc).avif({ quality: 60 }).resize(props.size).toBuffer()
  await sharp(avifBuffer).toFile(avifImage)

  const webpBuffer = await sharp(inputSrc).webp({ quality: 75 }).resize(props.size).toBuffer()
  await sharp(webpBuffer).toFile(webpImage)
}

const generateFavicon = async (props: Props) => {
  const inputSrc = 'src/assets/images/logo.png'
  const favicon = 'public/favicon.png'

  const faviconBuffer = await sharp(inputSrc).png({ quality: 90 }).resize(50).toBuffer()
  await sharp(faviconBuffer).toFile(favicon)
}

export default (props: Props) => {
  convertLogo(props)

  if (props.favicon) {
    generateFavicon(props)
  }
}
