/**
 * Rasterise les SVG Ultimail (PNG + JPG) pour usage hors navigateur / réseaux sociaux.
 * Dépendance : sharp (devDependency).
 */
import { readFileSync } from "node:fs"
import { dirname, join } from "node:path"
import { fileURLToPath } from "node:url"
import sharp from "sharp"

const __dirname = dirname(fileURLToPath(import.meta.url))
const root = join(__dirname, "..")

const jobs = [
  { input: join(root, "public/ultimail-mark.svg"), outBase: "ultimail-mark", w: 256, h: 256 },
  {
    input: join(root, "public/brand/ultimail-wordmark-horizontal.svg"),
    outBase: "ultimail-wordmark-horizontal",
    w: 1600,
    h: 460,
  },
  {
    input: join(root, "public/brand/ultimail-wordmark-stacked.svg"),
    outBase: "ultimail-wordmark-stacked",
    w: 800,
    h: 800,
  },
]

async function main() {
  const outDir = join(root, "public/brand")
  for (const job of jobs) {
    const buf = readFileSync(job.input)
    const resize = { width: job.w, height: job.h, fit: "contain", background: { r: 255, g: 255, b: 255, alpha: 1 } }
    await sharp(buf).resize(resize).png({ compressionLevel: 9 }).toFile(join(outDir, `${job.outBase}.png`))
    await sharp(buf)
      .resize(resize)
      .flatten({ background: "#ffffff" })
      .jpeg({ quality: 92, mozjpeg: true })
      .toFile(join(outDir, `${job.outBase}.jpg`))
    console.log(`Wrote ${job.outBase}.png / .jpg`)
  }
}

main().catch((e) => {
  console.error(e)
  process.exit(1)
})
