import sharp from 'sharp'

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

export default (props: Props) => {
  const webpImage = 'src/assets/images/optimized/logo.webp'
  const avifImage = 'src/assets/images/optimized/logo.avif'
  const inputSrc = 'src/assets/images/logo.png'

  const convertLogo = async (props: Props) => {
    // const webpImage = 'src/assets/images/optimized/logo.webp'
    // const avifImage = 'src/assets/images/optimized/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 () => {
    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)
  }

  convertLogo(props)

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