"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var device_discovery_1 = require("./device-discovery");
var Future = require("fibers/future");
var ios_simulator_device_1 = require("./../ios/simulator/ios-simulator-device");
var IOSSimulatorDiscovery = (function (_super) {
    __extends(IOSSimulatorDiscovery, _super);
    function IOSSimulatorDiscovery($injector, $iOSSimResolver, $hostInfo) {
        _super.call(this);
        this.$injector = $injector;
        this.$iOSSimResolver = $iOSSimResolver;
        this.$hostInfo = $hostInfo;
    }
    IOSSimulatorDiscovery.prototype.startLookingForDevices = function () {
        return this.checkForDevices(new Future());
    };
    IOSSimulatorDiscovery.prototype.checkForDevices = function (future) {
        if (this.$hostInfo.isDarwin) {
            var currentSimulator = this.$iOSSimResolver.iOSSim.getRunningSimulator();
            if (currentSimulator) {
                if (!this.cachedSimulator) {
                    this.createAndAddDevice(currentSimulator);
                }
                else if (this.cachedSimulator.id !== currentSimulator.id) {
                    this.removeDevice(this.cachedSimulator.id);
                    this.createAndAddDevice(currentSimulator);
                }
            }
            else if (this.cachedSimulator) {
                this.removeDevice(this.cachedSimulator.id);
                this.cachedSimulator = null;
            }
        }
        if (future) {
            future.return();
        }
        return future || Future.fromResult();
    };
    IOSSimulatorDiscovery.prototype.createAndAddDevice = function (simulator) {
        this.cachedSimulator = _.cloneDeep(simulator);
        this.addDevice(this.$injector.resolve(ios_simulator_device_1.IOSSimulator, { simulator: this.cachedSimulator }));
    };
    return IOSSimulatorDiscovery;
}(device_discovery_1.DeviceDiscovery));
exports.IOSSimulatorDiscovery = IOSSimulatorDiscovery;
$injector.register("iOSSimulatorDiscovery", IOSSimulatorDiscovery);
