"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "MoonrakerApi", {
    enumerable: true,
    get: function() {
        return MoonrakerApi;
    }
});
const _printerapiinterface = require("./printer-api.interface");
const _runtimeexceptions = require("../exceptions/runtime.exceptions");
class MoonrakerApi {
    logger;
    client;
    printerLogin;
    constructor({ moonrakerClient, printerLogin, loggerFactory }){
        this.logger = loggerFactory(MoonrakerApi.name);
        this.client = moonrakerClient;
        this.printerLogin = printerLogin;
    }
    get type() {
        return _printerapiinterface.MoonrakerType;
    }
    set login(login) {
        this.printerLogin = login;
    }
    get login() {
        return this.printerLogin;
    }
    async getVersion() {
        const result = await this.client.getApiVersion(this.login);
        return result.data?.server;
    }
    async connect() {
        throw new _runtimeexceptions.NotImplementedException();
    }
    async disconnect() {
        throw new _runtimeexceptions.NotImplementedException();
    }
    async restartServer() {
        await this.client.postRestartServer(this.login);
    }
    async restartHost() {
        await this.client.postHostRestart(this.login);
    }
    async restartPrinterFirmware() {
        await this.client.postFirmwareRestart(this.login);
    }
    async startPrint(filename) {
        await this.client.postPrintStart(this.login, filename);
    }
    async pausePrint() {
        await this.client.postPrintPause(this.login);
    }
    async resumePrint() {
        await this.client.postPrintResume(this.login);
    }
    async cancelPrint() {
        await this.client.postPrintCancel(this.login);
    }
    async sendGcode(script) {
        await this.client.postGcodeScript(this.login, script);
    }
    async quickStop() {
        await this.client.postQuickStop(this.login);
    }
    async movePrintHead(amounts) {
        const setSpeed = !!amounts.speed ? amounts.speed : 10;
        let g1CommandAxes = "";
        if (!Number.isNaN(amounts.x)) {
            g1CommandAxes += ` X${amounts.x}`;
        }
        if (!Number.isNaN(amounts.y)) {
            g1CommandAxes += ` Y${amounts.y}`;
        }
        if (!Number.isNaN(amounts.z)) {
            g1CommandAxes += ` Z${amounts.z}`;
        }
        await this.client.postGcodeScript(this.login, `
      G91
      G1 ${g1CommandAxes} F${setSpeed}
      G90`);
    }
    async getFiles() {
        const files = await this.client.getServerFilesList(this.login);
        return files.data.result.map((f)=>({
                size: f.size,
                path: f.path,
                date: f.modified
            }));
    }
    async homeAxes(axes) {
        let homeCommand = `G28`;
        if (!!axes.x) {
            homeCommand += ` X`;
        }
        if (!!axes.y) {
            homeCommand += ` Y`;
        }
        if (!!axes.z) {
            homeCommand += ` Z`;
        }
        await this.client.postGcodeScript(this.login, homeCommand);
    }
    async downloadFile(path) {
        return await this.client.getServerFilesDownload(this.login, "gcodes", path);
    }
    async uploadFile(fileOrBuffer, uploadToken) {
        await this.client.postServerFileUpload(this.login, fileOrBuffer, uploadToken);
    }
    async deleteFile(path) {
        await this.client.deleteServerFile(this.login, "gcodes", path);
    }
    async deleteFolder(path) {
        await this.client.deleteServerFilesDirectory(this.login, path, false);
    }
    async getSettings() {
        const result = await this.client.getServerConfig(this.login);
        return result.data?.result;
    }
    async getReprintState() {
        const response = await this.client.getPrinterObjectsQuery(this.login, {
            print_stats: [],
            webhooks: []
        });
        const result = response.data.result;
        const operational = result.status.webhooks.state === "ready";
        const response2 = await this.client.getServerHistoryList(this.login, 5, 0);
        const jobs = (response2.data?.result.jobs ?? []).sort((a, b)=>a.start_time > b.start_time ? 1 : 0);
        if (jobs.length === 0 || !operational) {
            return {
                connectionState: operational ? "Operational" : "Error",
                reprintState: _printerapiinterface.ReprintState.NoLastPrint
            };
        }
        const job = jobs[0];
        return {
            connectionState: "Operational",
            reprintState: _printerapiinterface.ReprintState.LastPrintReady,
            file: {
                date: job.start_time,
                path: job.filename,
                size: job.metadata.size
            }
        };
    }
}

//# sourceMappingURL=moonraker.api.js.map