"use strict";
/* ---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtHostCommentThread = exports.ExtHostComments = void 0;
exports.createCommentsApiFactory = createCommentsApiFactory;
const tslib_1 = require("tslib");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const common_1 = require("../../../common");
const vscode_1 = require("../../../common/vscode");
const extHostTypeConverter = tslib_1.__importStar(require("../../../common/vscode/converter"));
const types = tslib_1.__importStar(require("../../../common/vscode/ext-types"));
const models = tslib_1.__importStar(require("../../../common/vscode/models"));
function createCommentsApiFactory(extension, extHostComments) {
    const comment = {
        createCommentController(id, label) {
            return extHostComments.createCommentController(extension, id, label);
        },
    };
    return comment;
}
class ExtHostComments {
    constructor(rpcProtocol, commands, _documents) {
        this.rpcProtocol = rpcProtocol;
        this._documents = _documents;
        this._commentControllers = new Map();
        this._commentControllersByExtension = new Map();
        this._proxy = this.rpcProtocol.getProxy(vscode_1.MainThreadAPIIdentifier.MainThreadComments);
        this.registerArgumentProcessor(commands);
    }
    registerArgumentProcessor(commands) {
        commands.registerArgumentProcessor({
            processArgument: (arg) => {
                if (arg && arg.$mid === 6) {
                    const commentController = this._commentControllers.get(arg.handle);
                    if (!commentController) {
                        return arg;
                    }
                    return commentController;
                }
                else if (arg && arg.$mid === 7) {
                    const commentController = this._commentControllers.get(arg.commentControlHandle);
                    if (!commentController) {
                        return arg;
                    }
                    const commentThread = commentController.getCommentThread(arg.commentThreadHandle);
                    if (!commentThread) {
                        return arg;
                    }
                    return commentThread;
                }
                else if (arg && arg.$mid === 8) {
                    const commentController = this._commentControllers.get(arg.thread.commentControlHandle);
                    if (!commentController) {
                        return arg;
                    }
                    const commentThread = commentController.getCommentThread(arg.thread.commentThreadHandle);
                    if (!commentThread) {
                        return arg;
                    }
                    return {
                        thread: commentThread,
                        text: arg.text,
                    };
                }
                else if (arg && arg.$mid === 9) {
                    const commentController = this._commentControllers.get(arg.thread.commentControlHandle);
                    if (!commentController) {
                        return arg;
                    }
                    const commentThread = commentController.getCommentThread(arg.thread.commentThreadHandle);
                    if (!commentThread) {
                        return arg;
                    }
                    const commentUniqueId = arg.commentUniqueId;
                    const comment = commentThread.getCommentByUniqueId(commentUniqueId);
                    if (!comment) {
                        return arg;
                    }
                    return comment;
                }
                else if (arg && arg.$mid === 10) {
                    const commentController = this._commentControllers.get(arg.thread.commentControlHandle);
                    if (!commentController) {
                        return arg;
                    }
                    const commentThread = commentController.getCommentThread(arg.thread.commentThreadHandle);
                    if (!commentThread) {
                        return arg;
                    }
                    const body = arg.text;
                    const commentUniqueId = arg.commentUniqueId;
                    const comment = commentThread.getCommentByUniqueId(commentUniqueId);
                    if (!comment) {
                        return arg;
                    }
                    comment.body = body;
                    return comment;
                }
                return arg;
            },
        });
    }
    createCommentController(extension, id, label) {
        const handle = ExtHostComments.handlePool++;
        const commentController = new ExtHostCommentController(extension, handle, this._proxy, id, label);
        this._commentControllers.set(commentController.handle, commentController);
        const commentControllers = this._commentControllersByExtension.get((0, common_1.getExtensionId)(extension.id)) || [];
        commentControllers.push(commentController);
        this._commentControllersByExtension.set((0, common_1.getExtensionId)(extension.id), commentControllers);
        return commentController;
    }
    $createCommentThreadTemplate(commentControllerHandle, uriComponents, range) {
        const commentController = this._commentControllers.get(commentControllerHandle);
        if (!commentController) {
            return;
        }
        commentController.$createCommentThreadTemplate(uriComponents, range);
    }
    async $updateCommentThreadTemplate(commentControllerHandle, threadHandle, range) {
        const commentController = this._commentControllers.get(commentControllerHandle);
        if (!commentController) {
            return;
        }
        commentController.$updateCommentThreadTemplate(threadHandle, range);
    }
    $deleteCommentThread(commentControllerHandle, commentThreadHandle) {
        const commentController = this._commentControllers.get(commentControllerHandle);
        if (commentController) {
            commentController.$deleteCommentThread(commentThreadHandle);
        }
    }
    $provideCommentingRanges(commentControllerHandle, uriComponents, token) {
        const commentController = this._commentControllers.get(commentControllerHandle);
        if (!commentController || !commentController.commentingRangeProvider) {
            return Promise.resolve(undefined);
        }
        const document = this._documents.getDocument(ide_core_common_1.Uri.revive(uriComponents));
        return (0, ide_core_common_1.asPromise)(() => commentController.commentingRangeProvider.provideCommentingRanges(document, token)).then((ranges) => (ranges ? ranges.map((x) => extHostTypeConverter.fromRange(x)) : undefined));
    }
    $toggleReaction(commentControllerHandle, threadHandle, uri, comment, reaction) {
        const commentController = this._commentControllers.get(commentControllerHandle);
        if (!commentController || !commentController.reactionHandler) {
            return Promise.resolve(undefined);
        }
        return (0, ide_core_common_1.asPromise)(() => {
            const commentThread = commentController.getCommentThread(threadHandle);
            if (commentThread) {
                const vscodeComment = commentThread.getCommentByUniqueId(comment.uniqueIdInThread);
                if (commentController !== undefined && vscodeComment) {
                    if (commentController.reactionHandler) {
                        return commentController.reactionHandler(vscodeComment, convertFromReaction(reaction));
                    }
                }
            }
            return Promise.resolve(undefined);
        });
    }
}
exports.ExtHostComments = ExtHostComments;
ExtHostComments.handlePool = 0;
class ExtHostCommentController {
    get id() {
        return this._id;
    }
    get label() {
        return this._label;
    }
    get handle() {
        return this._handle;
    }
    get options() {
        return this._options;
    }
    set options(options) {
        this._options = options;
        this._proxy.$updateCommentControllerFeatures(this.handle, { options: this._options });
    }
    get reactionHandler() {
        return this._reactionHandler;
    }
    set reactionHandler(handler) {
        this._reactionHandler = handler;
        this._proxy.$updateCommentControllerFeatures(this.handle, { reactionHandler: !!handler });
    }
    constructor(_extension, _handle, _proxy, _id, _label) {
        this._extension = _extension;
        this._handle = _handle;
        this._proxy = _proxy;
        this._id = _id;
        this._label = _label;
        this._threads = new Map();
        this._proxy.$registerCommentController(this.handle, _id, _label);
    }
    createCommentThread(arg0, arg1, arg2, arg3) {
        if (typeof arg0 === 'string') {
            const commentThread = new ExtHostCommentThread(this._proxy, this, arg0, arg1, arg2, arg3, this._extension);
            this._threads.set(commentThread.handle, commentThread);
            return commentThread;
        }
        else {
            const commentThread = new ExtHostCommentThread(this._proxy, this, undefined, arg0, arg1, arg2, this._extension);
            this._threads.set(commentThread.handle, commentThread);
            return commentThread;
        }
    }
    $createCommentThreadTemplate(uriComponents, range) {
        const commentThread = new ExtHostCommentThread(this._proxy, this, undefined, ide_core_common_1.Uri.revive(uriComponents), extHostTypeConverter.toRange(range), [], this._extension);
        commentThread.collapsibleState = models.CommentThreadCollapsibleState.Expanded;
        this._threads.set(commentThread.handle, commentThread);
        return commentThread;
    }
    $updateCommentThreadTemplate(threadHandle, range) {
        const thread = this._threads.get(threadHandle);
        if (thread) {
            thread.range = extHostTypeConverter.toRange(range);
        }
    }
    $deleteCommentThread(threadHandle) {
        const thread = this._threads.get(threadHandle);
        if (thread) {
            thread.dispose();
        }
        this._threads.delete(threadHandle);
    }
    getCommentThread(handle) {
        return this._threads.get(handle);
    }
    dispose() {
        this._threads.forEach((value) => {
            value.dispose();
        });
        this._proxy.$unregisterCommentController(this.handle);
    }
}
class ExtHostCommentThread {
    set threadId(id) {
        this._id = id;
    }
    get threadId() {
        return this._id;
    }
    get id() {
        return this._id;
    }
    get resource() {
        return this._uri;
    }
    get uri() {
        return this._uri;
    }
    set range(range) {
        if (!range.isEqual(this._range)) {
            this._range = range;
            this.modifications.range = range;
            this._onDidUpdateCommentThread.fire();
        }
    }
    get range() {
        return this._range;
    }
    set canReply(state) {
        if (this._canReply !== state) {
            this._canReply = state;
            this.modifications.canReply = state;
            this._onDidUpdateCommentThread.fire();
        }
    }
    get canReply() {
        return this._canReply;
    }
    get label() {
        return this._label;
    }
    set label(label) {
        this._label = label;
        this.modifications.label = label;
        this._onDidUpdateCommentThread.fire();
    }
    get state() {
        return this._state;
    }
    set state(newState) {
        if (this._state !== newState) {
            this._state = newState;
            this._onDidUpdateCommentThread.fire();
        }
    }
    get contextValue() {
        return this._contextValue;
    }
    set contextValue(context) {
        this._contextValue = context;
        this.modifications.contextValue = context;
        this._onDidUpdateCommentThread.fire();
    }
    get comments() {
        return this._comments;
    }
    set comments(newComments) {
        this._comments = newComments;
        this.modifications.comments = newComments;
        this._onDidUpdateCommentThread.fire();
    }
    get collapsibleState() {
        return this._collapseState;
    }
    set collapsibleState(newState) {
        this._collapseState = newState;
        this.modifications.collapsibleState = newState;
        this._onDidUpdateCommentThread.fire();
    }
    get isDisposed() {
        return this._isDiposed;
    }
    constructor(_proxy, _commentController, _id, _uri, _range, _comments, extension) {
        this._proxy = _proxy;
        this._commentController = _commentController;
        this._id = _id;
        this._uri = _uri;
        this._range = _range;
        this._comments = _comments;
        this.handle = ExtHostCommentThread._handlePool++;
        this.commentHandle = 0;
        this.modifications = Object.create(null);
        this._onDidUpdateCommentThread = new ide_core_common_1.Emitter();
        this.onDidUpdateCommentThread = this._onDidUpdateCommentThread.event;
        this._canReply = true;
        this._commentsMap = new Map();
        this._acceptInputDisposables = new ide_core_common_1.MutableDisposable();
        this._acceptInputDisposables.value = new ide_core_common_1.DisposableStore();
        if (this._id === undefined) {
            this._id = `${_commentController.id}.${this.handle}`;
        }
        this._proxy.$createCommentThread(this._commentController.handle, this.handle, this._id, this._uri, extHostTypeConverter.fromRange(this._range), extension.id);
        this._localDisposables = [];
        this._isDiposed = false;
        this._localDisposables.push(types.Disposable.from(this.onDidUpdateCommentThread(() => {
            this.eventuallyUpdateCommentThread();
        })));
        // set up comments after ctor to batch update events.
        this.comments = _comments;
    }
    eventuallyUpdateCommentThread() {
        if (this._isDiposed) {
            return;
        }
        if (!this._acceptInputDisposables.value) {
            this._acceptInputDisposables.value = new ide_core_common_1.DisposableStore();
        }
        const modified = (value) => Object.prototype.hasOwnProperty.call(this.modifications, value);
        const formattedModifications = {};
        if (modified('range')) {
            formattedModifications.range = extHostTypeConverter.fromRange(this._range);
        }
        if (modified('label')) {
            formattedModifications.label = this.label;
        }
        if (modified('contextValue')) {
            formattedModifications.contextValue = this.contextValue;
        }
        if (modified('comments')) {
            formattedModifications.comments = this._comments.map((cmt) => convertToModeComment(this, this._commentController, cmt, this._commentsMap));
        }
        if (modified('collapsibleState')) {
            formattedModifications.collapseState = convertToCollapsibleState(this._collapseState);
        }
        if (modified('canReply')) {
            formattedModifications.canReply = this.canReply;
        }
        this.modifications = {};
        this._proxy.$updateCommentThread(this._commentController.handle, this.handle, this._id, this._uri, formattedModifications);
    }
    getCommentByUniqueId(uniqueId) {
        for (const key of this._commentsMap) {
            const comment = key[0];
            const id = key[1];
            if (uniqueId === id) {
                return comment;
            }
        }
        return;
    }
    dispose() {
        this._isDiposed = true;
        this._acceptInputDisposables.dispose();
        this._localDisposables.forEach((disposable) => disposable.dispose());
        this._proxy.$deleteCommentThread(this._commentController.handle, this.handle);
    }
}
exports.ExtHostCommentThread = ExtHostCommentThread;
ExtHostCommentThread._handlePool = 0;
tslib_1.__decorate([
    (0, ide_core_common_1.debounce)(100),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", []),
    tslib_1.__metadata("design:returntype", void 0)
], ExtHostCommentThread.prototype, "eventuallyUpdateCommentThread", null);
function convertToModeComment(thread, commentController, vscodeComment, commentsMap) {
    var _a;
    let commentUniqueId = commentsMap.get(vscodeComment);
    if (!commentUniqueId) {
        commentUniqueId = ++thread.commentHandle;
        commentsMap.set(vscodeComment, commentUniqueId);
    }
    const iconPath = vscodeComment.author && vscodeComment.author.iconPath ? vscodeComment.author.iconPath.toJSON() : undefined;
    return {
        mode: vscodeComment.mode,
        contextValue: vscodeComment.contextValue,
        uniqueIdInThread: commentUniqueId,
        body: typeof vscodeComment.body === 'string'
            ? vscodeComment.body
            : extHostTypeConverter.MarkdownString.from(vscodeComment.body),
        userName: vscodeComment.author.name,
        userIconPath: iconPath,
        label: vscodeComment.label,
        commentReactions: vscodeComment.reactions
            ? vscodeComment.reactions.map((reaction) => convertToReaction(reaction))
            : undefined,
        timestamp: (_a = vscodeComment.timestamp) === null || _a === void 0 ? void 0 : _a.toJSON(),
    };
}
function convertToReaction(reaction) {
    return {
        label: reaction.label,
        iconPath: reaction.iconPath ? extHostTypeConverter.pathOrURIToURI(reaction.iconPath) : undefined,
        count: reaction.count,
        hasReacted: reaction.authorHasReacted,
    };
}
function convertFromReaction(reaction) {
    return {
        label: reaction.label || '',
        count: reaction.count || 0,
        iconPath: reaction.iconPath ? ide_core_common_1.Uri.revive(reaction.iconPath) : '',
        authorHasReacted: reaction.hasReacted || false,
    };
}
function convertToCollapsibleState(kind) {
    if (kind !== undefined) {
        switch (kind) {
            case types.CommentThreadCollapsibleState.Expanded:
                return models.CommentThreadCollapsibleState.Expanded;
            case types.CommentThreadCollapsibleState.Collapsed:
                return models.CommentThreadCollapsibleState.Collapsed;
        }
    }
    return models.CommentThreadCollapsibleState.Collapsed;
}
//# sourceMappingURL=ext.host.comments.js.map