import { parseHelpDoc, validateFrontmatter, type HelpSection } from './help';

/**
 * Load all help sections from markdown files. `import: 'default'` guarantees the
 * glob values are raw strings (not `{ default: string }`). Invalid docs are skipped
 * with a console warning at runtime; the build-time validator (scripts/validate-help-docs.mjs)
 * is the hard gate.
 */
export function loadHelpSections(): HelpSection[] {
  const modules = import.meta.glob('../content/help/*.md', {
    eager: true,
    query: '?raw',
    import: 'default',
  }) as Record<string, string>;

  const sections: HelpSection[] = [];
  for (const [path, raw] of Object.entries(modules)) {
    try {
      const { data, body } = parseHelpDoc(raw);
      const result = validateFrontmatter(data, path);
      if (!result.ok || !result.value) {
        console.warn(`[help] skipping ${path}: ${result.errors.join('; ')}`);
        continue;
      }
      sections.push({ ...result.value, body });
    } catch (err) {
      console.warn(`[help] failed to parse ${path}:`, err);
    }
  }
  sections.sort((a, b) => a.order - b.order);
  return sections;
}
