"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.start = exports.processOpenOptions = void 0;
const debug_1 = __importDefault(require("debug"));
const util_1 = __importDefault(require("../util"));
const spawn_1 = require("./spawn");
const verify_1 = require("../tasks/verify");
const shared_1 = require("./shared");
const errors_1 = require("../errors");
const debug = (0, debug_1.default)('cypress:cli');
/**
 * Maps options collected by the CLI
 * and forms list of CLI arguments to the server.
 *
 * Note: there is lightweight validation, with errors
 * thrown synchronously.
 *
 * @returns {string[]} list of CLI arguments
 */
const processOpenOptions = (options = {}) => {
    // In addition to setting the project directory, setting the project option
    // here ultimately decides whether cypress is run in global mode or not.
    // It's first based off whether it's installed globally by npm/yarn (-g).
    // A global install can be overridden by the --project flag, putting Cypress
    // in project mode. A non-global install can be overridden by the --global
    // flag, putting it in global mode.
    if (!util_1.default.isInstalledGlobally() && !options.global && !options.project) {
        options.project = process.cwd();
    }
    const args = [];
    if (options.config) {
        args.push('--config', options.config);
    }
    if (options.configFile !== undefined) {
        (0, shared_1.checkConfigFile)(options);
        args.push('--config-file', options.configFile);
    }
    if (options.browser) {
        args.push('--browser', options.browser);
    }
    if (options.env) {
        args.push('--env', options.env);
    }
    if (options.port) {
        args.push('--port', options.port);
    }
    if (options.project) {
        args.push('--project', options.project);
    }
    if (options.global) {
        args.push('--global', options.global);
    }
    if (options.inspect) {
        args.push('--inspect');
    }
    if (options.inspectBrk) {
        args.push('--inspectBrk');
    }
    args.push(...(0, shared_1.processTestingType)(options));
    debug('opening from options %j', options);
    debug('command line arguments %j', args);
    return args;
};
exports.processOpenOptions = processOpenOptions;
const start = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* (options = {}) {
    function open() {
        try {
            const args = (0, exports.processOpenOptions)(options);
            return (0, spawn_1.start)(args, {
                dev: options.dev,
                detached: Boolean(options.detached),
            });
        }
        catch (err) {
            if (err.details) {
                return (0, errors_1.exitWithError)(err.details)();
            }
            throw err;
        }
    }
    if (options.dev) {
        return open();
    }
    yield (0, verify_1.start)();
    return open();
});
exports.start = start;
exports.default = {
    start: exports.start,
    processOpenOptions: exports.processOpenOptions,
};
