"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const helpers = require("../helpers");
const shell = require("shelljs");
const temp = require("temp");
class MobileHelper {
    constructor($errors, $fs, $devicePlatformsConstants) {
        this.$errors = $errors;
        this.$fs = $fs;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
    }
    get platformNames() {
        return [this.$devicePlatformsConstants.iOS, this.$devicePlatformsConstants.Android];
    }
    isAndroidPlatform(platform) {
        return !!(platform && (this.$devicePlatformsConstants.Android.toLowerCase() === platform.toLowerCase()));
    }
    isiOSPlatform(platform) {
        return !!(platform && (this.$devicePlatformsConstants.iOS.toLowerCase() === platform.toLowerCase()));
    }
    normalizePlatformName(platform) {
        if (this.isAndroidPlatform(platform)) {
            return "Android";
        }
        else if (this.isiOSPlatform(platform)) {
            return "iOS";
        }
        return undefined;
    }
    validatePlatformName(platform) {
        if (!platform) {
            this.$errors.fail("No device platform specified.");
        }
        const normalizedPlatform = this.normalizePlatformName(platform);
        if (!normalizedPlatform || !_.includes(this.platformNames, normalizedPlatform)) {
            this.$errors.fail("'%s' is not a valid device platform. Valid platforms are %s.", platform, helpers.formatListOfNames(this.platformNames));
        }
        return normalizedPlatform;
    }
    buildDevicePath(...args) {
        return this.correctDevicePath(args.join(MobileHelper.DEVICE_PATH_SEPARATOR));
    }
    correctDevicePath(filePath) {
        return helpers.stringReplaceAll(filePath, '\\', '/');
    }
    isiOSTablet(deviceName) {
        return deviceName && deviceName.toLowerCase().indexOf("ipad") !== -1;
    }
    getDeviceFileContent(device, deviceFilePath, projectData) {
        return __awaiter(this, void 0, void 0, function* () {
            temp.track();
            const uniqueFilePath = temp.path({ suffix: ".tmp" });
            const platform = device.deviceInfo.platform.toLowerCase();
            try {
                yield device.fileSystem.getFile(deviceFilePath, projectData.projectIdentifiers[platform], uniqueFilePath);
            }
            catch (e) {
                return null;
            }
            if (this.$fs.exists(uniqueFilePath)) {
                const text = this.$fs.readText(uniqueFilePath);
                shell.rm(uniqueFilePath);
                return text;
            }
            return null;
        });
    }
}
MobileHelper.DEVICE_PATH_SEPARATOR = "/";
exports.MobileHelper = MobileHelper;
$injector.register("mobileHelper", MobileHelper);
