"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const runAndroid_1 = require("../application/runAndroid");
const runIos_1 = require("../application/runIos");
const metroManager_1 = require("../application/metroManager");
const logger_1 = tslib_1.__importDefault(require("../application/logger"));
const iosUtils_1 = require("../application/iosUtils");
class Run extends core_1.Command {
    async run() {
        var _a, _b;
        const { args, flags } = await this.parse(Run);
        const shouldRunAndroid = (_a = flags.android) !== null && _a !== void 0 ? _a : flags.all;
        const shouldRunIos = (_b = flags.ios) !== null && _b !== void 0 ? _b : flags.all;
        const buildFlavor = flags.flavor;
        logger_1.default.setVerbose(flags.verbose);
        const start = performance.now();
        logger_1.default.info('Checking if metro is running...');
        const isMetroRunning = await (0, metroManager_1.checkIsMetroRunning)();
        if (!isMetroRunning) {
            logger_1.default.info('Metro is not running. Starting Metro');
            await (0, metroManager_1.startMetro)();
            logger_1.default.info('Metro started correctly');
        }
        else {
            logger_1.default.info('Metro is already running. Skipping metro start.');
        }
        if (shouldRunAndroid) {
            logger_1.default.info(`Running android app ${buildFlavor ? `with flavor ${buildFlavor}` : ''}`);
            await (0, runAndroid_1.runApp)(buildFlavor);
        }
        if (shouldRunIos) {
            logger_1.default.info(`Running ios app ${buildFlavor ? `with flavor ${buildFlavor}` : ''}`);
            await (0, runIos_1.runApp)(buildFlavor, iosUtils_1.iosBuildPlatforms.simulator);
        }
        logger_1.default.info(`Run finished in ${((performance.now() - start) / 1000).toFixed(1)} seconds`);
    }
}
exports.default = Run;
Run.description = 'Run the native app';
Run.examples = ['<%= config.bin %> <%= command.id %>'];
Run.flags = {
    android: core_1.Flags.boolean({ char: 'a', description: 'Run the android app' }),
    ios: core_1.Flags.boolean({ char: 'i', description: 'Run the ios app' }),
    flavor: core_1.Flags.string({ char: 'f', description: 'Specify flavor to build' }),
    verbose: core_1.Flags.boolean({ description: 'Verbose output' }),
};
Run.args = {
// file: Args.string({description: 'file to read'}),
};
