import fs, { readdirSync } from "fs";
import { format, resolveConfig, resolveConfigFile } from "prettier";

async function run() {
  let path = "./src/assets/icons";
  let listing = readdirSync(path);

  let result = ["// Generated by npm run gen:icons\n"];

  listing.forEach((file) => {
    if (!file.endsWith(".svg")) return;

    const iconComponent = `Icon${ucfirst(file.replace(".svg", ""))}`;
    let content = fs.readFileSync(`${path}/${file}`, "utf8");
    // replace kebab-case with camelCase in attribute names
    // regex captures two or more groups of letters seperated by dashes, if followed by an equal sign
    content = content.replace(/[A-Za-z]+(-[A-Za-z]+)+(?==)/g, kebabToCamelCase);
    content = content.replace(/^<svg /, `<svg data-icon="${iconComponent}" {...props} `);
    //check for role="img" and add it if not present
    if (!content.includes("role=")) {
      content = content.replace(/^<svg /, '<svg role="img" ');
    }

    result.push(`export const ${iconComponent} = (props:React.SVGAttributes<SVGElement>) => (${content});\n`);
  });

  let s = result.join("\n");

  const configPath = await resolveConfigFile();
  const localOptions = await resolveConfig(configPath || "./");
  const options = localOptions ?? {};

  s = await format(s, { parser: "typescript", ...options });

  fs.writeFileSync("./src/components/generated/icons.tsx", s);
}

run().catch((e) => {
  console.error(e);
});

function ucfirst(s) {
  return s.charAt(0).toUpperCase() + s.slice(1);
}

function kebabToCamelCase(s) {
  const keepAsIs = ["aria-label"];

  if (keepAsIs.includes(s)) {
    return s;
  } else {
    return s.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
  }
}
