"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.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;
    }
    getDeviceBuildInfo(device, projectData) {
        return __awaiter(this, void 0, void 0, function* () {
            const deviceFilePath = yield this.getDeviceBuildInfoFilePath(device, projectData);
            try {
                const deviceFileContent = yield 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);
    }
    saveDeviceBuildInfo(device, projectData, outputFilePath) {
        return __awaiter(this, void 0, void 0, function* () {
            const deviceFilePath = yield this.getDeviceBuildInfoFilePath(device, projectData);
            const appIdentifier = projectData.projectIdentifiers[device.deviceInfo.platform.toLowerCase()];
            yield device.fileSystem.putFile(path.join(outputFilePath, buildInfoFileName), deviceFilePath, appIdentifier);
        });
    }
    getDeviceBuildInfoFilePath(device, projectData) {
        return __awaiter(this, void 0, void 0, function* () {
            const platform = device.deviceInfo.platform.toLowerCase();
            const deviceRootPath = yield 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);
//# sourceMappingURL=build-info-file-service.js.map