import { parseMarkdownTable } from "../../../lib/parse-markdown-table";

export interface ParsedTransition {
  operation: string;
  from: string[];
  to: string;
  command: string;
}

export interface ParsedModelDoc {
  modelName: string;
  transitions: ParsedTransition[];
}

export function parseModelDoc(fileName: string, markdown: string): ParsedModelDoc {
  const modelName = fileName.charAt(0).toUpperCase() + fileName.slice(1);
  const transitions = parseMarkdownTable(markdown, "#### State Transitions")
    .filter((cells) => cells.length >= 4)
    .map(([operation, fromStr, to, command]) => ({
      operation,
      from: fromStr.split(",").map((s) => s.trim()),
      to,
      command,
    }));

  return { modelName, transitions };
}
