
import { encodeHex, VSBuffer, decodeHex } from '../../../../../base/common/buffer.js';
import '../../../../../base/common/errors.js';
import '../../../../../base/common/observableInternal/index.js';
import { hasKey } from '../../../../../base/common/types.js';
import { URI } from '../../../../../base/common/uri.js';
import { localize } from '../../../../../nls.js';
import { RawContextKey } from '../../../../../platform/contextkey/common/contextkey.js';
import { autorunSelfDisposable } from '../../../../../base/common/observableInternal/reactions/autorun.js';

function chatEditingSessionIsReady(session) {
    return ( new Promise(resolve => {
        autorunSelfDisposable(reader => {
            const state = session.state.read(reader);
            if (state !== ChatEditingSessionState.Initial) {
                reader.dispose();
                resolve();
            }
        });
    }));
}
function emptySessionEntryDiff(originalURI, modifiedURI) {
    return {
        originalURI,
        modifiedURI,
        added: 0,
        removed: 0,
        quitEarly: false,
        identical: false,
        isFinal: false,
        isBusy: false
    };
}
var ModifiedFileEntryState;
(function(ModifiedFileEntryState) {
    ModifiedFileEntryState[ModifiedFileEntryState["Modified"] = 0] = "Modified";
    ModifiedFileEntryState[ModifiedFileEntryState["Accepted"] = 1] = "Accepted";
    ModifiedFileEntryState[ModifiedFileEntryState["Rejected"] = 2] = "Rejected";
})(ModifiedFileEntryState || (ModifiedFileEntryState = {}));
var ChatEditingSessionState;
(function(ChatEditingSessionState) {
    ChatEditingSessionState[ChatEditingSessionState["Initial"] = 0] = "Initial";
    ChatEditingSessionState[ChatEditingSessionState["StreamingEdits"] = 1] = "StreamingEdits";
    ChatEditingSessionState[ChatEditingSessionState["Idle"] = 2] = "Idle";
    ChatEditingSessionState[ChatEditingSessionState["Disposed"] = 3] = "Disposed";
})(ChatEditingSessionState || (ChatEditingSessionState = {}));
const CHAT_EDITING_MULTI_DIFF_SOURCE_RESOLVER_SCHEME = "chat-editing-multi-diff-source";
const chatEditingWidgetFileStateContextKey = ( new RawContextKey("chatEditingWidgetFileState", undefined, ( localize(7209, "The current state of the file in the chat editing widget"))));
const chatEditingAgentSupportsReadonlyReferencesContextKey = ( new RawContextKey("chatEditingAgentSupportsReadonlyReferences", undefined, ( localize(
    7210,
    "Whether the chat editing agent supports readonly references (temporary)"
))));
const decidedChatEditingResourceContextKey = ( new RawContextKey("decidedChatEditingResource", []));
const chatEditingResourceContextKey = ( new RawContextKey("chatEditingResource", undefined));
const inChatEditingSessionContextKey = ( new RawContextKey("inChatEditingSession", undefined));
const hasUndecidedChatEditingResourceContextKey = ( new RawContextKey("hasUndecidedChatEditingResource", false));
const hasAppliedChatEditsContextKey = ( new RawContextKey("hasAppliedChatEdits", false));
const applyingChatEditsFailedContextKey = ( new RawContextKey("applyingChatEditsFailed", false));
var ChatEditKind;
(function(ChatEditKind) {
    ChatEditKind[ChatEditKind["Created"] = 0] = "Created";
    ChatEditKind[ChatEditKind["Modified"] = 1] = "Modified";
    ChatEditKind[ChatEditKind["Deleted"] = 2] = "Deleted";
})(ChatEditKind || (ChatEditKind = {}));
function isChatEditingActionContext(thing) {
    return typeof thing === "object" && !!thing && hasKey(thing, {
        sessionResource: true
    });
}
function getMultiDiffSourceUri(session, showPreviousChanges) {
    return ( URI.from({
        scheme: CHAT_EDITING_MULTI_DIFF_SOURCE_RESOLVER_SCHEME,
        authority: encodeHex(VSBuffer.fromString(( session.chatSessionResource.toString()))),
        query: showPreviousChanges ? "previous" : undefined
    }));
}
function parseChatMultiDiffUri(uri) {
    const chatSessionResource = ( URI.parse(( decodeHex(uri.authority).toString())));
    const showPreviousChanges = uri.query === "previous";
    return {
        chatSessionResource,
        showPreviousChanges
    };
}

export { CHAT_EDITING_MULTI_DIFF_SOURCE_RESOLVER_SCHEME, ChatEditKind, ChatEditingSessionState, ModifiedFileEntryState, applyingChatEditsFailedContextKey, chatEditingAgentSupportsReadonlyReferencesContextKey, chatEditingResourceContextKey, chatEditingSessionIsReady, chatEditingWidgetFileStateContextKey, decidedChatEditingResourceContextKey, emptySessionEntryDiff, getMultiDiffSourceUri, hasAppliedChatEditsContextKey, hasUndecidedChatEditingResourceContextKey, inChatEditingSessionContextKey, isChatEditingActionContext, parseChatMultiDiffUri };
