
import { MainContext } from './extHost.protocol.js';
import { TextEdit, ChatAgentResult, DocumentContextItem } from './extHostTypeConverters.js';
import { URI } from '../../../base/common/uri.js';

class ExtHostCodeMapper {
    static { this._providerHandlePool = 0; }
    constructor(mainContext) {
        this.providers = ( new Map());
        this._proxy = ( mainContext.getProxy(MainContext.MainThreadCodeMapper));
    }
    async $mapCode(handle, internalRequest, token) {
        const provider = this.providers.get(handle);
        if (!provider) {
            throw ( new Error(`Received request to map code for unknown provider handle ${handle}`));
        }
        const stream = {
            textEdit: (target, edits) => {
                edits = (Array.isArray(edits) ? edits : [edits]);
                this._proxy.$handleProgress(internalRequest.requestId, {
                    uri: target,
                    edits: ( edits.map(TextEdit.from))
                });
            }
        };
        const request = {
            codeBlocks: ( internalRequest.codeBlocks.map(block => {
                return {
                    code: block.code,
                    resource: URI.revive(block.resource),
                    markdownBeforeBlock: block.markdownBeforeBlock
                };
            })),
            conversation: ( internalRequest.conversation.map(item => {
                if (item.type === 'request') {
                    return {
                        type: 'request',
                        message: item.message
                    };
                }
                else {
                    return {
                        type: 'response',
                        message: item.message,
                        result: item.result ? ChatAgentResult.to(item.result) : undefined,
                        references: item.references?.map(DocumentContextItem.to)
                    };
                }
            }))
        };
        const result = await provider.provideMappedEdits(request, stream, token);
        return result ?? null;
    }
    registerMappedEditsProvider(extension, provider) {
        const handle = ExtHostCodeMapper._providerHandlePool++;
        this._proxy.$registerCodeMapperProvider(handle);
        this.providers.set(handle, provider);
        return {
            dispose: () => {
                return this._proxy.$unregisterCodeMapperProvider(handle);
            }
        };
    }
}

export { ExtHostCodeMapper };
