import { copyFileSync, existsSync, mkdirSync, readdirSync } from "node:fs";
import { basename, join } from "node:path";
import { fileURLToPath } from "node:url";

const assetsDir = new URL("../dist/assets/", import.meta.url);
const assetsPath = fileURLToPath(assetsDir);

if (!existsSync(assetsPath)) process.exit(0);
mkdirSync(assetsPath, { recursive: true });

const files = readdirSync(assetsPath);
const currentJs = files.find((name) => /^index-[A-Za-z0-9_-]+\.js$/.test(name));
const currentCss = files.find((name) => /^index-[A-Za-z0-9_-]+\.css$/.test(name));

const legacyJs = [
  "index-3IoCMmqv.js",
  "index-DjmlsRNV.js",
  "index-7Fzi8kyG.js",
  "index-OAuthRedirectExacto.js",
  "index-oauthv3-ed1f63a671.js",
  "index-C1g15Gzk.js",
  "index-DvnNNw_U.js",
  "index-B7i36ZCk.js",
  "index-DcLwYapC.js",
  "index-k1Oz2sTE.js",
  "index-rurDav1q.js",
];

const legacyCss = [
  "index-DrJGHNJG.css",
  "index-CD8ZsNgz.css",
  "index-_4eSo35_.css",
  "index-CvdK_cH7.css",
  "index-DPuohwR6.css",
  "index-DGUCvIOo.css",
  "index-C-5g0-Hp.css",
];

function copyAliases(sourceName, aliases) {
  if (!sourceName) return;
  const source = join(assetsPath, sourceName);
  for (const alias of aliases) {
    if (alias === basename(source)) continue;
    copyFileSync(source, join(assetsPath, alias));
  }
}

copyAliases(currentJs, legacyJs);
copyAliases(currentCss, legacyCss);
