import sharp from 'sharp'
import * as fs from 'fs'
import * as path from 'path'
import { consola } from 'consola'
;(async () => {
  try {
    const dirPath = path.resolve('../../../public')
    const webpImage = 'src/assets/images/optimized/logo.webp'
    const avifImage = 'src/assets/images/optimized/logo.avif'
    const inputSrc = 'src/assets/images/logo.png'

    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)

    if (fs.existsSync(dirPath)) {
      const inputSrc = '../../assets/images/logo.png'
      const favicon = dirPath + '/favicon.png'
      const faviconBuffer = await sharp(inputSrc).png({ quality: 90 }).resize(50).toBuffer()
      await sharp(faviconBuffer).toFile(favicon)
      consola.success('Favicon generated successfully')
    } else {
      consola.error('Directory does not exist:', dirPath)
    }
  } catch (error) {
    consola.error('Error generating favicon:', error)
  }
})()
