import fs from "fs-extra";
import path from "node:path";
import { compile } from "./compile";
import { createPackageConfig } from "./create-package-config";
import { generateCSS } from "./generate-css";
import { generateDts } from "./generate-dts";
import { getPath } from "../utils/get-path";

export async function buildPackage() {
  const packagePath = getPath("");
  console.log("packagePath", packagePath);
  try {
    await generateDts();
    const config = await createPackageConfig();
    await compile(config);
    if (await fs.pathExists(path.join(packagePath, "esm/index.css"))) {
      await fs.copyFile(
        path.join(packagePath, "esm/index.css"),
        path.join(packagePath, "styles.css")
      );

      await fs.remove(path.join(packagePath, "esm/index.css"));
      await fs.remove(path.join(packagePath, "cjs/index.css"));
    }
    await generateCSS();
  } catch (err: any) {
    console.log("err", err);
    // TO_DO
  }
}
