import "./style.css";
import typescriptLogo from "./typescript.svg";
import viteLogo from "/vite.svg";
import { setupCounter } from "./counter.ts";

// document.querySelector<HTMLDivElement>('#app')!.innerHTML = `
//   <div>
//     <a href="https://vitejs.dev" target="_blank">
//       <img src="${viteLogo}" class="logo" alt="Vite logo" />
//     </a>
//     <a href="https://www.typescriptlang.org/" target="_blank">
//       <img src="${typescriptLogo}" class="logo vanilla" alt="TypeScript logo" />
//     </a>
//     <h1>Vite + TypeScript</h1>
//     <div class="card">
//       <button id="counter" type="button"></button>
//     </div>
//     <p class="read-the-docs">
//       Click on the Vite and TypeScript logos to learn more
//     </p>
//   </div>
// `

// setupCounter(document.querySelector<HTMLButtonElement>('#counter')!)

// index.ts

import fs from "fs/promises";
import { log } from "console";

const assetsPath = "./assets";
const tsPath = "./tsFiles";

async function main() {
  console.log(1111);

  const files = await fs.readdir(assetsPath);
  for (const file of files) {
    const filePath = path.join(assetsPath, file);
    const stat = await fs.stat(filePath);

    if (stat.isDirectory()) {
      console.log(`dir: ${filePath}`);
    } else if (stat.isFile() && ["svg", "png"].includes(path.extname(file))) {
      // 生成 ts files
      generateTsFile(filePath);
    }
  }
}

async function generateTsFile(filePath) {
  const fileContent = fs.readFile(filePath, "utf8");
  if (fileContent.includes("<svg")) {
    // svg文件内容
    const match = fileContent.match(/<svg[^>]*>(.*?)<\/svg>/s);
    if (match) {
      console.log(`icon ${filePath} created!`);
    }
  } else if (fileContent.includes(".png")) {
    // png文件内容
    const match = fileContent.match(/data:image\/png;,\s*base64\((.*?)\)/);
    if (match) {
      console.log(`icon ${filePath} created!`);
    }
  }
}

main().catch((error) => {
  console.error(error);
});
