// src/install/blocks/parsers.ts
import * as TOML from "@iarna/toml";
import CommentJSON from "comment-json";
import * as YAML from "yaml";
import type { ConfigShape } from "../Installer";

export interface ConfigParser {
  parse(content: string): unknown;
  serialize(value: unknown): string;
}

export const parsers: Record<ConfigShape, ConfigParser> = {
  toml: {
    parse: (c) => TOML.parse(c),
    serialize: (v) => TOML.stringify(v as TOML.JsonMap),
  },
  json: {
    parse: (c) => CommentJSON.parse(c),
    serialize: (v) => CommentJSON.stringify(v, null, 2) + "\n",
  },
  yaml: {
    parse: (c) => YAML.parse(c),
    serialize: (v) => YAML.stringify(v),
  },
};
