"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IOSDeviceDiscovery = void 0;
const device_discovery_1 = require("./device-discovery");
const ios_device_1 = require("../ios/device/ios-device");
const yok_1 = require("../../yok");
class IOSDeviceDiscovery extends device_discovery_1.DeviceDiscovery {
    constructor($injector, $logger, $mobileHelper, $iosDeviceOperations, $devicePlatformsConstants) {
        super();
        this.$injector = $injector;
        this.$logger = $logger;
        this.$mobileHelper = $mobileHelper;
        this.$iosDeviceOperations = $iosDeviceOperations;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
    }
    startLookingForDevices(options) {
        return __awaiter(this, void 0, void 0, function* () {
            this.$logger.trace("Options for ios-device-discovery", options);
            if (options &&
                options.platform &&
                (!this.$mobileHelper.isApplePlatfrom(options.platform) ||
                    options.emulator)) {
                return;
            }
            if (this.$mobileHelper.isvisionOSPlatform(options.platform)) {
                options.platform = this.$devicePlatformsConstants.iOS;
            }
            yield this.$iosDeviceOperations.startLookingForDevices((deviceInfo) => {
                const device = this.createDevice(deviceInfo);
                this.addDevice(device);
            }, (deviceInfo) => {
                const currentDevice = this.getDevice(deviceInfo.deviceId);
                if (currentDevice) {
                    const device = this.createDevice(deviceInfo);
                    this.updateDeviceInfo(device);
                }
                else {
                    const device = this.createDevice(deviceInfo);
                    this.addDevice(device);
                }
            }, (deviceInfo) => {
                this.removeDevice(deviceInfo.deviceId);
            }, options);
        });
    }
    createDevice(deviceActionInfo) {
        const device = this.$injector.resolve(ios_device_1.IOSDevice, {
            deviceActionInfo: deviceActionInfo,
        });
        return device;
    }
}
exports.IOSDeviceDiscovery = IOSDeviceDiscovery;
yok_1.injector.register("iOSDeviceDiscovery", IOSDeviceDiscovery);
