import { revive } from '../../../../base/common/marshalling.js';
import { OffsetRange } from '../../../../editor/common/core/offsetRange.js';
function getPromptText(request) {
    const message = ( request.parts.map(r => r.promptText)).join('').trimStart();
    const diff = request.text.length - message.length;
    return { message, diff };
}
class ChatRequestTextPart {
    static { this.Kind = 'text'; }
    constructor(range, editorRange, text) {
        this.range = range;
        this.editorRange = editorRange;
        this.text = text;
        this.kind = ChatRequestTextPart.Kind;
    }
    get promptText() {
        return this.text;
    }
}
const chatVariableLeader = '#';
const chatAgentLeader = '@';
const chatSubcommandLeader = '/';
class ChatRequestVariablePart {
    static { this.Kind = 'var'; }
    constructor(range, editorRange, variableName, variableArg) {
        this.range = range;
        this.editorRange = editorRange;
        this.variableName = variableName;
        this.variableArg = variableArg;
        this.kind = ChatRequestVariablePart.Kind;
    }
    get text() {
        const argPart = this.variableArg ? `:${this.variableArg}` : '';
        return `${chatVariableLeader}${this.variableName}${argPart}`;
    }
    get promptText() {
        return this.text;
    }
}
class ChatRequestAgentPart {
    static { this.Kind = 'agent'; }
    constructor(range, editorRange, agent) {
        this.range = range;
        this.editorRange = editorRange;
        this.agent = agent;
        this.kind = ChatRequestAgentPart.Kind;
    }
    get text() {
        return `${chatAgentLeader}${this.agent.name}`;
    }
    get promptText() {
        return '';
    }
    toJSON() {
        return {
            kind: this.kind,
            range: this.range,
            editorRange: this.editorRange,
            agent: {
                id: this.agent.id,
                name: this.agent.name,
                description: this.agent.description,
                metadata: this.agent.metadata
            }
        };
    }
}
class ChatRequestAgentSubcommandPart {
    static { this.Kind = 'subcommand'; }
    constructor(range, editorRange, command) {
        this.range = range;
        this.editorRange = editorRange;
        this.command = command;
        this.kind = ChatRequestAgentSubcommandPart.Kind;
    }
    get text() {
        return `${chatSubcommandLeader}${this.command.name}`;
    }
    get promptText() {
        return '';
    }
}
class ChatRequestSlashCommandPart {
    static { this.Kind = 'slash'; }
    constructor(range, editorRange, slashCommand) {
        this.range = range;
        this.editorRange = editorRange;
        this.slashCommand = slashCommand;
        this.kind = ChatRequestSlashCommandPart.Kind;
    }
    get text() {
        return `${chatSubcommandLeader}${this.slashCommand.command}`;
    }
    get promptText() {
        return `${chatSubcommandLeader}${this.slashCommand.command}`;
    }
}
class ChatRequestDynamicVariablePart {
    static { this.Kind = 'dynamic'; }
    constructor(range, editorRange, text, data) {
        this.range = range;
        this.editorRange = editorRange;
        this.text = text;
        this.data = data;
        this.kind = ChatRequestDynamicVariablePart.Kind;
    }
    get referenceText() {
        return this.text.replace(chatVariableLeader, '');
    }
    get promptText() {
        return this.text;
    }
}
function reviveParsedChatRequest(serialized) {
    return {
        text: serialized.text,
        parts: ( serialized.parts.map(part => {
            if (part.kind === ChatRequestTextPart.Kind) {
                return ( new ChatRequestTextPart(( new OffsetRange(part.range.start, part.range.endExclusive)), part.editorRange, part.text));
            }
            else if (part.kind === ChatRequestVariablePart.Kind) {
                return ( new ChatRequestVariablePart(( new OffsetRange(part.range.start, part.range.endExclusive)), part.editorRange, part.variableName, part.variableArg));
            }
            else if (part.kind === ChatRequestAgentPart.Kind) {
                let agent = part.agent;
                if (!('name' in agent)) {
                    agent = {
                        ...agent,
                        name: agent.id
                    };
                }
                return ( new ChatRequestAgentPart(( new OffsetRange(part.range.start, part.range.endExclusive)), part.editorRange, agent));
            }
            else if (part.kind === ChatRequestAgentSubcommandPart.Kind) {
                return ( new ChatRequestAgentSubcommandPart(( new OffsetRange(part.range.start, part.range.endExclusive)), part.editorRange, part.command));
            }
            else if (part.kind === ChatRequestSlashCommandPart.Kind) {
                return ( new ChatRequestSlashCommandPart(( new OffsetRange(part.range.start, part.range.endExclusive)), part.editorRange, part.slashCommand));
            }
            else if (part.kind === ChatRequestDynamicVariablePart.Kind) {
                return ( new ChatRequestDynamicVariablePart(( new OffsetRange(part.range.start, part.range.endExclusive)), part.editorRange, part.text, revive(part.data)));
            }
            else {
                throw ( new Error(`Unknown chat request part: ${part.kind}`));
            }
        }))
    };
}
function extractAgentAndCommand(parsed) {
    const agentPart = parsed.parts.find((r) => r instanceof ChatRequestAgentPart);
    const commandPart = parsed.parts.find((r) => r instanceof ChatRequestAgentSubcommandPart);
    return { agentPart, commandPart };
}
export { ChatRequestAgentPart, ChatRequestAgentSubcommandPart, ChatRequestDynamicVariablePart, ChatRequestSlashCommandPart, ChatRequestTextPart, ChatRequestVariablePart, chatAgentLeader, chatSubcommandLeader, chatVariableLeader, extractAgentAndCommand, getPromptText, reviveParsedChatRequest };
