import { Iterable } from '../../../base/common/iterator.js';
import { toDisposable } from '../../../base/common/lifecycle.js';
import { MainContext } from './extHost.protocol.js';
class ChatProviderWrapper {
    static { this._pool = 0; }
    constructor(extension, provider) {
        this.extension = extension;
        this.provider = provider;
        this.handle = ChatProviderWrapper._pool++;
    }
}
class ExtHostChat {
    static { this._nextId = 0; }
    constructor(mainContext) {
        this._chatProvider = ( new Map());
        this._chatSessions = ( new Map());
        this._proxy = ( mainContext.getProxy(MainContext.MainThreadChat));
    }
    registerChatProvider(extension, id, provider) {
        const wrapper = ( new ChatProviderWrapper(extension, provider));
        this._chatProvider.set(wrapper.handle, wrapper);
        this._proxy.$registerChatProvider(wrapper.handle, id);
        return toDisposable(() => {
            this._proxy.$unregisterChatProvider(wrapper.handle);
            this._chatProvider.delete(wrapper.handle);
        });
    }
    transferChatSession(session, newWorkspace) {
        const sessionId = Iterable.find(( this._chatSessions.keys()), key => this._chatSessions.get(key) === session) ?? 0;
        if (typeof sessionId !== 'number') {
            return;
        }
        this._proxy.$transferChatSession(sessionId, newWorkspace);
    }
    async $prepareChat(handle, token) {
        const entry = this._chatProvider.get(handle);
        if (!entry) {
            return undefined;
        }
        const session = await entry.provider.prepareSession(token);
        if (!session) {
            return undefined;
        }
        const id = ExtHostChat._nextId++;
        this._chatSessions.set(id, session);
        return {
            id,
        };
    }
    $releaseSession(sessionId) {
        this._chatSessions.delete(sessionId);
    }
}
export { ExtHostChat };
