const fs = require("fs");
const path = require("path");

const processData = process.argv;
// console.log("processData", processData);
const filePath = processData[2]
  ? `../components/${processData[2]}`
  : `../components`;
const pageDir = path.resolve(__dirname, filePath);

const folderize = () => {
  const directoryContent = fs.readdirSync(pageDir);
  //   console.log("directoryContent >>", directoryContent);
  if (directoryContent.length) {
    directoryContent.forEach((item) => {
      if (item.endsWith(".jsx")) {
        const fileName = item.replace(".jsx", "");
        // fs.mkdir(path.resolve(pageDir, fileName), (error) => {
        // if (!error) {
        const fileContent = fs.readFileSync(path.resolve(pageDir, item), {
          encoding: "utf-8",
        });

        fs.writeFileSync(path.resolve(pageDir, fileName, item), fileContent, {
          encoding: "utf-8",
        });
        fs.writeFileSync(
          path.resolve(pageDir, fileName, "index.js"),
          `
            import { lazy } from "react";

export const ${fileName} = lazy(() => import("./${fileName}"));

            `,
          { encoding: "utf-8" }
        );
        // console.log("fileContent", fileContent);
      }
    });

    // return `
    //     export const ${item.replace(".jsx", "")} = lazy(() => {
    //         const __import = import("${filePath}/${item.replace(
    //   ".jsx",
    //   ""
    // )}");
    //         __import.finally(() => {});
    //         return __import;
    //       });
    //     `;
    // }
    // });
    // const result = fs.appendFileSync(routePath, exportedPages.join("\n"));
    // console.log("result >>", result);
  }
};

folderize();
