"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const packageInfo = require("../../package.json");
const console_1 = require("../utils/console");
const fs = require("fs");
function createUUID() {
    return (Math.random().toString(36).slice(2, 10) +
        Math.random().toString(36).slice(2, 10));
}
function stringToBase64(input) {
    return Buffer.from(input).toString("base64");
}
const TelemetryManager = {
    version: packageInfo.version,
    socket: null,
    configPath: "",
    start: function (path, socket) {
        this.configPath = path;
        this.socket = socket;
    },
    registerStepEvent: function (stepPosition, event, data) {
        var _a;
        console_1.default.debug(`Registering Telemetry Event ${event}`);
        (_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit("telemetry_event", "ready", [`Registering telemetry event ${event}`], [], [], {
            stepPosition,
            event,
            data,
        });
    },
    retrieve: function () {
        return new Promise((resolve, reject) => {
            fs.readFile(`${this.configPath}/telemetry.json`, "utf8", (err, data) => {
                if (err) {
                    if (err.code === "ENOENT") {
                        // File does not exist, resolve with undefined
                        resolve(null);
                    }
                    else {
                        reject(err);
                    }
                }
                else {
                    try {
                        resolve(JSON.parse(data));
                    }
                    catch (_a) {
                        resolve(null);
                    }
                }
            });
        });
    },
    save: function (telemetry) {
        fs.writeFile(`${this.configPath}/telemetry.json`, JSON.stringify(telemetry), (err) => {
            if (err)
                throw err;
        });
    },
};
exports.default = TelemetryManager;
