"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.interpolateString = exports.coalesceVariable = exports.stripReservedPrefixFromKeyTokens = exports.cleanupTempVars = exports.loadFromTokenMap = exports.storeInTokenMap = exports.storeVarToken = exports.ANNOYING_METHODS = exports.REGISTRY_URLS = void 0;
exports.REGISTRY_URLS = {
    google: 'https://dl.google.com/android/maven2/',
    gradlePluginPortal: 'https://plugins.gradle.org/m2/',
    jcenter: 'https://jcenter.bintray.com/',
    mavenCentral: 'https://repo.maven.apache.org/maven2',
};
exports.ANNOYING_METHODS = new Set([
    'createXmlValueRemover',
    'events',
    'args',
    'arrayOf',
    'listOf',
    'mutableListOf',
    'setOf',
    'mutableSetOf',
    'stages',
    'mapScalar', // https://github.com/apollographql/apollo-kotlin
]);
function storeVarToken(ctx, node) {
    ctx.varTokens.push(node);
    return ctx;
}
exports.storeVarToken = storeVarToken;
function storeInTokenMap(ctx, tokenMapKey) {
    ctx.tokenMap[tokenMapKey] = ctx.varTokens;
    ctx.varTokens = [];
    return ctx;
}
exports.storeInTokenMap = storeInTokenMap;
function loadFromTokenMap(ctx, tokenMapKey) {
    const tokens = ctx.tokenMap[tokenMapKey];
    if (!tokens) {
        throw new Error(`Expected token ${tokenMapKey} not found`);
    }
    return tokens;
}
exports.loadFromTokenMap = loadFromTokenMap;
function cleanupTempVars(ctx) {
    ctx.tokenMap = {};
    ctx.varTokens = [];
    return ctx;
}
exports.cleanupTempVars = cleanupTempVars;
function stripReservedPrefixFromKeyTokens(ctx) {
    const unwantedPrefixes = ['ext', 'extra', 'project', 'rootProject'];
    while (ctx.varTokens.length > 1 && // ensures there will be always at least one token
        ctx.varTokens[0] &&
        unwantedPrefixes.includes(ctx.varTokens[0].value)) {
        ctx.varTokens.shift();
    }
    return ctx;
}
exports.stripReservedPrefixFromKeyTokens = stripReservedPrefixFromKeyTokens;
function coalesceVariable(ctx) {
    if (ctx.varTokens.length > 1) {
        ctx.varTokens[0].value = ctx.varTokens
            .map((token) => token.value)
            .join('.');
        ctx.varTokens.length = 1;
    }
    return ctx;
}
exports.coalesceVariable = coalesceVariable;
function interpolateString(childTokens, variables) {
    const resolvedSubstrings = [];
    for (const childToken of childTokens) {
        const type = childToken.type;
        if (type === 'string-value') {
            resolvedSubstrings.push(childToken.value);
        }
        else if (type === 'symbol') {
            const varData = variables[childToken.value];
            if (varData) {
                resolvedSubstrings.push(varData.value);
            }
            else {
                return null;
            }
        }
        else {
            return null;
        }
    }
    return resolvedSubstrings.join('');
}
exports.interpolateString = interpolateString;
//# sourceMappingURL=common.js.map