import { BestAgentError } from "@best-agent/foundation";
export function compilePrompt(prompt) {
    let remaining = prompt.trim();
    let requestedProfile;
    let delegatedProfile;
    let workflow;
    while (remaining.length > 0) {
        const { token, rest } = splitLeadingToken(remaining);
        if (token.startsWith("agent:")) {
            const profileName = token.slice("agent:".length).trim();
            if (!profileName) {
                throw new BestAgentError("PROMPT_INVALID", "Prompt directive agent:<profile> requires a profile name.");
            }
            if (requestedProfile || delegatedProfile || workflow) {
                throw new BestAgentError("PROMPT_INVALID", "Prompt directives may only declare one explicit execution profile.");
            }
            requestedProfile = profileName;
            remaining = rest;
            continue;
        }
        if (token.startsWith("delegate:")) {
            const profileName = token.slice("delegate:".length).trim();
            if (!profileName) {
                throw new BestAgentError("PROMPT_INVALID", "Prompt directive delegate:<profile> requires a profile name.");
            }
            if (delegatedProfile || requestedProfile || workflow) {
                throw new BestAgentError("PROMPT_INVALID", "Prompt directives may not mix delegate execution with another explicit profile selector.");
            }
            delegatedProfile = profileName;
            remaining = rest;
            continue;
        }
        if (token.startsWith("workflow:")) {
            const workflowName = token.slice("workflow:".length).trim();
            if (!workflowName) {
                throw new BestAgentError("PROMPT_INVALID", "Prompt directive workflow:<name> requires a workflow name.");
            }
            if (requestedProfile || delegatedProfile || workflow) {
                throw new BestAgentError("PROMPT_INVALID", "Prompt directives may not mix workflow execution with another explicit execution selector.");
            }
            if (![
                "build-verify",
                "coordinate",
                "deep-research",
                "explore-build",
                "plan-build-verify",
                "plan-explore-build-verify",
            ].includes(workflowName)) {
                throw new BestAgentError("PROMPT_INVALID", `Workflow ${workflowName} is not supported.`);
            }
            workflow = workflowName;
            remaining = rest;
            continue;
        }
        break;
    }
    const normalizedPrompt = remaining;
    const { token, rest } = splitLeadingToken(normalizedPrompt);
    if (token.startsWith("tool:")) {
        const toolName = token.slice("tool:".length).trim();
        if (!toolName) {
            throw new BestAgentError("PROMPT_INVALID", "Prompt directive tool:<name> requires a tool name.");
        }
        return {
            kind: "tool",
            rawPrompt: prompt,
            prompt: normalizedPrompt,
            requestedProfile,
            delegatedProfile,
            workflow,
            toolName,
            input: rest,
        };
    }
    return {
        kind: "message",
        rawPrompt: prompt,
        prompt: normalizedPrompt,
        requestedProfile,
        delegatedProfile,
        workflow,
        text: normalizedPrompt,
    };
}
function splitLeadingToken(input) {
    const firstSpace = input.indexOf(" ");
    if (firstSpace === -1) {
        return {
            token: input,
            rest: "",
        };
    }
    return {
        token: input.slice(0, firstSpace),
        rest: input.slice(firstSpace + 1).trimStart(),
    };
}
//# sourceMappingURL=compiler.js.map