/**
 * Business-rules catalog parser. Thin wrapper over the shared catalog runtime
 * (`../_catalog-runtime/`): it pins the BR globs and narrows the parsed
 * frontmatter to the BR-specific `FileFrontmatter`. The parser logic itself
 * (heading regex, metadata-table parsing, body slicing, frontmatter
 * normalization) is no longer duplicated here.
 *
 * Per-rule structure expected (see docs/business-rules/README.md):
 *
 *   ## <ID> — <title>
 *
 *   | Campo      | Valor              |
 *   | ---------- | ------------------ |
 *   | Tipo       | `<type>`           |
 *   | Estado     | `<state>`          |
 *   | Severidad  | `<severity>`       |
 *   | Origen     | <text>             |
 *   | Trigger en | <optional>         |
 *   | Efecto en  | <optional>         |
 *
 * Anything past the table belongs to the rule body until the next `## ` heading
 * or end of file.
 */
import { parseCatalog as parseCatalogRuntime } from '../_catalog-runtime/parse';
import type { FileFrontmatter, ParsedFile } from './types';

// Re-exported so the rest of the BR scripts keep importing it from
// `./parse-catalog` unchanged (indirectly resolving to the shared runtime).
export { findRepoRoot } from '../_catalog-runtime/repo-root';

export interface ParseOptions {
  repoRoot: string;
}

export async function parseCatalog(opts: ParseOptions): Promise<ParsedFile[]> {
  // The runtime is generic over the frontmatter shape: by pinning
  // `FileFrontmatter` here the result is already typed, so no cast is needed.
  const files = await parseCatalogRuntime<FileFrontmatter>({
    repoRoot: opts.repoRoot,
    globs: ['cliter/*/business-rules/*.md'],
  });
  return files.map((file) => ({
    filePath: file.filePath,
    relativePath: file.relativePath,
    frontmatter: file.frontmatter,
    rules: file.rules,
  }));
}
