"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const find_java_home_1 = __importDefault(require("find-java-home"));
const ExecuteCommand_1 = __importDefault(require("@flxbl-io/sfdx-process-wrapper/lib/commandExecutor/ExecuteCommand"));
const sfp_logger_1 = require("@flxbl-io/sfp-logger");
const sfp_logger_2 = __importDefault(require("@flxbl-io/sfp-logger"));
const sfp_logger_3 = require("@flxbl-io/sfp-logger");
const fs = __importStar(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const jarFile = path_1.default.join(__dirname, '..', 'jars', '*');
class ApexDepedencyCheckImpl {
    constructor(logger, projectDirectory) {
        this.logger = logger;
        this.projectDirectory = projectDirectory;
    }
    async execute() {
        let apexLinkProcessExecutor = new ExecuteCommand_1.default(this.logger, sfp_logger_1.LoggerLevel.INFO, false);
        let generatedCommand = await this.getGeneratedCommandWithParams();
        await apexLinkProcessExecutor.execCommand(generatedCommand, process.cwd());
        let result = fs.readJSONSync(`${this.projectDirectory}/apexlink.json`);
        return result;
    }
    async getGeneratedCommandWithParams() {
        let javaHome = await this.getJavaHome();
        //Replace Program Files with Progra~1 in Windows
        javaHome = javaHome.replace(/Program Files/, "Progra~1");
        let command = `${path_1.default.join(javaHome, 'bin', 'java')}  -cp  "${jarFile}" "io.github.apexdevtools.apexls.DependencyReport"  -f json -w ${this.projectDirectory} > ${this.projectDirectory}/apexlink.json`;
        return command;
    }
    /**
     *Finds java home
     *
     * @return the Java home path
     */
    async getJavaHome() {
        return new Promise((resolve, reject) => {
            (0, find_java_home_1.default)({ allowJre: true }, (err, res) => {
                if (err) {
                    return reject(err);
                }
                sfp_logger_2.default.log(`Java HOME ${res}`, sfp_logger_1.LoggerLevel.TRACE, new sfp_logger_3.ConsoleLogger());
                resolve(res);
            });
        });
    }
}
exports.default = ApexDepedencyCheckImpl;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQXBleERlcGVkZW5jeUNoZWNrSW1wbC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9BcGV4RGVwZWRlbmN5Q2hlY2tJbXBsLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQSxvRUFBMEM7QUFDMUMsdUhBQStGO0FBQy9GLHFEQUEyRDtBQUMzRCxzRUFBNkM7QUFDN0MscURBQXFEO0FBQ3JELDZDQUErQjtBQUMvQixnREFBd0I7QUFFeEIsTUFBTSxPQUFPLEdBQUcsY0FBSSxDQUFDLElBQUksQ0FBQyxTQUFTLEVBQUcsSUFBSSxFQUFFLE1BQU0sRUFBRSxHQUFHLENBQUMsQ0FBQztBQUN6RCxNQUFxQixzQkFBc0I7SUFDdkMsWUFBMkIsTUFBYyxFQUFVLGdCQUF3QjtRQUFoRCxXQUFNLEdBQU4sTUFBTSxDQUFRO1FBQVUscUJBQWdCLEdBQWhCLGdCQUFnQixDQUFRO0lBQUcsQ0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