"use strict";
const path = require("path");
const future = require("fibers/future");
class Wp8EmulatorServices {
    constructor($logger, $emulatorSettingsService, $errors, $childProcess, $devicePlatformsConstants, $hostInfo, $fs) {
        this.$logger = $logger;
        this.$emulatorSettingsService = $emulatorSettingsService;
        this.$errors = $errors;
        this.$childProcess = $childProcess;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$hostInfo = $hostInfo;
        this.$fs = $fs;
    }
    static get programFilesPath() {
        return (process.arch === "x64") ? process.env["PROGRAMFILES(X86)"] : process.env.ProgramFiles;
    }
    getEmulatorId() {
        return future.fromResult("");
    }
    checkDependencies() {
        return future.fromResult();
    }
    checkAvailability() {
        return (() => {
            if (!this.$fs.exists(this.getPathToEmulatorStarter()).wait()) {
                this.$errors.failWithoutHelp("You do not have Windows Phone 8 SDK installed. Please install it in order to continue.");
            }
            if (!this.$hostInfo.isWindows) {
                this.$errors.fail("Windows Phone Emulator is available only on Windows 8 or later.");
            }
            let platform = this.$devicePlatformsConstants.WP8;
            if (!this.$emulatorSettingsService.canStart(platform).wait()) {
                this.$errors.fail("The current project does not target Windows Phone 8 and cannot be run in the Windows Phone emulator.");
            }
        }).future()();
    }
    startEmulator() {
        return future.fromResult("Not implemented.");
    }
    runApplicationOnEmulator(app, emulatorOptions) {
        return (() => {
            this.$logger.info("Starting Windows Phone Emulator");
            let emulatorStarter = this.getPathToEmulatorStarter();
            this.$childProcess.spawn(emulatorStarter, ["/installlaunch", app, "/targetdevice:xd"], { stdio: "ignore", detached: true }).unref();
        }).future()();
    }
    getPathToEmulatorStarter() {
        return path.join(Wp8EmulatorServices.programFilesPath, Wp8EmulatorServices.WP8_LAUNCHER_PATH, Wp8EmulatorServices.WP8_LAUNCHER);
    }
}
Wp8EmulatorServices.WP8_LAUNCHER = "XapDeployCmd.exe";
Wp8EmulatorServices.WP8_LAUNCHER_PATH = "Microsoft SDKs\\Windows Phone\\v8.0\\Tools\\XAP Deployment";
$injector.register("wp8EmulatorServices", Wp8EmulatorServices);
