"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cli_utils_1 = require("@ionic/cli-utils");
exports.CORDOVA_INTENT = 'CORDOVA';
function filterByIntent(metadata, options, intentName) {
    return Object.keys(options).reduce((allOptions, optionName) => {
        const metadataOptionFound = (metadata.options || []).find((mdOption) => (mdOption.name === optionName || (mdOption.aliases || []).includes(optionName)));
        if (metadataOptionFound) {
            if (intentName && metadataOptionFound.intent === intentName) {
                allOptions[optionName] = options[optionName];
            }
            else if (!intentName && !metadataOptionFound.intent) {
                allOptions[optionName] = options[optionName];
            }
        }
        return allOptions;
    }, {});
}
/**
 * Filter and gather arguments from command line to be passed to Cordova
 */
function filterArgumentsForCordova(metadata, inputs, options) {
    const results = filterByIntent(metadata, options, exports.CORDOVA_INTENT);
    const args = cli_utils_1.minimistOptionsToArray(results);
    return [metadata.name].concat(inputs, args);
}
exports.filterArgumentsForCordova = filterArgumentsForCordova;
/**
 * Gather arguments from command line to be passed to Cordova
 */
function gatherArgumentsForCordova(metadata, inputs, options) {
    const args = cli_utils_1.minimistOptionsToArray(options);
    return [metadata.name].concat(inputs, args);
}
exports.gatherArgumentsForCordova = gatherArgumentsForCordova;
/**
 * Start the app scripts server for emulator or device
 */
function generateBuildOptions(metadata, options) {
    const results = filterByIntent(metadata, options);
    // Serve specific options not related to the actual run or emulate code
    return Object.assign({}, results, { 'iscordovaserve': true, 'externalIpRequired': true, 'nobrowser': true });
}
exports.generateBuildOptions = generateBuildOptions;
