"use strict";
var Future = require("fibers/future");
var IosEmulatorServices = (function () {
    function IosEmulatorServices($logger, $emulatorSettingsService, $errors, $childProcess, $devicePlatformsConstants, $hostInfo, $options, $iOSSimResolver) {
        this.$logger = $logger;
        this.$emulatorSettingsService = $emulatorSettingsService;
        this.$errors = $errors;
        this.$childProcess = $childProcess;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$hostInfo = $hostInfo;
        this.$options = $options;
        this.$iOSSimResolver = $iOSSimResolver;
    }
    IosEmulatorServices.prototype.getEmulatorId = function () {
        return Future.fromResult("");
    };
    IosEmulatorServices.prototype.checkDependencies = function () {
        return Future.fromResult();
    };
    IosEmulatorServices.prototype.checkAvailability = function (dependsOnProject) {
        var _this = this;
        if (dependsOnProject === void 0) { dependsOnProject = true; }
        return (function () {
            if (!_this.$hostInfo.isDarwin) {
                _this.$errors.failWithoutHelp("iOS Simulator is available only on Mac OS X.");
            }
            var platform = _this.$devicePlatformsConstants.iOS;
            if (dependsOnProject && !_this.$emulatorSettingsService.canStart(platform).wait()) {
                _this.$errors.failWithoutHelp("The current project does not target iOS and cannot be run in the iOS Simulator.");
            }
        }).future()();
    };
    IosEmulatorServices.prototype.startEmulator = function () {
        return this.$iOSSimResolver.iOSSim.startSimulator();
    };
    IosEmulatorServices.prototype.runApplicationOnEmulator = function (app, emulatorOptions) {
        var _this = this;
        return (function () {
            return _this.runApplicationOnEmulatorCore(app, emulatorOptions);
        }).future()();
    };
    IosEmulatorServices.prototype.postDarwinNotification = function (notification) {
        var iosSimPath = this.$iOSSimResolver.iOSSimPath;
        var nodeCommandName = process.argv[0];
        var opts = ["notify-post", notification];
        if (this.$options.device) {
            opts.push("--device", this.$options.device);
        }
        return this.$childProcess.exec(nodeCommandName + " " + iosSimPath + " " + opts.join(' '));
    };
    IosEmulatorServices.prototype.runApplicationOnEmulatorCore = function (app, emulatorOptions) {
        this.$logger.info("Starting iOS Simulator");
        var iosSimPath = this.$iOSSimResolver.iOSSimPath;
        var nodeCommandName = process.argv[0];
        if (this.$options.availableDevices) {
            this.$childProcess.spawnFromEvent(nodeCommandName, [iosSimPath, "device-types"], "close", { stdio: "inherit" }).wait();
            return;
        }
        var opts = [
            iosSimPath,
            "launch", app, emulatorOptions.appId
        ];
        if (this.$options.timeout) {
            opts = opts.concat("--timeout", this.$options.timeout);
        }
        if (this.$options.sdk) {
            opts = opts.concat("--sdkVersion", this.$options.sdk);
        }
        if (!this.$options.justlaunch) {
            opts.push("--logging");
        }
        else {
            if (emulatorOptions) {
                if (emulatorOptions.stderrFilePath) {
                    opts = opts.concat("--stderr", emulatorOptions.stderrFilePath);
                }
                if (emulatorOptions.stdoutFilePath) {
                    opts = opts.concat("--stdout", emulatorOptions.stdoutFilePath);
                }
            }
            opts.push("--exit");
        }
        if (this.$options.device) {
            opts = opts.concat("--device", this.$options.device);
        }
        else if (emulatorOptions && emulatorOptions.deviceType) {
            opts = opts.concat("--device", emulatorOptions.deviceType);
        }
        if (emulatorOptions && emulatorOptions.args) {
            opts.push("--args=" + emulatorOptions.args);
        }
        if (emulatorOptions && emulatorOptions.waitForDebugger) {
            opts.push("--waitForDebugger");
        }
        var stdioOpts = { stdio: (emulatorOptions && emulatorOptions.captureStdin) ? "pipe" : "inherit" };
        return this.$childProcess.spawn(nodeCommandName, opts, stdioOpts);
    };
    return IosEmulatorServices;
})();
$injector.register("iOSEmulatorServices", IosEmulatorServices);
