"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "PluginBaseService", {
    enumerable: true,
    get: function() {
        return PluginBaseService;
    }
});
const _runtimeexceptions = require("../../exceptions/runtime.exceptions");
const _octoprintserviceconstants = require("./constants/octoprint-service.constants");
class PluginBaseService {
    octoprintClient;
    pluginRepositoryCache;
    _pluginName;
    pluginUrl;
    logger;
    constructor({ octoprintClient, pluginRepositoryCache, loggerFactory }, { pluginName, pluginUrl }){
        this.octoprintClient = octoprintClient;
        this.pluginRepositoryCache = pluginRepositoryCache;
        this._pluginName = pluginName;
        this.logger = loggerFactory(`Plugin-${pluginName}`);
        const pluginReference = pluginRepositoryCache.getPlugin(pluginName);
        this.pluginUrl = pluginUrl || pluginReference.archive;
        if (!pluginName || !pluginUrl) {
            throw new _runtimeexceptions.ValidationException("OctoPrint Plugin not configured correctly");
        }
    }
    get pluginName() {
        return this._pluginName;
    }
    async isPluginUpToDate(printerLogin) {
        const response = await this.#queryInstalledPlugin(printerLogin);
        const result = this.pluginRepositoryCache.getPlugin(this.pluginName);
        return result?.github?.latest_release.name === response.version;
    }
    async #queryInstalledPlugin(printerLogin) {
        const response = await this.octoprintClient.getPluginManagerPlugin(printerLogin, this.pluginName);
        if (!response) {
            throw new _runtimeexceptions.InternalServerException("Plugin query response was empty");
        }
        return response.plugin;
    }
    async isPluginInstalled(printerLogin) {
        const foundPlugin = await this.findPluginFromListQuery(printerLogin);
        return !!foundPlugin;
    }
    async findPluginFromListQuery(printerLogin) {
        const response = await this.octoprintClient.getPluginManagerPlugins(printerLogin);
        if (!response?.plugins?.length) {
            throw new _runtimeexceptions.InternalServerException("Plugin query response was empty");
        }
        return this.findPluginInList(response.plugins);
    }
    findPluginInList(pluginList) {
        return pluginList.find((p)=>p.key.toLowerCase() === this.pluginName.toLowerCase());
    }
    async updatePlugin(login) {
        return await this.octoprintClient.postSoftwareUpdate(login, [
            this.pluginName
        ], this.pluginUrl);
    }
    async installPlugin(login, restartAfter = false) {
        const command = _octoprintserviceconstants.pluginManagerCommands.install.name;
        const plugin = this.pluginRepositoryCache.getPlugin(this.pluginName);
        await this.octoprintClient.postApiPluginManagerCommand(login, command, plugin.archive);
        await this.#conditionalRestartCommand(login, restartAfter);
    }
    async uninstallPlugin(login, restartAfter = false) {
        const command = _octoprintserviceconstants.pluginManagerCommands.uninstall.name;
        await this.octoprintClient.postApiPluginManagerCommand(login, command, this.pluginName);
        await this.#conditionalRestartCommand(login, restartAfter);
    }
    async enablePlugin(login, restartAfter = false) {
        const command = _octoprintserviceconstants.pluginManagerCommands.enable.name;
        await this.octoprintClient.postApiPluginManagerCommand(login, command, this.pluginName);
        await this.#conditionalRestartCommand(login, restartAfter);
    }
    async disablePlugin(login, restartAfter = false) {
        const command = _octoprintserviceconstants.pluginManagerCommands.disable.name;
        await this.octoprintClient.postApiPluginManagerCommand(login, command, this.pluginName);
        await this.#conditionalRestartCommand(login, restartAfter);
    }
    async cleanupPlugin(login, restartAfter = false) {
        const command = _octoprintserviceconstants.pluginManagerCommands.cleanup.name;
        await this.octoprintClient.postApiPluginManagerCommand(login, command, this.pluginName);
        await this.#conditionalRestartCommand(login, restartAfter);
    }
    async cleanupAllPlugins(login, restartAfter = false) {
        const command = _octoprintserviceconstants.pluginManagerCommands.cleanup_all.name;
        await this.octoprintClient.postApiPluginManagerCommand(login, command);
        await this.#conditionalRestartCommand(login, restartAfter);
    }
    async #conditionalRestartCommand(printerLogin, restartAfter = false) {
        if (!restartAfter) return;
        await this.octoprintClient.postServerRestartCommand(printerLogin);
    }
}

//# sourceMappingURL=plugin-base.service.js.map