"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Utility for parsing script startup parameters.
 *
 * Types of script parameters:
 *      keyWithoutValue - key without value.
 *      keylessValue - param without key.
 *      --key=value - primitive type value with key.
 *      --collectionKey[]=value1,value2 - array type value with key. Values ​​are comma separated.
 *
 * Example:
 *
 * node script.js --keyWithoutValue keylessValue1 --key=value --collectionKey[]=value1,value2
 */
class ArgumentParser {
    /**
     * Get parsed arguments.
     *
     * @param rawArgs Collection of raw arguments.
     * @return Argument store.
     */
    get(rawArgs) {
        const keylessValues = rawArgs.filter(arg => !arg.startsWith(ArgumentParser.KEY_PREFIX));
        const valuesWithKey = rawArgs.filter(arg => arg.startsWith(ArgumentParser.KEY_PREFIX));
        const booleanValues = valuesWithKey.filter(arg => !this.isExpression(arg));
        const booleanEntries = booleanValues.map(key => [this.getKeyFromRawKey(key), true]);
        const expressions = valuesWithKey.filter(arg => this.isExpression(arg));
        const expressionEntries = expressions.map(expr => expr.split(ArgumentParser.EXPRESSION_SIGN))
            .map(([rawKey, rawValue]) => {
            const isListType = this.isListTypeExpression(rawKey);
            const key = this.getKeyFromRawKey(rawKey);
            const value = isListType ? rawValue.split(ArgumentParser.LIST_VALUE_SEPARATOR) : rawValue;
            return [key, value];
        });
        return new Map([
            ...(keylessValues.length ? [[ArgumentParser.DEFAULT_KEY, keylessValues]] : []),
            ...booleanEntries,
            ...expressionEntries
        ]);
    }
    // noinspection JSMethodCanBeStatic
    getKeyFromRawKey(rawKey) {
        return rawKey.replace(/^-+/, '')
            .replace(ArgumentParser.LIST_TYPE_KEY, '');
    }
    // noinspection JSMethodCanBeStatic
    isExpression(arg) {
        return arg.includes(ArgumentParser.EXPRESSION_SIGN);
    }
    // noinspection JSMethodCanBeStatic
    isListTypeExpression(expr) {
        return expr.endsWith(ArgumentParser.LIST_TYPE_KEY);
    }
}
ArgumentParser.DEFAULT_KEY = '_';
ArgumentParser.KEY_PREFIX = '-';
ArgumentParser.EXPRESSION_SIGN = '=';
ArgumentParser.LIST_TYPE_KEY = '[]';
ArgumentParser.LIST_VALUE_SEPARATOR = ',';
exports.ArgumentParser = ArgumentParser;
//# sourceMappingURL=argument-parser.js.map