"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const vscode = require("vscode");
var uuid = require('node-uuid');
var md5 = require('md5');
const fs = require("fs");
const command_1 = require("../constants/command");
const utils_1 = require("../utils");
class default_1 {
    constructor({ parser, filepath, start, end, startRow, startCol, endRow, endCol }) {
        this.isRight = true;
        this.parser = parser;
        this.filepath = filepath;
        this.start = start;
        this.end = end;
        this.startRow = startRow;
        this.startCol = startCol;
        this.endRow = endRow;
        this.endCol = endCol;
        this.id = md5(this.filepath + this.start + this.end);
    }
    registerCommand(commandId, callback) {
        const commandSerivce = this.parser.getService('hoverMenuCommand');
        commandSerivce.registerCommand(commandId + this.id, callback, true);
    }
    removeCommand(commandId, callback) {
        const commandSerivce = this.parser.getService('hoverMenuCommand');
        commandSerivce.removeCommand(commandId, true);
    }
    sendErrorNodoInfoToWebwiew(params) {
        // 这里执行，对key，以及lang等参数的筛选
        this.parser.webViewHooks.sendLangInfoHook.promise(params, this).then((data) => {
            this.parser.webview.triggerWebviewListener('onErrorNode', data);
        });
    }
    formatErrorLog() {
    }
    _replace(start, end, text) {
        const fileContent = fs.readFileSync(this.filepath).toString();
        // 如果当前文件，是当前窗口，那么就用另一种api
        const currentDocumentFilepath = utils_1.default.getCurrentFilePath();
        if (currentDocumentFilepath === this.filepath) {
            const activeTextEditor = utils_1.default.getActiveEditor();
            activeTextEditor.edit((editBuilder) => {
                editBuilder.replace(new vscode.Range(this.startRow - 1, this.startCol, this.endRow - 1, this.endCol), text);
            });
        }
        else {
            fs.writeFileSync(this.filepath, fileContent.substr(0, start) + text + fileContent.substr(end));
        }
    }
    getCommandUrl(commandKey, params) {
        return `command:${commandKey}?${encodeURIComponent(JSON.stringify(params))}`;
    }
    createHoverCommandMenu(menus) {
        const commands = menus.map((item) => {
            item.params.command = item.params.command + item.params.errorNodeId;
            return `- [${item.name}](${this.getCommandUrl(command_1.Commands.HoverMenu, item.params)})`;
        });
        const markdown = new vscode.MarkdownString(commands.join('\n'));
        markdown.isTrusted = true;
        return new vscode.Hover(markdown);
    }
    replaceAndSave(errorInfo, text) {
        return __awaiter(this, void 0, void 0, function* () {
            var addParams = [];
            errorInfo.langs.forEach(item => {
                if (item.value) {
                    addParams.push({
                        key: errorInfo.key,
                        text: item.value,
                        lang: item.langKey
                    });
                }
            });
            yield this.parser.intlStorage.storeKeyAndValues(addParams);
            this._replace(this.start, this.end, text ? text : `intl.get('${errorInfo.key}').d('${errorInfo.langs[0].value}')`);
        });
    }
    putColor() {
        const activeEditor = this.parser.utils.getActiveEditor();
        const { document } = activeEditor;
        if (document) {
            this.parser.addDecoration('red', {
                range: new vscode.Range(document.positionAt(this.start), document.positionAt(this.end))
            });
        }
    }
    logError(text) {
        this.parser.addDiagnostic(text || '出现错误', {
            startCol: this.startCol,
            startRow: this.startRow,
            endCol: this.endCol,
            endRow: this.endRow
        });
    }
    showMenu(params) {
        return;
    }
}
exports.default = default_1;
//# sourceMappingURL=BaseErrorNode.js.map