"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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 });
exports.markImpactCommand = void 0;
const fs = __importStar(require("fs"));
const dotenv = __importStar(require("dotenv"));
const impactDetectionService_1 = require("./core/impactDetectionService");
const typescript_1 = require("./core/lsp/server/typescript");
const java_1 = require("./core/lsp/server/java");
const kotlin_1 = require("./core/lsp/server/kotlin");
const clang_1 = require("./core/lsp/server/clang");
const swift_1 = require("./core/lsp/server/swift");
const csharp_1 = require("./core/lsp/server/csharp");
const util_1 = require("./util/util");
const markImpactCommand = (options) => __awaiter(void 0, void 0, void 0, function* () {
    const conf = dotenv.config({ path: options.dotenvFile }).parsed;
    let diff;
    const workspace = options.workspace || process.cwd();
    if (options.diff) {
        diff = fs.readFileSync(options.diff, { encoding: 'utf-8' });
    }
    else {
        diff = yield (0, util_1.readStdin)();
    }
    const impactDetectionService = new impactDetectionService_1.ImpactDetectionService(workspace);
    if (conf.TYPESCRIPT_LSP_SERVER_BIN) {
        impactDetectionService.addLspService(new typescript_1.TypeScriptLspService(conf.TYPESCRIPT_LSP_SERVER_BIN, workspace));
    }
    if (conf.JAVA_LSP_SERVER_BIN) {
        impactDetectionService.addLspService(new java_1.JavaLspService(conf.JAVA_LSP_SERVER_BIN, workspace));
    }
    if (conf.KOTLIN_LSP_SERVER_BIN) {
        impactDetectionService.addLspService(new kotlin_1.KotlinLspService(conf.KOTLIN_LSP_SERVER_BIN, workspace));
    }
    if (conf.CLANG_LSP_SERVER_BIN) {
        impactDetectionService.addLspService(new clang_1.ClangLspService(conf.CLANG_LSP_SERVER_BIN, workspace));
    }
    if (conf.SWIFT_LSP_SERVER_BIN) {
        impactDetectionService.addLspService(new swift_1.SwiftLspService(conf.SWIFT_LSP_SERVER_BIN, workspace));
    }
    if (conf.CSHARP_LSP_SERVER_BIN) {
        impactDetectionService.addLspService(new csharp_1.CSharpLspService(conf.CSHARP_LSP_SERVER_BIN, workspace));
    }
    try {
        const impactedLinesList = yield impactDetectionService.findImpactLinesByDiff(diff);
        const impactedFiles = impactDetectionService.markImpactedLines(impactedLinesList);
        for (const impactedFile of impactedFiles) {
            fs.writeFileSync(impactedFile.filePath, impactedFile.content);
        }
    }
    finally {
        yield impactDetectionService.stopAllLspServices();
        process.exit(1);
    }
    process.exit(0);
});
exports.markImpactCommand = markImpactCommand;
