import fs from 'fs';
import path from 'path';
import sharp from 'sharp';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const rootDir = path.resolve(__dirname, '..');
const publicDir = path.join(rootDir, 'public');
const splashDir = path.join(publicDir, 'apple-splash');
const splashScreens = [
{ width: 1320, height: 2868 },
{ width: 2868, height: 1320 },
{ width: 1290, height: 2796 },
{ width: 2796, height: 1290 },
{ width: 1179, height: 2556 },
{ width: 2556, height: 1179 },
{ width: 1170, height: 2532 },
{ width: 2532, height: 1170 },
{ width: 1125, height: 2436 },
{ width: 2436, height: 1125 },
{ width: 1242, height: 2688 },
{ width: 2688, height: 1242 },
{ width: 828, height: 1792 },
{ width: 1792, height: 828 },
{ width: 1536, height: 2048 },
{ width: 2048, height: 1536 },
{ width: 1668, height: 2388 },
{ width: 2388, height: 1668 },
{ width: 1640, height: 2360 },
{ width: 2360, height: 1640 },
{ width: 2048, height: 2732 },
{ width: 2732, height: 2048 },
];
fs.mkdirSync(publicDir, { recursive: true });
fs.mkdirSync(splashDir, { recursive: true });
const iconSvg = (size, maskable = false) => {
const inset = Math.round(size * (maskable ? 0.035 : 0.08));
const innerSize = size - inset * 2;
const innerRadius = Math.round(size * (maskable ? 0.24 : 0.19));
return ``;
};
const splashSvg = (width, height) => {
const portrait = height >= width;
const iconBox = Math.round(Math.min(width, height) * (portrait ? 0.28 : 0.22));
const iconX = Math.round((width - iconBox) / 2);
const iconY = Math.round(height * (portrait ? 0.22 : 0.18));
const titleSize = Math.round(Math.min(width, height) * (portrait ? 0.075 : 0.06));
const subtitleSize = Math.round(titleSize * 0.3);
const titleY = iconY + iconBox + Math.round(titleSize * 1.35);
const subtitleY = titleY + Math.round(subtitleSize * 1.9);
const logo = iconSvg(iconBox, false).replace('`;
};
const writePng = async (filePath, svg, size = null) => {
let image = sharp(Buffer.from(svg));
if (size) image = image.resize(size, size);
await image.png().toFile(filePath);
};
await writePng(path.join(publicDir, 'icon-192.png'), iconSvg(192), 192);
await writePng(path.join(publicDir, 'icon-512.png'), iconSvg(512), 512);
await writePng(path.join(publicDir, 'maskable-icon-192.png'), iconSvg(192, true), 192);
await writePng(path.join(publicDir, 'maskable-icon-512.png'), iconSvg(512, true), 512);
await writePng(path.join(publicDir, 'apple-touch-icon.png'), iconSvg(180), 180);
for (const screen of splashScreens) {
await sharp(Buffer.from(splashSvg(screen.width, screen.height))).png().toFile(path.join(splashDir, `${screen.width}x${screen.height}.png`));
}