/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { z } from 'zod';
import { parse as parseYaml, normalizeContent } from '@hoptrendy/hopcode-core';
/**
 * Defines the Zod schema for a Markdown command definition file.
 * The frontmatter contains optional metadata, and the body is the prompt.
 */
export const MarkdownCommandDefSchema = z.object({
    frontmatter: z
        .object({
        description: z.string().optional(),
        'argument-hint': z.string().optional(),
        when_to_use: z.string().optional(),
        'disable-model-invocation': z.boolean().optional(),
    })
        .passthrough()
        .optional(),
    prompt: z.string({
        required_error: 'The prompt content is required.',
        invalid_type_error: 'The prompt content must be a string.',
    }),
});
/**
 * Parses a Markdown command file with optional YAML frontmatter.
 * @param content The file content
 * @returns Parsed command definition with frontmatter and prompt
 */
export function parseMarkdownCommand(content) {
    const normalizedContent = normalizeContent(content);
    // Match YAML frontmatter pattern: ---\n...\n---\n
    // Allow empty frontmatter: ---\n---\n
    const frontmatterRegex = /^---\n(?:([\s\S]*?)\n)?---(?:\n|$)([\s\S]*)$/;
    const match = normalizedContent.match(frontmatterRegex);
    if (!match) {
        // No frontmatter, entire content is the prompt
        return {
            prompt: normalizedContent.trim(),
        };
    }
    const [, frontmatterYaml = '', body] = match;
    // Parse YAML frontmatter if not empty
    let frontmatter;
    if (frontmatterYaml.trim()) {
        try {
            frontmatter = parseYaml(frontmatterYaml);
        }
        catch (error) {
            throw new Error(`Failed to parse YAML frontmatter: ${error instanceof Error ? error.message : String(error)}`);
        }
    }
    return {
        frontmatter,
        prompt: body.trim(),
    };
}
//# sourceMappingURL=markdown-command-parser.js.map