"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "OctoprintRoutes", {
    enumerable: true,
    get: function() {
        return OctoprintRoutes;
    }
});
const _octoprintserviceconstants = require("./constants/octoprint-service.constants");
const _apiutils = require("./utils/api.utils");
const _serversettingsconstants = require("../../constants/server-settings.constants");
const _normalizeurl = require("../../utils/normalize-url");
class OctoprintRoutes {
    octoPrintBase = "/";
    apiBase = `${this.octoPrintBase}api`;
    apiVersion = `${this.apiBase}/version`;
    apiServer = `${this.apiBase}/server`;
    apiCurrentUser = `${this.apiBase}/currentuser`;
    apiSettingsPart = `${this.apiBase}/settings`;
    apiFiles = `${this.apiBase}/files`;
    apiFilesLocal = `${this.apiFiles}/local`;
    apiConnection = `${this.apiBase}/connection`;
    apiJob = `${this.apiBase}/job`;
    apiPrinter = `${this.apiBase}/printer`;
    apiPrinterHead = `${this.apiBase}/printer/printhead`;
    apiPrinterBed = `${this.apiPrinter}/bed`;
    apiPrinterCustomCommand = `${this.apiPrinter}/command`;
    apiPrinterProfiles = `${this.apiBase}/printerprofiles`;
    apiSystem = `${this.apiBase}/system`;
    apiSystemInfo = `${this.apiSystem}/info`;
    apiSystemCommands = `${this.apiSystem}/commands`;
    apiServerRestartCommand = `${this.apiSystemCommands}/core/restart`;
    apiUsers = `${this.apiBase}/users`;
    apiLogin = `${this.apiBase}/login?passive=true`;
    apiPluginPiSupport = `${this.apiBase}/plugin/pi_support`;
    apiProfiles = `${this.apiBase}/plugin/printerprofiles`;
    apiTimelapse = `${this.apiBase}/timelapse`;
    apiPlugin = `${this.apiBase}/plugin`;
    apiPluginManager = `${this.apiPlugin}/pluginmanager`;
    pluginsBase = `${this.octoPrintBase}plugin`;
    pluginSoftwareUpdate = `${this.pluginsBase}/softwareupdate`;
    pluginSoftwareUpdateCheck = `${this.pluginSoftwareUpdate}/check`;
    pluginSoftwareUpdateUpdate = `${this.pluginSoftwareUpdate}/update`;
    pluginFirmwareUpdater = `${this.pluginsBase}/firmwareupdater`;
    pluginFirmwareUpdaterStatus = `${this.pluginsBase}/firmwareupdater/status`;
    pluginFirmwareUpdaterFlash = `${this.pluginsBase}/firmwareupdater/flash`;
    pluginBackupIndex = `${this.pluginsBase}/backup`;
    pluginBackupEndpoint = `${this.pluginsBase}/backup/backup`;
    pluginBackupFile = (filename)=>`${this.pluginsBase}/backup/backup/${filename}`;
    pluginBackupFileDownload = (filename)=>`${this.pluginsBase}/backup/download/${filename}`;
    pluginBackupFileRestore = `${this.pluginsBase}/backup/restore`;
    pluginManager = `${this.pluginsBase}/pluginmanager`;
    pluginManagerPlugins = `${this.pluginManager}/plugins`;
    pluginManagerExport = `${this.pluginManager}/export`;
    pluginManagerOrphans = `${this.pluginManager}/orphans`;
    settingsStore;
    timeouts;
    constructor({ settingsStore }){
        this.settingsStore = settingsStore;
    }
    get disconnectCommand() {
        return {
            command: "disconnect"
        };
    }
    get cancelJobCommand() {
        return {
            command: "cancel"
        };
    }
    get pauseJobCommand() {
        return {
            command: "pause",
            action: "pause"
        };
    }
    get resumeJobCommand() {
        return {
            command: "pause",
            action: "resume"
        };
    }
    get connectCommand() {
        return {
            command: "connect"
        };
    }
    getBedTargetCommand(targetTemperature) {
        return {
            command: "target",
            target: targetTemperature
        };
    }
    pluginManagerPlugin = (pluginName)=>`${this.pluginManager}/${pluginName}`;
    pluginManagerRepository = (refresh = false)=>`${this.pluginManager}/repository?refresh=${refresh}`;
    apiPrinterCurrent = (history, limit, exclude)=>{
        exclude = exclude?.filter((e)=>!!e.length);
        const excludeParam = exclude?.length ? `&exclude=${exclude?.join(",")}` : "";
        const limitParam = !!limit ? `&limit=${limit}` : "";
        return `${this.apiPrinter}?history=${!!history}${limitParam}${excludeParam}`;
    };
    apiFile = (path)=>`${this.apiFilesLocal}/${path}`;
    downloadFileLocal = (path)=>`${this.octoPrintBase}downloads/files/local/${path}`;
    apiGetFiles = (recursive = false)=>`${this.apiFiles}/local?recursive=${recursive}`;
    apiSoftwareUpdateCheck = (force)=>`${this.octoPrintBase}plugin/softwareupdate/check${force ? "?force=true" : ""}`;
    selectCommand(print = false) {
        return {
            command: "select",
            print
        };
    }
    moveFileCommand(destination) {
        return {
            command: "move",
            destination
        };
    }
    printerNameSetting(name) {
        return {
            appearance: {
                name: name
            }
        };
    }
    gcodeAnalysisSetting(enabled) {
        return {
            gcodeAnalysis: {
                runAt: enabled ? "idle" : "never"
            }
        };
    }
    pluginFirmwareUpdaterSettings(subsettings) {
        return {
            plugins: {
                firmwareupdater: subsettings
            }
        };
    }
    pluginManagerCommand(command, url) {
        return {
            command,
            url
        };
    }
    ensureTimeoutSettingsLoaded() {
        const serverSettings = this.settingsStore.getSettings();
        this.timeouts = {
            ...serverSettings[_serversettingsconstants.timeoutSettingKey]
        };
        if (!this.timeouts) {
            throw new Error("OctoPrint API Service could not load timeout settings. settingsStore:Settings:timeout didnt return anything");
        }
    }
    prepareRequest(login, path, timeoutOverride, contentType = _octoprintserviceconstants.jsonContentType) {
        this.ensureTimeoutSettingsLoaded();
        const { apiKey, printerURL } = (0, _apiutils.validateLogin)(login);
        let headers = (0, _apiutils.constructHeaders)(apiKey, contentType);
        let timeout = timeoutOverride || this.timeouts.apiTimeout;
        if (timeout <= 0) {
            timeout = (0, _serversettingsconstants.getDefaultTimeout)().apiTimeout;
        }
        return {
            url: new URL(path, (0, _normalizeurl.normalizeUrl)(printerURL)).href,
            options: {
                headers,
                timeout
            }
        };
    }
    prepareAnonymousRequest(path, timeoutOverride, contentType = _octoprintserviceconstants.jsonContentType) {
        this.ensureTimeoutSettingsLoaded();
        let headers = {
            [_octoprintserviceconstants.contentTypeHeaderKey]: contentType
        };
        let timeout = timeoutOverride || this.timeouts.apiTimeout;
        if (timeout <= 0) {
            timeout = (0, _serversettingsconstants.getDefaultTimeout)().apiTimeout;
        }
        return {
            url: path,
            options: {
                headers,
                timeout
            }
        };
    }
    prepareJsonRequest(login, path, data, timeoutOverride) {
        const { url, options } = this.prepareRequest(login, path, timeoutOverride);
        let serializedData = data ? JSON.stringify(data) : undefined;
        options.headers[_octoprintserviceconstants.contentTypeHeaderKey] = _octoprintserviceconstants.jsonContentType;
        return {
            url,
            data: serializedData,
            options
        };
    }
}

//# sourceMappingURL=octoprint-api.routes.js.map