"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.MobileHelper = void 0;
const helpers = require("../helpers");
const shell = require("shelljs");
const _ = require("lodash");
const yok_1 = require("../yok");
class MobileHelper {
    constructor($errors, $fs, $devicePlatformsConstants, $tempService) {
        this.$errors = $errors;
        this.$fs = $fs;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$tempService = $tempService;
    }
    get platformNames() {
        return [
            this.$devicePlatformsConstants.iOS,
            this.$devicePlatformsConstants.Android,
            this.$devicePlatformsConstants.visionOS,
        ];
    }
    isAndroidPlatform(platform) {
        return !!(platform &&
            this.$devicePlatformsConstants.Android.toLowerCase() ===
                platform.toLowerCase());
    }
    isiOSPlatform(platform) {
        return !!(platform &&
            this.$devicePlatformsConstants.iOS.toLowerCase() ===
                platform.toLowerCase());
    }
    isvisionOSPlatform(platform) {
        return !!(platform &&
            this.$devicePlatformsConstants.visionOS.toLowerCase() ===
                platform.toLowerCase());
    }
    isApplePlatform(platform) {
        return this.isiOSPlatform(platform) || this.isvisionOSPlatform(platform);
    }
    normalizePlatformName(platform) {
        if (this.isAndroidPlatform(platform)) {
            return "Android";
        }
        else if (this.isiOSPlatform(platform)) {
            return "iOS";
        }
        else if (this.isvisionOSPlatform(platform)) {
            return "visionOS";
        }
        return undefined;
    }
    validatePlatformName(platform) {
        if (!platform) {
            this.$errors.failWithHelp("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* () {
            const uniqueFilePath = yield this.$tempService.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;
yok_1.injector.register("mobileHelper", MobileHelper);
//# sourceMappingURL=mobile-helper.js.map