"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.AndroidDeviceDiscovery = void 0;
const device_discovery_1 = require("./device-discovery");
const android_device_1 = require("../android/android-device");
const yok_1 = require("../../yok");
const _ = require("lodash");
class AndroidDeviceDiscovery extends device_discovery_1.DeviceDiscovery {
    constructor($injector, $adb, $mobileHelper) {
        super();
        this.$injector = $injector;
        this.$adb = $adb;
        this.$mobileHelper = $mobileHelper;
        this._devices = [];
    }
    createAndAddDevice(adbDeviceInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            this._devices.push(adbDeviceInfo);
            const device = this.$injector.resolve(android_device_1.AndroidDevice, { identifier: adbDeviceInfo.identifier, status: adbDeviceInfo.status });
            yield device.init();
            this.addDevice(device);
        });
    }
    deleteAndRemoveDevice(deviceIdentifier) {
        _.remove(this._devices, (d) => d.identifier === deviceIdentifier);
        this.removeDevice(deviceIdentifier);
    }
    startLookingForDevices(options) {
        return __awaiter(this, void 0, void 0, function* () {
            if (options &&
                options.platform &&
                !this.$mobileHelper.isAndroidPlatform(options.platform)) {
                return;
            }
            yield this.ensureAdbServerStarted();
            yield this.checkForDevices();
        });
    }
    checkForDevices() {
        return __awaiter(this, void 0, void 0, function* () {
            const devices = yield this.$adb.getDevices();
            yield this.checkCurrentData(devices);
        });
    }
    checkCurrentData(result) {
        return __awaiter(this, void 0, void 0, function* () {
            const currentDevices = result.map((element) => {
                // http://developer.android.com/tools/help/adb.html#devicestatus
                const data = element.split("\t");
                const identifier = data[0];
                const status = data[1];
                return {
                    identifier: identifier,
                    status: status,
                };
            });
            _(this._devices)
                .reject((d) => (_.find(currentDevices, (device) => device.identifier === d.identifier && device.status === d.status)))
                .each((d) => this.deleteAndRemoveDevice(d.identifier));
            yield Promise.all(_(currentDevices)
                .reject((d) => (_.find(this._devices, (device) => device.identifier === d.identifier &&
                device.status === d.status)))
                .map((d) => this.createAndAddDevice(d))
                .value());
        });
    }
    ensureAdbServerStarted() {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.isStarted) {
                this.isStarted = true;
                try {
                    return yield this.$adb.executeCommand(["start-server"]);
                }
                catch (err) {
                    this.isStarted = false;
                    throw err;
                }
            }
        });
    }
}
exports.AndroidDeviceDiscovery = AndroidDeviceDiscovery;
yok_1.injector.register("androidDeviceDiscovery", AndroidDeviceDiscovery);
//# sourceMappingURL=android-device-discovery.js.map