import fs from 'fs';
import { geoMercator, geoPath } from 'd3-geo';
import { feature } from 'topojson-client';
import { chromium } from 'playwright-core';
const D = process.cwd();
const topo = JSON.parse(fs.readFileSync(D + '/node_modules/world-atlas/countries-50m.json'));
const countries = feature(topo, topo.objects.countries).features;
const b64 = f => fs.readFileSync(D + '/' + f).toString('base64');
const font = f => fs.readFileSync(D + '/node_modules/@fontsource/' + f).toString('base64');
const HISP = new Set([724, 620, 484, 320, 340, 222, 558, 188, 591, 192, 214, 630, 170, 862, 218, 604, 68, 152, 32, 858, 600, 76, 226].map(String).map(s => s.padStart(3, '0')));
const PHI = '608';
// main map: Americas
const W = 1080;
const proj = geoMercator().fitExtent([[80, 470], [640, 975]], {
type: 'MultiPoint', coordinates: [[-117, 32.5], [-60, 23], [-74, -55.5], [-34.8, -10]]
});
const path = geoPath(proj);
const iproj = geoMercator().fitExtent([[760, 455], [990, 645]], { type: 'MultiPoint', coordinates: [[-9.5, 36], [4.3, 43.8]] });
const ipath = geoPath(iproj);
const IBE = new Set(['724', '620']);
let canar = '';
const cproj = geoMercator().scale(iproj.scale()).center([-15.6, 28.3]).translate([742, 668]);
let hisp = '', iber = '';
for (const c of countries) {
if (c.id === PHI) continue;
if (IBE.has(c.id)) {
if (c.id === '724') {
const polys = c.geometry.coordinates;
const main = polys.filter(p => p[0].every(([x]) => x > -12));
const can = polys.filter(p => p[0].some(([x]) => x <= -12) && p[0].every(([, y]) => y > 26.5));
iber += ipath({ type: 'MultiPolygon', coordinates: main });
canar = geoPath(cproj)({ type: 'MultiPolygon', coordinates: can });
} else iber += ipath(c);
continue;
}
if (!HISP.has(c.id)) continue;
const d = path(c); if (!d) continue;
const [[x0, y0], [x1, y1]] = path.bounds(c);
if (x1 < 0 || x0 > W || y1 < 0 || y0 > W) continue;
hisp += d;
}
hisp += iber + canar;
const phi = countries.find(c => c.id === PHI);
const pproj = geoMercator().fitExtent([[905, 700], [1000, 880]], phi);
const phiPath = geoPath(pproj)(phi);
const P = iproj([-6.9, 37.2]), G = cproj([-17.2, 28.1]), S = proj([-74.5, 24.1]);
const route = `M${P[0]},${P[1]} Q${(P[0] + G[0]) / 2 + 10},${(P[1] + G[1]) / 2 + 5} ${G[0]},${G[1]} Q${(G[0] + S[0]) / 2},${Math.min(G[1], S[1]) - 70} ${S[0]},${S[1]}`;
const mid = [(G[0] + S[0]) / 2 + 10, (G[1] + S[1]) / 2 - 50];
// sun centre in mid-Atlantic
const sun = [645, 650];
let rays = '';
for (let i = 0; i < 36; i++) {
const a = i * Math.PI * 2 / 36, r0 = 70, r1 = i % 2 ? 190 : 270;
rays += `