#!/usr/bin/env node
const fs = require("fs");
const path = require("path");

const file = path.join(__dirname, "..", "index.html");
let html = fs.readFileSync(file, "utf8");

const gradientMap = [
  ["linear-gradient(180deg,#252f44 0%,#1e2739 100%)", "var(--site-surface-gradient)"],
  ["linear-gradient(180deg,#252f44 0%,#1e2739 38%,#181f2e 100%)", "var(--site-surface-gradient)"],
  ["linear-gradient(0deg,#181f2e 0%,#1e2739 100%)", "var(--site-surface-gradient)"],
  ["linear-gradient(180deg,#1e2739,#181f2e)", "var(--site-surface-gradient)"],
  ["linear-gradient(180deg,#252f44,#181f2e)", "var(--site-surface-gradient)"],
  ["linear-gradient(180deg,#1e2739 0%,#181f2e 100%)", "var(--site-surface-gradient)"],
  ["linear-gradient(145deg,#252f44 0%,#1e2739 42%,#181f2e 100%)", "var(--site-panel-gradient)"],
  ["linear-gradient(165deg,#2c374f 0%,#1e2739 100%)", "var(--site-surface-gradient)"],
  ["linear-gradient(165deg,#2c374f 0%,#1e2739 48%,#181f2e 100%)", "var(--site-panel-gradient)"],
  ["linear-gradient(165deg,#2c374f 0%,#1e2739 55%,#181f2e 100%)", "var(--site-panel-gradient)"],
  ["linear-gradient(165deg,#252f44 0%,#181f2e 58%,#141a28 100%)", "var(--site-panel-gradient)"],
  ["linear-gradient(180deg,#2e3544 0%,#252f44 55%,#1e2739 100%)", "var(--site-raised-gradient)"],
  ["linear-gradient(180deg,#2e3544 0%,#252f44 100%)", "var(--site-raised-gradient)"],
  ["linear-gradient(165deg,#181f2e 0%,#1e2739 48%,#0c121c 100%)", "var(--site-panel-gradient)"],
  [
    "linear-gradient(165deg,#2c374f 0%,#1e2739 42%,#181f2e 100%) padding-box",
    "var(--site-panel-gradient) padding-box",
  ],
  ["linear-gradient(165deg,rgba(27,37,56,.98) 0%,rgba(20,27,44,.99) 100%)", "var(--site-surface-gradient)"],
  ["linear-gradient(180deg,#2a3a52 0%,#2c374f 55%,#252f44 100%)", "var(--site-elevated-gradient)"],
];

for (const [from, to] of gradientMap) {
  html = html.split(from).join(to);
}

html = html.replace(/background:#1e2739/g, "background:var(--site-surface-gradient)");
html = html.replace(/background:#252f44/g, "background:var(--site-raised-gradient)");
html = html.replace(/background:#141a28/g, "background:var(--site-deep)");
html = html.replace(/background:#181f2e/g, "background:var(--site-bg)");
html = html.replace(/rgba\(15,24,40/g, "rgba(18,24,36");
html = html.replace(
  /linear-gradient\(180deg,#181f2e 0%,#141a28 100%\)/g,
  "linear-gradient(180deg,var(--site-bg) 0%,var(--site-deep) 100%)",
);

html = html.replace(
  /\.home-quick-cell\{([^}]*?)background:rgba\(56,89,130,\.1\);/,
  ".home-quick-cell{$1background:var(--site-surface-gradient);",
);

html = html.replace(
  /content="#161f33"/,
  'content="#181f2e"',
);

fs.writeFileSync(file, html);
console.log("index.html theme palette updated");
