
import { DisposableStore } from '../../../../../base/common/lifecycle.js';
import '../../../../../base/common/observableInternal/index.js';
import { hasKey } from '../../../../../base/common/types.js';
import { URI } from '../../../../../base/common/uri.js';
import { Range } from '../../../../../editor/common/core/range.js';
import { autorun, autorunSelfDisposable } from '../../../../../base/common/observableInternal/reactions/autorun.js';

var ChatErrorLevel;
(function(ChatErrorLevel) {
    ChatErrorLevel[ChatErrorLevel["Info"] = 0] = "Info";
    ChatErrorLevel[ChatErrorLevel["Warning"] = 1] = "Warning";
    ChatErrorLevel[ChatErrorLevel["Error"] = 2] = "Error";
})(ChatErrorLevel || (ChatErrorLevel = {}));
function isIDocumentContext(obj) {
    return (!!obj && typeof obj === "object" && "uri" in obj && obj.uri instanceof URI && "version" in obj && typeof obj.version === "number" && "ranges" in obj && Array.isArray(obj.ranges) && obj.ranges.every(Range.isIRange));
}
function isIUsedContext(obj) {
    return (!!obj && typeof obj === "object" && "documents" in obj && Array.isArray(obj.documents) && obj.documents.every(isIDocumentContext));
}
function isChatContentVariableReference(obj) {
    return !!obj && typeof obj === "object" && typeof obj.variableName === "string";
}
var ChatResponseReferencePartStatusKind;
(function(ChatResponseReferencePartStatusKind) {
    ChatResponseReferencePartStatusKind[ChatResponseReferencePartStatusKind["Complete"] = 1] = "Complete";
    ChatResponseReferencePartStatusKind[ChatResponseReferencePartStatusKind["Partial"] = 2] = "Partial";
    ChatResponseReferencePartStatusKind[ChatResponseReferencePartStatusKind["Omitted"] = 3] = "Omitted";
})(
    ChatResponseReferencePartStatusKind || (ChatResponseReferencePartStatusKind = {})
);
var ChatResponseClearToPreviousToolInvocationReason;
(function(ChatResponseClearToPreviousToolInvocationReason) {
    ChatResponseClearToPreviousToolInvocationReason[ChatResponseClearToPreviousToolInvocationReason["NoReason"] = 0] = "NoReason";
    ChatResponseClearToPreviousToolInvocationReason[ChatResponseClearToPreviousToolInvocationReason["FilteredContentRetry"] = 1] = "FilteredContentRetry";
    ChatResponseClearToPreviousToolInvocationReason[ChatResponseClearToPreviousToolInvocationReason["CopyrightContentRetry"] = 2] = "CopyrightContentRetry";
})(
    ChatResponseClearToPreviousToolInvocationReason || (ChatResponseClearToPreviousToolInvocationReason = {})
);
var ElicitationState;
(function(ElicitationState) {
    ElicitationState["Pending"] = "pending";
    ElicitationState["Accepted"] = "accepted";
    ElicitationState["Rejected"] = "rejected";
})(ElicitationState || (ElicitationState = {}));
function isLegacyChatTerminalToolInvocationData(data) {
    return !!data && typeof data === "object" && "command" in data && "language" in data;
}
var ToolConfirmKind;
(function(ToolConfirmKind) {
    ToolConfirmKind[ToolConfirmKind["Denied"] = 0] = "Denied";
    ToolConfirmKind[ToolConfirmKind["ConfirmationNotNeeded"] = 1] = "ConfirmationNotNeeded";
    ToolConfirmKind[ToolConfirmKind["Setting"] = 2] = "Setting";
    ToolConfirmKind[ToolConfirmKind["LmServicePerTool"] = 3] = "LmServicePerTool";
    ToolConfirmKind[ToolConfirmKind["UserAction"] = 4] = "UserAction";
    ToolConfirmKind[ToolConfirmKind["Skipped"] = 5] = "Skipped";
})(ToolConfirmKind || (ToolConfirmKind = {}));
var IChatToolInvocation;
(function(IChatToolInvocation) {
    let StateKind;
    (function(StateKind) {
        StateKind[StateKind["Streaming"] = 0] = "Streaming";
        StateKind[StateKind["WaitingForConfirmation"] = 1] = "WaitingForConfirmation";
        StateKind[StateKind["Executing"] = 2] = "Executing";
        StateKind[StateKind["WaitingForPostApproval"] = 3] = "WaitingForPostApproval";
        StateKind[StateKind["Completed"] = 4] = "Completed";
        StateKind[StateKind["Cancelled"] = 5] = "Cancelled";
    })(
        StateKind = IChatToolInvocation.StateKind || (IChatToolInvocation.StateKind = {})
    );
    function executionConfirmedOrDenied(invocation, reader) {
        if (invocation.kind === "toolInvocationSerialized") {
            if (invocation.isConfirmed === undefined || typeof invocation.isConfirmed === "boolean") {
                return {
                    type: invocation.isConfirmed ? ToolConfirmKind.UserAction : ToolConfirmKind.Denied
                };
            }
            return invocation.isConfirmed;
        }
        const state = invocation.state.read(reader);
        if (state.type === StateKind.Streaming || state.type === StateKind.WaitingForConfirmation) {
            return undefined;
        }
        if (state.type === StateKind.Cancelled) {
            return {
                type: state.reason
            };
        }
        return state.confirmed;
    }
    IChatToolInvocation.executionConfirmedOrDenied = executionConfirmedOrDenied;
    function awaitConfirmation(invocation, token) {
        const reason = executionConfirmedOrDenied(invocation);
        if (reason) {
            return Promise.resolve(reason);
        }
        const store = ( new DisposableStore());
        return ( new Promise(resolve => {
            if (token) {
                store.add(token.onCancellationRequested(() => {
                    resolve({
                        type: ToolConfirmKind.Denied
                    });
                }));
            }
            store.add(autorun(reader => {
                const reason = executionConfirmedOrDenied(invocation, reader);
                if (reason) {
                    store.dispose();
                    resolve(reason);
                }
            }));
        })).finally(() => {
            store.dispose();
        });
    }
    IChatToolInvocation.awaitConfirmation = awaitConfirmation;
    function postApprovalConfirmedOrDenied(invocation, reader) {
        const state = invocation.state.read(reader);
        if (state.type === StateKind.Completed) {
            return state.postConfirmed || {
                type: ToolConfirmKind.ConfirmationNotNeeded
            };
        }
        if (state.type === StateKind.Cancelled) {
            return {
                type: state.reason
            };
        }
        return undefined;
    }
    function confirmWith(invocation, reason) {
        const state = invocation?.state.get();
        if (state?.type === StateKind.WaitingForConfirmation || state?.type === StateKind.WaitingForPostApproval) {
            state.confirm(reason);
            return true;
        }
        return false;
    }
    IChatToolInvocation.confirmWith = confirmWith;
    function awaitPostConfirmation(invocation, token) {
        const reason = postApprovalConfirmedOrDenied(invocation);
        if (reason) {
            return Promise.resolve(reason);
        }
        const store = ( new DisposableStore());
        return ( new Promise(resolve => {
            if (token) {
                store.add(token.onCancellationRequested(() => {
                    resolve({
                        type: ToolConfirmKind.Denied
                    });
                }));
            }
            store.add(autorun(reader => {
                const reason = postApprovalConfirmedOrDenied(invocation, reader);
                if (reason) {
                    store.dispose();
                    resolve(reason);
                }
            }));
        })).finally(() => {
            store.dispose();
        });
    }
    IChatToolInvocation.awaitPostConfirmation = awaitPostConfirmation;
    function resultDetails(invocation, reader) {
        if (invocation.kind === "toolInvocationSerialized") {
            return invocation.resultDetails;
        }
        const state = invocation.state.read(reader);
        if (state.type === StateKind.Completed || state.type === StateKind.WaitingForPostApproval) {
            return state.resultDetails;
        }
        return undefined;
    }
    IChatToolInvocation.resultDetails = resultDetails;
    function isComplete(invocation, reader) {
        if (invocation.kind === "toolInvocationSerialized") {
            return true;
        }
        const state = invocation.state.read(reader);
        return state.type === StateKind.Completed || state.type === StateKind.Cancelled;
    }
    IChatToolInvocation.isComplete = isComplete;
    function isStreaming(invocation, reader) {
        if (invocation.kind === "toolInvocationSerialized") {
            return false;
        }
        const state = invocation.state.read(reader);
        return state.type === StateKind.Streaming;
    }
    IChatToolInvocation.isStreaming = isStreaming;
    function getParameters(invocation, reader) {
        if (invocation.kind === "toolInvocationSerialized") {
            return undefined;
        }
        const state = invocation.state.read(reader);
        if (state.type === StateKind.Streaming) {
            return undefined;
        }
        return state.parameters;
    }
    IChatToolInvocation.getParameters = getParameters;
    function getConfirmationMessages(invocation, reader) {
        if (invocation.kind === "toolInvocationSerialized") {
            return undefined;
        }
        const state = invocation.state.read(reader);
        if (state.type === StateKind.Streaming) {
            return undefined;
        }
        return state.confirmationMessages;
    }
    IChatToolInvocation.getConfirmationMessages = getConfirmationMessages;
})(IChatToolInvocation || (IChatToolInvocation = {}));
class ChatMcpServersStarting {
    get isEmpty() {
        const s = this.state.get();
        return !s.working && s.serversRequiringInteraction.length === 0;
    }
    constructor(state) {
        this.state = state;
        this.kind = "mcpServersStarting";
        this.didStartServerIds = [];
    }
    wait() {
        return ( new Promise(resolve => {
            autorunSelfDisposable(reader => {
                const s = this.state.read(reader);
                if (!s.working) {
                    reader.dispose();
                    resolve(s);
                }
            });
        }));
    }
    toJSON() {
        return {
            kind: "mcpServersStarting",
            didStartServerIds: this.didStartServerIds
        };
    }
}
function isChatFollowup(obj) {
    return (!!obj && obj.kind === "reply" && typeof obj.message === "string" && typeof obj.agentId === "string");
}
var ChatAgentVoteDirection;
(function(ChatAgentVoteDirection) {
    ChatAgentVoteDirection[ChatAgentVoteDirection["Down"] = 0] = "Down";
    ChatAgentVoteDirection[ChatAgentVoteDirection["Up"] = 1] = "Up";
})(ChatAgentVoteDirection || (ChatAgentVoteDirection = {}));
var ChatAgentVoteDownReason;
(function(ChatAgentVoteDownReason) {
    ChatAgentVoteDownReason["IncorrectCode"] = "incorrectCode";
    ChatAgentVoteDownReason["DidNotFollowInstructions"] = "didNotFollowInstructions";
    ChatAgentVoteDownReason["IncompleteCode"] = "incompleteCode";
    ChatAgentVoteDownReason["MissingContext"] = "missingContext";
    ChatAgentVoteDownReason["PoorlyWrittenOrFormatted"] = "poorlyWrittenOrFormatted";
    ChatAgentVoteDownReason["RefusedAValidRequest"] = "refusedAValidRequest";
    ChatAgentVoteDownReason["OffensiveOrUnsafe"] = "offensiveOrUnsafe";
    ChatAgentVoteDownReason["Other"] = "other";
    ChatAgentVoteDownReason["WillReportIssue"] = "willReportIssue";
})(ChatAgentVoteDownReason || (ChatAgentVoteDownReason = {}));
var ChatCopyKind;
(function(ChatCopyKind) {
    ChatCopyKind[ChatCopyKind["Action"] = 1] = "Action";
    ChatCopyKind[ChatCopyKind["Toolbar"] = 2] = "Toolbar";
})(ChatCopyKind || (ChatCopyKind = {}));
function convertLegacyChatSessionTiming(timing) {
    if (hasKey(timing, {
        created: true
    })) {
        return timing;
    }
    return {
        created: timing.startTime,
        lastRequestStarted: timing.startTime,
        lastRequestEnded: timing.endTime
    };
}
var ResponseModelState;
(function(ResponseModelState) {
    ResponseModelState[ResponseModelState["Pending"] = 0] = "Pending";
    ResponseModelState[ResponseModelState["Complete"] = 1] = "Complete";
    ResponseModelState[ResponseModelState["Cancelled"] = 2] = "Cancelled";
    ResponseModelState[ResponseModelState["Failed"] = 3] = "Failed";
    ResponseModelState[ResponseModelState["NeedsInput"] = 4] = "NeedsInput";
})(ResponseModelState || (ResponseModelState = {}));
var ChatSendResult;
(function(ChatSendResult) {
    function isSent(result) {
        return result.kind === "sent";
    }
    ChatSendResult.isSent = isSent;
    function isRejected(result) {
        return result.kind === "rejected";
    }
    ChatSendResult.isRejected = isRejected;
    function isQueued(result) {
        return result.kind === "queued";
    }
    ChatSendResult.isQueued = isQueued;
    function assertSent(result) {
        if (result.kind !== "sent") {
            throw ( new Error(`Expected ChatSendResult to be 'sent', but was '${result.kind}'`));
        }
    }
    ChatSendResult.assertSent = assertSent;
})(ChatSendResult || (ChatSendResult = {}));
var ChatRequestQueueKind;
(function(ChatRequestQueueKind) {
    ChatRequestQueueKind["Queued"] = "queued";
    ChatRequestQueueKind["Steering"] = "steering";
})(ChatRequestQueueKind || (ChatRequestQueueKind = {}));
const ChatStopCancellationNoopEventName = "chat.stopCancellationNoop";
const ChatPendingRequestChangeEventName = "chat.pendingRequestChange";

export { ChatAgentVoteDirection, ChatAgentVoteDownReason, ChatCopyKind, ChatErrorLevel, ChatMcpServersStarting, ChatPendingRequestChangeEventName, ChatRequestQueueKind, ChatResponseClearToPreviousToolInvocationReason, ChatResponseReferencePartStatusKind, ChatSendResult, ChatStopCancellationNoopEventName, ElicitationState, IChatToolInvocation, ResponseModelState, ToolConfirmKind, convertLegacyChatSessionTiming, isChatContentVariableReference, isChatFollowup, isIDocumentContext, isIUsedContext, isLegacyChatTerminalToolInvocationData };
