const { log } = require("console");
const fs = require("fs");
const path = require("path");
const { parse } = require("lua-json");
const folderPath = "./luaConfigs";

const scope = [
  // "MergeTotal",
  "ActivitiesConf",
].filter(Boolean);

const start = () => {
  const files = fs.readdirSync(folderPath);

  // 过滤出所有的 JSON 文件
  const luaFiles = files.filter((file) => path.extname(file) === ".config").filter((file) => scope.map((e) => `${e}.config`).includes(file));
  luaFiles.forEach((file) => {
    const luaContent = fs.readFileSync(path.join(folderPath, file), "utf8");
    const filename = path.basename(file, path.extname(file));
    const content = luaContent.replace(filename, "").slice(1);
    const luaConfig = parse(`return ${content}`);
    for (const key in luaConfig) {
      const item = luaConfig[key];
      const arr = [];
      Object.entries(item).forEach(([k, v]) => {
        if (!isNaN(k) && typeof v === "object") {
          arr.push(v);
        }
      });
      if (arr.length > 0) {
        luaConfig[key] = arr;
      }
    }
    fs.writeFileSync(`./configs/${filename}.json`, JSON.stringify(luaConfig, "utf-8"));
  });
};
start();
