
import { __decorate, __param } from '../../../../../../external/tslib/tslib.es6.js';
import { URI } from '../../../base/common/uri.js';
import { MainContext } from './extHost.protocol.js';
import { DocumentSelector } from './extHostTypeConverters.js';
import { IExtHostRpcService } from './extHostRpcService.js';

let ExtHostChatContext = class ExtHostChatContext {
    constructor(extHostRpc) {
        this._handlePool = 0;
        this._providers = ( new Map());
        this._itemPool = 0;
        this._items = ( new Map());
        this._proxy = ( extHostRpc.getProxy(MainContext.MainThreadChatContext));
    }
    async $provideChatContext(handle, token) {
        this._items.delete(handle);
        const provider = this._getProvider(handle);
        if (!provider.provideChatContextExplicit) {
            throw ( new Error('provideChatContext not implemented'));
        }
        const result = (await provider.provideChatContextExplicit(token)) ?? [];
        const items = [];
        for (const item of result) {
            const itemHandle = this._itemPool++;
            if (!( this._items.has(handle))) {
                this._items.set(handle, ( new Map()));
            }
            this._items.get(handle).set(itemHandle, item);
            items.push({
                handle: itemHandle,
                icon: item.icon,
                label: item.label,
                modelDescription: item.modelDescription,
                value: item.value
            });
        }
        return items;
    }
    async $provideChatContextForResource(handle, options, token) {
        const provider = this._getProvider(handle);
        if (!provider.provideChatContextForResource) {
            throw ( new Error('provideChatContextForResource not implemented'));
        }
        let result = await provider.provideChatContextForResource({ resource: URI.revive(options.resource) }, token);
        if (result && (result.value === undefined)) {
            result = await provider.resolveChatContext(result, token);
        }
        const item = result ? {
            handle: this._itemPool++,
            icon: result.icon,
            label: result.label,
            modelDescription: result.modelDescription,
            value: result.value
        } : undefined;
        return item;
    }
    async $resolveChatContext(handle, context, token) {
        const provider = this._getProvider(handle);
        if (!provider.resolveChatContext) {
            throw ( new Error('resolveChatContext not implemented'));
        }
        const extItem = this._items.get(handle)?.get(context.handle);
        if (!extItem) {
            throw ( new Error('Chat context item not found'));
        }
        const extResult = await provider.resolveChatContext(extItem, token);
        const result = extResult ?? context;
        return {
            handle: context.handle,
            icon: result.icon,
            label: result.label,
            modelDescription: result.modelDescription,
            value: result.value
        };
    }
    registerChatContextProvider(selector, id, provider) {
        const handle = this._handlePool++;
        this._providers.set(handle, provider);
        this._proxy.$registerChatContextProvider(handle, `${id}`, DocumentSelector.from(selector), {}, { supportsResource: !!provider.provideChatContextForResource, supportsResolve: !!provider.resolveChatContext });
        return {
            dispose: () => {
                this._providers.delete(handle);
                this._proxy.$unregisterChatContextProvider(handle);
            }
        };
    }
    _getProvider(handle) {
        if (!( this._providers.has(handle))) {
            throw ( new Error('Chat context provider not found'));
        }
        return this._providers.get(handle);
    }
};
ExtHostChatContext = ( __decorate([
    ( __param(0, IExtHostRpcService))
], ExtHostChatContext));

export { ExtHostChatContext };
