"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeviceInstallAppService = void 0;
const constants_1 = require("../../constants");
const path = require("path");
const yok_1 = require("../../common/yok");
class DeviceInstallAppService {
    constructor($analyticsService, $buildArtifactsService, $buildInfoFileService, $fs, $logger, $mobileHelper, $projectDataService, $platformsDataService) {
        this.$analyticsService = $analyticsService;
        this.$buildArtifactsService = $buildArtifactsService;
        this.$buildInfoFileService = $buildInfoFileService;
        this.$fs = $fs;
        this.$logger = $logger;
        this.$mobileHelper = $mobileHelper;
        this.$projectDataService = $projectDataService;
        this.$platformsDataService = $platformsDataService;
    }
    async installOnDevice(device, buildData, packageFile) {
        this.$logger.info(`Installing on device ${device.deviceInfo.identifier}...`);
        const platform = device.deviceInfo.platform.toLowerCase();
        const projectData = this.$projectDataService.getProjectData(buildData.projectDir);
        const platformData = this.$platformsDataService.getPlatformData(platform, projectData);
        await this.$analyticsService.trackEventActionInGoogleAnalytics({
            action: "Deploy",
            device,
            projectDir: projectData.projectDir,
        });
        if (!packageFile) {
            packageFile = await this.$buildArtifactsService.getLatestAppPackagePath(platformData, buildData);
        }
        await platformData.platformProjectService.cleanDeviceTempFolder(device.deviceInfo.identifier, projectData);
        const appIdentifier = projectData.projectIdentifiers[platform];
        const outputFilePath = buildData.outputPath || platformData.getBuildOutputPath(buildData);
        await device.applicationManager.reinstallApplication(appIdentifier, packageFile, buildData);
        await this.updateHashesOnDevice({
            device,
            appIdentifier,
            outputFilePath,
            platformData,
        });
        if (!buildData.release) {
            await this.$buildInfoFileService.saveDeviceBuildInfo(device, projectData, outputFilePath);
        }
        this.$logger.info(`Successfully installed on device with identifier '${device.deviceInfo.identifier}'.`);
    }
    async installOnDeviceIfNeeded(device, buildData, packageFile) {
        const shouldInstall = await this.shouldInstall(device, buildData);
        if (shouldInstall) {
            await this.installOnDevice(device, buildData, packageFile);
        }
    }
    async shouldInstall(device, buildData) {
        const projectData = this.$projectDataService.getProjectData(buildData.projectDir);
        const platformData = this.$platformsDataService.getPlatformData(device.deviceInfo.platform, projectData);
        const platform = device.deviceInfo.platform;
        if (!(await device.applicationManager.isApplicationInstalled(projectData.projectIdentifiers[platform.toLowerCase()]))) {
            return true;
        }
        const deviceBuildInfo = await this.$buildInfoFileService.getDeviceBuildInfo(device, projectData);
        const localBuildInfo = this.$buildInfoFileService.getLocalBuildInfo(platformData, { ...buildData, buildForDevice: !device.isEmulator });
        return (!localBuildInfo ||
            !deviceBuildInfo ||
            deviceBuildInfo.buildTime !== localBuildInfo.buildTime);
    }
    async updateHashesOnDevice(data) {
        const { device, appIdentifier, platformData, outputFilePath } = data;
        if (!this.$mobileHelper.isAndroidPlatform(platformData.normalizedPlatformName)) {
            return;
        }
        let hashes = {};
        const hashesFilePath = path.join(outputFilePath, constants_1.HASHES_FILE_NAME);
        if (this.$fs.exists(hashesFilePath)) {
            hashes = this.$fs.readJson(hashesFilePath);
        }
        await device.fileSystem.updateHashesOnDevice(hashes, appIdentifier);
    }
}
exports.DeviceInstallAppService = DeviceInstallAppService;
yok_1.injector.register("deviceInstallAppService", DeviceInstallAppService);
