"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecretInput = void 0;
exports.parseSecretInput = parseSecretInput;
const validators_1 = require("./validators");
class SecretInput {
    constructor(id, outputEnvName) {
        this.id = id;
        this.outputEnvName = outputEnvName;
    }
}
exports.SecretInput = SecretInput;
class ParsingError extends Error {
    constructor(message) {
        super(message);
    }
}
function parseSecretInput(secrets) {
    const results = secrets.map((secret) => {
        try {
            if (secret.indexOf(">") === -1) {
                throw new ParsingError(`Expected format: <secretGuid> > <environmentVariableName>`);
            }
            let [id, envName] = secret.split(">", 2);
            id = id.trim();
            envName = envName.trim();
            if (!(0, validators_1.isValidGuid)(id)) {
                throw new ParsingError(`Id is not a valid GUID`);
            }
            if (!(0, validators_1.isValidEnvName)(envName)) {
                throw new ParsingError(`Environment variable name is not valid`);
            }
            return new SecretInput(id, envName);
        }
        catch (e) {
            const message = `Error occurred when attempting to parse ${secret}`;
            if (e instanceof ParsingError) {
                throw TypeError(`${message}. ${e.message}`);
            }
            throw TypeError(message);
        }
    });
    if (!(0, validators_1.isUniqueEnvNames)(results)) {
        throw TypeError("Environmental variable names provided are not unique, names must be unique");
    }
    return results;
}
