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

export function readDatabaseUrl(stateDir) {
  return readStateValue(path.join(stateDir, "database_url"));
}

export function readStateValue(filePath) {
  if (!fs.existsSync(filePath)) return null;
  return fs.readFileSync(filePath, "utf8").trim();
}

export function printStateDir(dir, indent) {
  for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
    const filePath = path.join(dir, entry.name);
    if (entry.isDirectory()) {
      console.log(`${indent}${entry.name}/`);
      printStateDir(filePath, `${indent}  `);
      continue;
    }
    const value =
      entry.name === "password" ? "********" : fs.readFileSync(filePath, "utf8").trim();
    console.log(`${indent}${entry.name}: ${value}`);
  }
}
