"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 path = require("path");
const semver = require("semver");
const util = require("util");
const preview_app_constants_1 = require("./preview-app-constants");
class PreviewAppPluginsService {
    constructor($fs, $logger, $projectData) {
        this.$fs = $fs;
        this.$logger = $logger;
        this.$projectData = $projectData;
    }
    comparePluginsOnDevice(device) {
        return __awaiter(this, void 0, void 0, function* () {
            const devicePlugins = this.getDevicePlugins(device);
            const localPlugins = this.getLocalPlugins();
            _.keys(localPlugins).forEach(localPlugin => {
                const localPluginVersion = localPlugins[localPlugin];
                const devicePluginVersion = devicePlugins[localPlugin];
                this.$logger.trace(`Comparing plugin ${localPlugin} with localPluginVersion ${localPluginVersion} and devicePluginVersion ${devicePluginVersion}`);
                if (devicePluginVersion) {
                    const localPluginVersionData = semver.coerce(localPluginVersion);
                    const devicePluginVersionData = semver.coerce(devicePluginVersion);
                    if (localPluginVersionData.major !== devicePluginVersionData.major) {
                        this.$logger.warn(util.format(preview_app_constants_1.PluginComparisonMessages.LOCAL_PLUGIN_WITH_DIFFERENCE_IN_MAJOR_VERSION, localPlugin, localPluginVersion, devicePluginVersion));
                    }
                    if (localPluginVersionData.major === devicePluginVersionData.major && localPluginVersionData.minor > devicePluginVersionData.minor) {
                        this.$logger.warn(util.format(preview_app_constants_1.PluginComparisonMessages.LOCAL_PLUGIN_WITH_GREATHER_MINOR_VERSION, localPlugin, localPluginVersion, devicePluginVersion));
                    }
                }
                else {
                    this.$logger.warn(util.format(preview_app_constants_1.PluginComparisonMessages.PLUGIN_NOT_INCLUDED_IN_PREVIEW_APP, localPlugin, device.id));
                }
            });
        });
    }
    getDevicePlugins(device) {
        try {
            return JSON.parse(device.plugins);
        }
        catch (err) {
            this.$logger.trace(`Error while parsing plugins from device ${device.id}. Error is ${err.message}`);
            return {};
        }
    }
    getLocalPlugins() {
        const projectFilePath = path.join(this.$projectData.projectDir, "package.json");
        try {
            return this.$fs.readJson(projectFilePath).dependencies;
        }
        catch (err) {
            this.$logger.trace(`Error while parsing ${projectFilePath}. Error is ${err.message}`);
            return {};
        }
    }
}
exports.PreviewAppPluginsService = PreviewAppPluginsService;
$injector.register("previewAppPluginsService", PreviewAppPluginsService);
