"use strict";
const fs = require("fs");
const os = require("os");
const errors = require("./errors");
const iphone_simulator_xcode_simctl_1 = require("./iphone-simulator-xcode-simctl");
const _ = require("lodash");
class iPhoneSimulator {
    constructor() {
        this.simulator = null;
        this.simulator = this.createSimulator();
    }
    run(applicationPath, applicationIdentifier, options) {
        if (!fs.existsSync(applicationPath)) {
            errors.fail("Path does not exist ", applicationPath);
        }
        if (options.device) {
            const hasSuchDevice = _.find(this.simulator.getDevices(), device => device.name === options.device || device.id === options.device);
            if (!hasSuchDevice) {
                errors.fail(`Unable to find device ${options.device}.`);
            }
        }
        let sdkVersion = options.sdkVersion || options.sdk;
        if (sdkVersion) {
            let runtimeVersions = _.unique(_.map(this.simulator.getDevices(), (device) => device.runtimeVersion));
            if (!_.contains(runtimeVersions, sdkVersion)) {
                errors.fail(`Unable to find sdk ${sdkVersion}. The valid runtime versions are ${runtimeVersions.join(", ")}`);
            }
        }
        return this.simulator.run(applicationPath, applicationIdentifier, options);
    }
    printDeviceTypes() {
        let devices = this.simulator.getDevices();
        _.each(devices, device => console.log(`Device Identifier: ${device.fullId}. ${os.EOL}Runtime version: ${device.runtimeVersion} ${os.EOL}`));
    }
    printSDKS() {
        let sdks = this.simulator.getSdks();
        _.each(sdks, (sdk) => {
            let output = `    Display Name: ${sdk.displayName} ${os.EOL}    Version: ${sdk.version} ${os.EOL}`;
            if (sdk.rootPath) {
                output += `    Root path: ${sdk.rootPath} ${os.EOL}`;
            }
            console.log(output);
        });
    }
    sendNotification(notification, deviceId) {
        if (!notification) {
            errors.fail("Notification required.");
        }
        return this.simulator.sendNotification(notification, deviceId);
    }
    createSimulator() {
        return new iphone_simulator_xcode_simctl_1.XCodeSimctlSimulator();
    }
}
exports.iPhoneSimulator = iPhoneSimulator;
