"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments)).next());
    });
};
const configXmlUtils_1 = require("./configXmlUtils");
const cli_utils_1 = require("@ionic/cli-utils");
/**
 * Filter and gather arguments from command line to be passed to Cordova
 */
function filterArgumentsForCordova(metadata, inputs, options) {
    const results = cli_utils_1.normalizeOptionAliases(metadata, options);
    const args = cli_utils_1.minimistOptionsToArray(metadata, results);
    // clean out any cmds that may confuse cordova
    const ignoreCmds = [
        '--livereload',
        '--consolelogs',
        '--serverlogs',
        '--port',
        '--livereload-port',
        '--address'
    ];
    const cleanOptions = args.filter(function (arg, index, fullList) {
        // Only return true if the arg is not found in the ignore Cmds list
        return ignoreCmds.every(ignoreCmd => {
            return arg.search(ignoreCmd) !== 0;
        });
    });
    return [metadata.name].concat(inputs, cleanOptions);
}
exports.filterArgumentsForCordova = filterArgumentsForCordova;
/**
 * Start the app scripts server for emulator or device
 */
function startAppScriptsServer(metadata, inputs, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const results = cli_utils_1.normalizeOptionAliases(metadata, options);
        let args = cli_utils_1.minimistOptionsToArray(metadata, results);
        // Serve specific options not related to the actual run or emulate code
        args = args.concat([
            '--iscordovaserve',
            '--nobrowser'
        ]);
        const serveSettings = yield cli_utils_1.appScriptsServe(args);
        yield configXmlUtils_1.writeConfigXmlContentSrc(process.cwd(), serveSettings.url);
    });
}
exports.startAppScriptsServer = startAppScriptsServer;
function runAppScriptsBuild(metadata, inputs, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const results = cli_utils_1.normalizeOptionAliases(metadata, options);
        let args = cli_utils_1.minimistOptionsToArray(metadata, results);
        yield cli_utils_1.appScriptsBuild(args);
    });
}
exports.runAppScriptsBuild = runAppScriptsBuild;
