"use strict";
/*
1. Create CxScanConfig object, CxScan object, ExecutionService call, CxParamType enum, CxAuthType enum, CxException exception class, CxAuth class
2. Create global variables for this class(CxAuth)
3. Based on the passed CxScanConfig object, grab the authentication credentials and save them in global variables
4. Check if path to the executable is provided. if not, use the packaged module
5. Create a function to return mandatory auth parameters that needs to be passed with every call.
6. Create scanCreate, scanShow, scanList functions
7. Check if the returned object is of CxScan object or not and return it for each function call.
8. Add the executables for other environments.
*/
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 CxParamType_1 = require("./CxParamType");
const ExecutionService_1 = require("./ExecutionService");
class CxAuthCall {
    constructor(cxScanConfig) {
        this.baseUri = "";
        this.clientId = "";
        this.clientSecret = "";
        this.apiKey = "";
        this.commands = [];
        if (cxScanConfig.clientId !== null && cxScanConfig.clientSecret !== null) {
            console.log("Received clientId and clientSecret");
            this.clientId = cxScanConfig.clientId;
            this.clientSecret = cxScanConfig.clientSecret;
        }
        else if (cxScanConfig.apiKey != null) {
            this.apiKey = cxScanConfig.apiKey;
        }
        else {
            console.log("Did not receive ClientId/Secret or ApiKey");
        }
        if (cxScanConfig.pathToExecutable !== null && cxScanConfig.pathToExecutable !== "") {
            this.pathToExecutable = cxScanConfig.pathToExecutable;
        }
        else if (process.platform === 'win32') {
            this.pathToExecutable = 'src/main/resources/cx.exe';
        }
        else if (process.platform === 'darwin') {
            this.pathToExecutable = 'src/main/resources/cx-mac';
        }
        else {
            this.pathToExecutable = 'src/main/resources/cx-linux';
        }
        if (cxScanConfig.baseUri !== null) {
            this.baseUri = cxScanConfig.baseUri;
        }
    }
    initializeCommands() {
        let list = [];
        if (this.clientId !== null && this.clientId !== "") {
            list.push("--client-id");
            list.push(this.clientId);
        }
        if (this.clientSecret !== null && this.clientSecret !== "") {
            list.push("--client-secret");
            list.push(this.clientSecret);
        }
        if (this.apiKey !== null && this.apiKey !== "") {
            list.push("--apikey");
            list.push(this.apiKey);
        }
        if (this.baseUri !== null && this.baseUri !== "") {
            list.push("--base-uri");
            list.push(this.baseUri);
        }
        list.push("--format");
        list.push("json");
        list.push("-v");
        return list;
    }
    scanCreate(params) {
        return __awaiter(this, void 0, void 0, function* () {
            this.commands = this.initializeCommands();
            this.commands.push("scan");
            this.commands.push("create");
            params.forEach((value, key) => {
                if (key !== CxParamType_1.CxParamType.ADDITIONAL_PARAMETERS && key.length !== 1) {
                    this.commands.push("--" + key.toString().replace(/_/g, "-").toLowerCase());
                    this.commands.push(value);
                }
                else if (key.length === 1) {
                    this.commands.push("-" + key.toString().replace(/_/g, "-").toLowerCase());
                    this.commands.push(value);
                }
                else {
                    let paramList = value.match(/(?:[^\s"]+|"[^"]*")+/g);
                    console.log("Additional parameters refined: " + paramList);
                    if (paramList !== null) {
                        paramList.forEach((element) => {
                            this.commands.push(element);
                        });
                    }
                }
            });
            let exec = new ExecutionService_1.ExecutionService();
            return yield exec.executeCommands(this.pathToExecutable, this.commands);
        });
    }
    scanShow(id) {
        return __awaiter(this, void 0, void 0, function* () {
            this.commands = this.initializeCommands();
            this.commands.push("scan");
            this.commands.push("show");
            this.commands.push(id);
            let exec = new ExecutionService_1.ExecutionService();
            return yield exec.executeCommands(this.pathToExecutable, this.commands);
        });
    }
    scanList() {
        return __awaiter(this, void 0, void 0, function* () {
            this.commands = this.initializeCommands();
            this.commands.push("scan");
            this.commands.push("list");
            let exec = new ExecutionService_1.ExecutionService();
            return yield exec.executeCommands(this.pathToExecutable, this.commands);
        });
    }
}
exports.default = CxAuthCall;
//# sourceMappingURL=CxAuthCall.js.map