"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BuildInfoFileService = void 0;
const path = require("path");
const helpers = require("../common/helpers");
const yok_1 = require("../common/yok");
const buildInfoFileName = ".nsbuildinfo";
class BuildInfoFileService {
    constructor($devicePathProvider, $fs, $mobileHelper, $projectChangesService) {
        this.$devicePathProvider = $devicePathProvider;
        this.$fs = $fs;
        this.$mobileHelper = $mobileHelper;
        this.$projectChangesService = $projectChangesService;
    }
    getLocalBuildInfo(platformData, buildData) {
        const outputPath = buildData.outputPath || platformData.getBuildOutputPath(buildData);
        const buildInfoFile = path.join(outputPath, buildInfoFileName);
        if (this.$fs.exists(buildInfoFile)) {
            try {
                const buildInfo = this.$fs.readJson(buildInfoFile);
                return buildInfo;
            }
            catch (e) {
                return null;
            }
        }
        return null;
    }
    async getDeviceBuildInfo(device, projectData) {
        const deviceFilePath = await this.getDeviceBuildInfoFilePath(device, projectData);
        try {
            const deviceFileContent = await this.$mobileHelper.getDeviceFileContent(device, deviceFilePath, projectData);
            return JSON.parse(deviceFileContent);
        }
        catch (e) {
            return null;
        }
    }
    saveLocalBuildInfo(platformData, buildInfoFileDirname) {
        const buildInfoFile = path.join(buildInfoFileDirname, buildInfoFileName);
        const prepareInfo = this.$projectChangesService.getPrepareInfo(platformData);
        const buildInfo = {
            prepareTime: prepareInfo.changesRequireBuildTime,
            buildTime: new Date().toString(),
        };
        this.$fs.writeJson(buildInfoFile, buildInfo);
    }
    async saveDeviceBuildInfo(device, projectData, outputFilePath) {
        const deviceFilePath = await this.getDeviceBuildInfoFilePath(device, projectData);
        const appIdentifier = projectData.projectIdentifiers[device.deviceInfo.platform.toLowerCase()];
        await device.fileSystem.putFile(path.join(outputFilePath, buildInfoFileName), deviceFilePath, appIdentifier);
    }
    async getDeviceBuildInfoFilePath(device, projectData) {
        const platform = device.deviceInfo.platform.toLowerCase();
        const deviceRootPath = await this.$devicePathProvider.getDeviceProjectRootPath(device, {
            appIdentifier: projectData.projectIdentifiers[platform],
            getDirname: true,
        });
        const result = helpers.fromWindowsRelativePathToUnix(path.join(deviceRootPath, buildInfoFileName));
        return result;
    }
}
exports.BuildInfoFileService = BuildInfoFileService;
yok_1.injector.register("buildInfoFileService", BuildInfoFileService);
