"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 BaseErrorNode_1 = require("../../../model/BaseErrorNode");
const path = require("path");
var CRC32 = require('crc-32');
const command_1 = require("../command");
class NoKeyErrorNode extends BaseErrorNode_1.default {
    constructor(params, extraParams) {
        super(params);
        this.type = 'HardCode';
        this.isRight = false;
        this.extraParams = extraParams;
        const { parser } = params;
        this.registerCommand(command_1.default.open_webview, ({ errorNodeId }) => __awaiter(this, void 0, void 0, function* () {
            if (this.id === errorNodeId) {
                yield parser.webview.open();
                yield this.sendErrorNodoInfoToWebwiew({
                    filePath: this.filepath,
                    id: this.id,
                    langs: this.parser.config.langs.map((langItem, key) => {
                        return {
                            langKey: langItem.key,
                            value: key === 0 ? this.extraParams.text : ''
                        };
                    }),
                    key: path.relative(parser.config.projectDir, path.dirname(this.filepath) + '/' + path.basename(this.filepath).substr(0, path.basename(this.filepath).lastIndexOf('.'))).split('/').join('_') + CRC32.str(this.extraParams.text)
                });
            }
        }));
        this.registerCommand(command_1.default.HardCode_Haskey, ({ errorNodeId, key }) => __awaiter(this, void 0, void 0, function* () {
            if (this.id === errorNodeId) {
                this._replace(this.start, this.end, `intl.get('${key}').d('${this.extraParams.text}')`);
            }
        }));
        this.registerCommand(command_1.default.HardCode_Haskey_With_Brackets, ({ errorNodeId, key }) => __awaiter(this, void 0, void 0, function* () {
            if (this.id === errorNodeId) {
                this._replace(this.start, this.end, `{intl.get('${key}').d('${this.extraParams.text}')}`);
            }
        }));
    }
    replaceAndSave(errorInfo, text) {
        const _super = Object.create(null, {
            replaceAndSave: { get: () => super.replaceAndSave }
        });
        return __awaiter(this, void 0, void 0, function* () {
            yield _super.replaceAndSave.call(this, errorInfo, text ? text : `intl.get('${errorInfo.key}').d('${errorInfo.langs[0].value}')`);
        });
    }
    replaceAndSaveWithBrackets(errorInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.replaceAndSave(errorInfo, `{intl.get('${errorInfo.key}').d('${errorInfo.langs[0].value}')}`);
        });
    }
    showMenu({ position, document, offset }) {
        if (offset > this.start && offset < this.end) {
            const keyMap = this.parser.intlStorage.getValueKey(this.extraParams.text);
            const key = keyMap[this.parser.config.getFirstLangKey()];
            if (key) {
                // 如果有key
                return this.createHoverCommandMenu([
                    {
                        name: '已经存在key，点击替换',
                        params: {
                            filePath: this.filepath,
                            command: command_1.default.HardCode_Haskey,
                            errorNodeId: this.id,
                            key
                        }
                    },
                    {
                        name: '已经存在key，点击替换, 加大括号',
                        params: {
                            filePath: this.filepath,
                            command: command_1.default.HardCode_Haskey_With_Brackets,
                            errorNodeId: this.id,
                            key
                        }
                    }
                ]);
            }
            else {
                // 如果没有key
                return this.createHoverCommandMenu([
                    {
                        name: '硬编码',
                        params: {
                            filePath: this.filepath,
                            command: command_1.default.open_webview,
                            errorNodeId: this.id
                        }
                    }
                ]);
            }
        }
        return;
    }
    logError() {
        super.logError('硬编码文件');
    }
}
exports.default = NoKeyErrorNode;
//# sourceMappingURL=HardCode.js.map