import fs from "fs";
import path from "path";

import type { JsonStructure } from "../types/structures";
import { shouldIgnore, imgExtensions, getFileExtension } from "../utils/files";

export default function scaffoldJSON(
  dir: string,
  structure: JsonStructure
): void {
  if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir, { recursive: true });
  }

  Object.keys(structure).forEach((key) => {
    const fullPath = path.join(dir, key);
    const value: any = structure[key];

    // Skip ignored patterns during scaffolding
    if (shouldIgnore(fullPath, dir)) {
      return;
    }

    if (typeof value === "string") {
      const parentDir = path.dirname(fullPath);
      if (!fs.existsSync(parentDir)) {
        fs.mkdirSync(parentDir, { recursive: true });
      }

      if (imgExtensions.includes(getFileExtension(key))) {
        // Decode base64 image
        fs.writeFileSync(fullPath, Buffer.from(value.split(",")[1], "base64"));
      } else {
        fs.writeFileSync(fullPath, value, "utf8");
      }
    } else if (typeof value === "object" && value !== null) {
      scaffoldJSON(fullPath, value);
    }
  });
}