import { cp, mkdir, rm } from "node:fs/promises";
import { join } from "node:path";

const projectRoot = process.cwd();
const vendorRoot = join(projectRoot, "dist", "vendor");
const assetsRoot = join(projectRoot, "dist", "assets");

async function copyFile(source: string, target: string): Promise<void> {
  await mkdir(join(target, ".."), { recursive: true });
  await cp(source, target);
}

async function copyStaticAssets(): Promise<void> {
  await rm(vendorRoot, { recursive: true, force: true });
  await rm(assetsRoot, { recursive: true, force: true });

  await copyFile(
    join(projectRoot, "node_modules", "leaflet", "dist", "leaflet.css"),
    join(vendorRoot, "leaflet", "leaflet.css")
  );
  await copyFile(
    join(projectRoot, "node_modules", "leaflet", "dist", "leaflet.js"),
    join(vendorRoot, "leaflet", "leaflet.js")
  );
  await cp(
    join(projectRoot, "node_modules", "leaflet", "dist", "images"),
    join(vendorRoot, "leaflet", "images"),
    { recursive: true }
  );
  await copyFile(
    join(projectRoot, "node_modules", "exifr", "dist", "full.esm.mjs"),
    join(vendorRoot, "exifr", "full.esm.mjs")
  );
  await cp(
    join(projectRoot, "src", "assets"),
    assetsRoot,
    { recursive: true }
  );
}

await copyStaticAssets();
