import { MCGrizzConf } from "../types/mcgrizzconf.ts";

const defaultConf: MCGrizzConf = {
  loader: 'unset',
}

const confPath = 'mcgrizz.json'

export function makeConfFile(): MCGrizzConf {

  Deno.writeTextFileSync(confPath, JSON.stringify(defaultConf, null, 2), {create: true});
  
  return defaultConf;
}

export function getConfFile(): MCGrizzConf {
  const conf = JSON.parse(Deno.readTextFileSync(confPath));

  if (!conf) {
    return makeConfFile();
  }

  return conf;
}

export async function updateConfFile(newConf: Partial<MCGrizzConf>) {
  const conf = {...getConfFile(), newConf};

  await Deno.writeTextFile(confPath, JSON.stringify(conf, null, 2));
}
