"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_app_data_base_1 = require("../../mobile/device-app-data/device-app-data-base");
var Future = require("fibers/future");
var querystring = require("querystring");
var path = require("path");
var util = require("util");
var constants_1 = require("../../mobile/constants");
var AndroidAppIdentifier = (function (_super) {
    __extends(AndroidAppIdentifier, _super);
    function AndroidAppIdentifier(_appIdentifier, device, platform, $errors, $deployHelper, $devicePlatformsConstants) {
        _super.call(this, _appIdentifier);
        this.device = device;
        this.platform = platform;
        this.$errors = $errors;
        this.$deployHelper = $deployHelper;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this._deviceProjectRootPath = null;
    }
    Object.defineProperty(AndroidAppIdentifier.prototype, "deviceProjectRootPath", {
        get: function () {
            if (!this._deviceProjectRootPath) {
                var deviceTmpDirFormat = "";
                var version = this.getLiveSyncVersion().wait();
                if (version === 2) {
                    deviceTmpDirFormat = constants_1.LiveSyncConstants.DEVICE_TMP_DIR_FORMAT_V2;
                }
                else if (version === 3) {
                    deviceTmpDirFormat = constants_1.LiveSyncConstants.DEVICE_TMP_DIR_FORMAT_V3;
                }
                else {
                    this.$errors.failWithoutHelp("Unsupported LiveSync version: " + version);
                }
                this._deviceProjectRootPath = this.getDeviceProjectRootPath(util.format(deviceTmpDirFormat, this.appIdentifier));
            }
            return this._deviceProjectRootPath;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(AndroidAppIdentifier.prototype, "liveSyncFormat", {
        get: function () {
            return null;
        },
        enumerable: true,
        configurable: true
    });
    AndroidAppIdentifier.prototype.encodeLiveSyncHostUri = function (hostUri) {
        return hostUri;
    };
    AndroidAppIdentifier.prototype.getLiveSyncNotSupportedError = function () {
        return "You can't LiveSync on device with id " + this.device.deviceInfo.identifier + "! Deploy the app with LiveSync enabled and wait for the initial start up before LiveSyncing.";
    };
    AndroidAppIdentifier.prototype.isLiveSyncSupported = function () {
        var _this = this;
        return (function () {
            var isApplicationInstalled = _this.device.applicationManager.isApplicationInstalled(_this.appIdentifier).wait();
            if (!isApplicationInstalled) {
                _this.$deployHelper.deploy(_this.$devicePlatformsConstants.Android.toLowerCase()).wait();
            }
            return _this.getLiveSyncVersion().wait() !== 0;
        }).future()();
    };
    AndroidAppIdentifier.prototype.getLiveSyncVersion = function () {
        var _this = this;
        return (function () {
            if (!_this._liveSyncVersion) {
                _this._liveSyncVersion = _this.device.adb.sendBroadcastToDevice(constants_1.LiveSyncConstants.CHECK_LIVESYNC_INTENT_NAME, { "app-id": _this.appIdentifier }).wait();
            }
            return _this._liveSyncVersion;
        }).future()();
    };
    return AndroidAppIdentifier;
})(device_app_data_base_1.DeviceAppDataBase);
exports.AndroidAppIdentifier = AndroidAppIdentifier;
var AndroidCompanionAppIdentifier = (function (_super) {
    __extends(AndroidCompanionAppIdentifier, _super);
    function AndroidCompanionAppIdentifier(_appIdentifier, device, platform, $companionAppsService, $projectConstants) {
        _super.call(this, $companionAppsService.getCompanionAppIdentifier($projectConstants.TARGET_FRAMEWORK_IDENTIFIERS.Cordova, platform));
        this.device = device;
        this.platform = platform;
        this.$companionAppsService = $companionAppsService;
        this.$projectConstants = $projectConstants;
    }
    Object.defineProperty(AndroidCompanionAppIdentifier.prototype, "deviceProjectRootPath", {
        get: function () {
            return this.getDeviceProjectRootPath(util.format(constants_1.LiveSyncConstants.DEVICE_TMP_DIR_FORMAT_V3, this.appIdentifier));
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(AndroidCompanionAppIdentifier.prototype, "liveSyncFormat", {
        get: function () {
            return "icenium://%s?token=%s&appId=%s&configuration=%s";
        },
        enumerable: true,
        configurable: true
    });
    AndroidCompanionAppIdentifier.prototype.encodeLiveSyncHostUri = function (hostUri) {
        return querystring.escape(hostUri);
    };
    AndroidCompanionAppIdentifier.prototype.getLiveSyncNotSupportedError = function () {
        return "Cannot LiveSync changes to the companion app. The companion app is not installed on " + this.device.deviceInfo.identifier + ".";
    };
    AndroidCompanionAppIdentifier.prototype.isLiveSyncSupported = function () {
        return this.device.applicationManager.isApplicationInstalled(this.appIdentifier);
    };
    return AndroidCompanionAppIdentifier;
})(device_app_data_base_1.DeviceAppDataBase);
exports.AndroidCompanionAppIdentifier = AndroidCompanionAppIdentifier;
var AndroidNativeScriptCompanionAppIdentifier = (function (_super) {
    __extends(AndroidNativeScriptCompanionAppIdentifier, _super);
    function AndroidNativeScriptCompanionAppIdentifier(_appIdentifier, device, platform, $companionAppsService, $projectConstants) {
        _super.call(this, $companionAppsService.getCompanionAppIdentifier($projectConstants.TARGET_FRAMEWORK_IDENTIFIERS.NativeScript, platform));
        this.device = device;
        this.platform = platform;
        this.$companionAppsService = $companionAppsService;
        this.$projectConstants = $projectConstants;
    }
    Object.defineProperty(AndroidNativeScriptCompanionAppIdentifier.prototype, "deviceProjectRootPath", {
        get: function () {
            return util.format(constants_1.LiveSyncConstants.DEVICE_TMP_DIR_FORMAT_V3, this.appIdentifier);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(AndroidNativeScriptCompanionAppIdentifier.prototype, "liveSyncFormat", {
        get: function () {
            return "nativescript://%s?token=%s&appId=%s&configuration=%s";
        },
        enumerable: true,
        configurable: true
    });
    AndroidNativeScriptCompanionAppIdentifier.prototype.encodeLiveSyncHostUri = function (hostUri) {
        return querystring.escape(hostUri);
    };
    AndroidNativeScriptCompanionAppIdentifier.prototype.getLiveSyncNotSupportedError = function () {
        return "Cannot LiveSync changes to the NativeScript companion app. The NativeScript companion app is not installed on " + this.device.deviceInfo.identifier + ".";
    };
    AndroidNativeScriptCompanionAppIdentifier.prototype.isLiveSyncSupported = function () {
        return this.device.applicationManager.isApplicationInstalled(this.appIdentifier);
    };
    return AndroidNativeScriptCompanionAppIdentifier;
})(device_app_data_base_1.DeviceAppDataBase);
exports.AndroidNativeScriptCompanionAppIdentifier = AndroidNativeScriptCompanionAppIdentifier;
var IOSAppIdentifier = (function (_super) {
    __extends(IOSAppIdentifier, _super);
    function IOSAppIdentifier(_appIdentifier, device, platform, $iOSSimResolver) {
        _super.call(this, _appIdentifier);
        this.device = device;
        this.platform = platform;
        this.$iOSSimResolver = $iOSSimResolver;
        this._deviceProjectRootPath = null;
    }
    Object.defineProperty(IOSAppIdentifier.prototype, "deviceProjectRootPath", {
        get: function () {
            if (!this._deviceProjectRootPath) {
                if (this.device.isEmulator) {
                    var applicationPath = this.$iOSSimResolver.iOSSim.getApplicationPath(this.device.deviceInfo.identifier, this.appIdentifier);
                    this._deviceProjectRootPath = path.join(applicationPath, "www");
                }
                else {
                    this._deviceProjectRootPath = constants_1.LiveSyncConstants.IOS_PROJECT_PATH;
                }
            }
            return this._deviceProjectRootPath;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IOSAppIdentifier.prototype, "liveSyncFormat", {
        get: function () {
            return null;
        },
        enumerable: true,
        configurable: true
    });
    IOSAppIdentifier.prototype.encodeLiveSyncHostUri = function (hostUri) {
        return querystring.escape(hostUri);
    };
    IOSAppIdentifier.prototype.getLiveSyncNotSupportedError = function () {
        return "You can't LiveSync on device with id " + this.device.deviceInfo.identifier + "! Deploy the app with LiveSync enabled and wait for the initial start up before LiveSyncing.";
    };
    IOSAppIdentifier.prototype.isLiveSyncSupported = function () {
        return Future.fromResult(true);
    };
    return IOSAppIdentifier;
})(device_app_data_base_1.DeviceAppDataBase);
exports.IOSAppIdentifier = IOSAppIdentifier;
var IOSNativeScriptAppIdentifier = (function (_super) {
    __extends(IOSNativeScriptAppIdentifier, _super);
    function IOSNativeScriptAppIdentifier(_appIdentifier, device, platform, $iOSSimResolver) {
        _super.call(this, _appIdentifier);
        this.device = device;
        this.platform = platform;
        this.$iOSSimResolver = $iOSSimResolver;
        this._deviceProjectRootPath = null;
    }
    Object.defineProperty(IOSNativeScriptAppIdentifier.prototype, "deviceProjectRootPath", {
        get: function () {
            if (!this._deviceProjectRootPath) {
                if (this.device.isEmulator) {
                    var applicationPath = this.$iOSSimResolver.iOSSim.getApplicationPath(this.device.deviceInfo.identifier, this.appIdentifier);
                    this._deviceProjectRootPath = applicationPath;
                }
                else {
                    this._deviceProjectRootPath = constants_1.LiveSyncConstants.IOS_PROJECT_PATH;
                }
            }
            return this._deviceProjectRootPath;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IOSNativeScriptAppIdentifier.prototype, "liveSyncFormat", {
        get: function () {
            return null;
        },
        enumerable: true,
        configurable: true
    });
    IOSNativeScriptAppIdentifier.prototype.encodeLiveSyncHostUri = function (hostUri) {
        return querystring.escape(hostUri);
    };
    IOSNativeScriptAppIdentifier.prototype.getLiveSyncNotSupportedError = function () {
        return "You can't LiveSync on device with id " + this.device.deviceInfo.identifier + "! Deploy the app with LiveSync enabled and wait for the initial start up before LiveSyncing.";
    };
    IOSNativeScriptAppIdentifier.prototype.isLiveSyncSupported = function () {
        return Future.fromResult(true);
    };
    return IOSNativeScriptAppIdentifier;
})(device_app_data_base_1.DeviceAppDataBase);
exports.IOSNativeScriptAppIdentifier = IOSNativeScriptAppIdentifier;
var IOSCompanionAppIdentifier = (function (_super) {
    __extends(IOSCompanionAppIdentifier, _super);
    function IOSCompanionAppIdentifier(device, platform, $companionAppsService, $projectConstants) {
        _super.call(this, $companionAppsService.getCompanionAppIdentifier($projectConstants.TARGET_FRAMEWORK_IDENTIFIERS.Cordova, platform));
        this.device = device;
        this.platform = platform;
        this.$companionAppsService = $companionAppsService;
        this.$projectConstants = $projectConstants;
    }
    Object.defineProperty(IOSCompanionAppIdentifier.prototype, "deviceProjectRootPath", {
        get: function () {
            return constants_1.LiveSyncConstants.IOS_PROJECT_PATH;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IOSCompanionAppIdentifier.prototype, "liveSyncFormat", {
        get: function () {
            return "icenium://%s?LiveSyncToken=%s&appId=%s&configuration=%s";
        },
        enumerable: true,
        configurable: true
    });
    IOSCompanionAppIdentifier.prototype.encodeLiveSyncHostUri = function (hostUri) {
        return querystring.escape(hostUri);
    };
    IOSCompanionAppIdentifier.prototype.getLiveSyncNotSupportedError = function () {
        return "Cannot LiveSync changes to the companion app. The companion app is not installed on " + this.device.deviceInfo.identifier + ".";
    };
    IOSCompanionAppIdentifier.prototype.isLiveSyncSupported = function () {
        return this.device.applicationManager.isApplicationInstalled(this.appIdentifier);
    };
    return IOSCompanionAppIdentifier;
})(device_app_data_base_1.DeviceAppDataBase);
exports.IOSCompanionAppIdentifier = IOSCompanionAppIdentifier;
var IOSNativeScriptCompanionAppIdentifier = (function (_super) {
    __extends(IOSNativeScriptCompanionAppIdentifier, _super);
    function IOSNativeScriptCompanionAppIdentifier(device, platform, $companionAppsService, $projectConstants) {
        _super.call(this, $companionAppsService.getCompanionAppIdentifier($projectConstants.TARGET_FRAMEWORK_IDENTIFIERS.NativeScript, platform));
        this.device = device;
        this.platform = platform;
        this.$companionAppsService = $companionAppsService;
        this.$projectConstants = $projectConstants;
    }
    Object.defineProperty(IOSNativeScriptCompanionAppIdentifier.prototype, "deviceProjectRootPath", {
        get: function () {
            return constants_1.LiveSyncConstants.IOS_PROJECT_PATH;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IOSNativeScriptCompanionAppIdentifier.prototype, "liveSyncFormat", {
        get: function () {
            return "nativescript://%s?LiveSyncToken=%s&appId=%s&configuration=%s";
        },
        enumerable: true,
        configurable: true
    });
    IOSNativeScriptCompanionAppIdentifier.prototype.encodeLiveSyncHostUri = function (hostUri) {
        return querystring.escape(hostUri);
    };
    IOSNativeScriptCompanionAppIdentifier.prototype.getLiveSyncNotSupportedError = function () {
        return "Cannot LiveSync changes to the NativeScript companion app. The NativeScript companion app is not installed on " + this.device.deviceInfo.identifier + ".";
    };
    IOSNativeScriptCompanionAppIdentifier.prototype.isLiveSyncSupported = function () {
        return this.device.applicationManager.isApplicationInstalled(this.appIdentifier);
    };
    return IOSNativeScriptCompanionAppIdentifier;
})(device_app_data_base_1.DeviceAppDataBase);
exports.IOSNativeScriptCompanionAppIdentifier = IOSNativeScriptCompanionAppIdentifier;
var WP8CompanionAppIdentifier = (function (_super) {
    __extends(WP8CompanionAppIdentifier, _super);
    function WP8CompanionAppIdentifier(device, platform, $companionAppsService, $projectConstants) {
        _super.call(this, $companionAppsService.getCompanionAppIdentifier($projectConstants.TARGET_FRAMEWORK_IDENTIFIERS.Cordova, platform));
        this.device = device;
        this.platform = platform;
        this.$companionAppsService = $companionAppsService;
        this.$projectConstants = $projectConstants;
    }
    Object.defineProperty(WP8CompanionAppIdentifier.prototype, "deviceProjectRootPath", {
        get: function () {
            return "";
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(WP8CompanionAppIdentifier.prototype, "liveSyncFormat", {
        get: function () {
            return "%s/Mist/MobilePackage/redirect?token=%s&appId=%s&configuration=%s";
        },
        enumerable: true,
        configurable: true
    });
    WP8CompanionAppIdentifier.prototype.encodeLiveSyncHostUri = function (hostUri) {
        return hostUri;
    };
    WP8CompanionAppIdentifier.prototype.isLiveSyncSupported = function () {
        return Future.fromResult(true);
    };
    WP8CompanionAppIdentifier.prototype.getLiveSyncNotSupportedError = function () {
        return "";
    };
    return WP8CompanionAppIdentifier;
})(device_app_data_base_1.DeviceAppDataBase);
exports.WP8CompanionAppIdentifier = WP8CompanionAppIdentifier;
var DeviceAppDataProvider = (function () {
    function DeviceAppDataProvider($project) {
        this.$project = $project;
    }
    DeviceAppDataProvider.prototype.createFactoryRules = function () {
        var rules = {
            Cordova: {
                Android: {
                    vanilla: AndroidAppIdentifier,
                    companion: AndroidCompanionAppIdentifier
                },
                iOS: {
                    vanilla: IOSAppIdentifier,
                    companion: IOSCompanionAppIdentifier
                },
                WP8: {
                    vanilla: "",
                    companion: WP8CompanionAppIdentifier
                }
            },
            NativeScript: {
                Android: {
                    vanilla: AndroidAppIdentifier,
                    companion: AndroidNativeScriptCompanionAppIdentifier
                },
                iOS: {
                    vanilla: IOSNativeScriptAppIdentifier,
                    companion: IOSNativeScriptCompanionAppIdentifier
                }
            }
        };
        return rules[this.$project.projectData.Framework];
    };
    return DeviceAppDataProvider;
})();
exports.DeviceAppDataProvider = DeviceAppDataProvider;
$injector.register("deviceAppDataProvider", DeviceAppDataProvider);
