"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const electron_1 = require("electron");
const Util_1 = require("../Util");
const LogCommon_1 = require("./LogCommon");
/** API for the log used by the main process */
class LogMainApi {
    constructor(sendToRenderer) {
        this.entries = [];
        this.onAddEntry = (event, entry, msgId) => {
            this.addEntry(entry);
            // Reply if it's a tracked message
            if (msgId !== undefined) {
                event.sender.send(LogCommon_1.LogChannel.addEntryReply, msgId);
            }
        };
        this.onRefreshEntries = (event, start) => {
            event.sender.send(LogCommon_1.LogChannel.refreshEntriesReply, start, this.entries.slice(start));
        };
        this.onRemoveEntries = (event, first, last) => {
            // Remove the entries
            this.entries.splice(first, last);
            // Respond
            event.sender.send(LogCommon_1.LogChannel.removeEntriesReply, first, last);
        };
        this.sendToRenderer = sendToRenderer;
    }
    bindListeners() {
        electron_1.ipcMain.on(LogCommon_1.LogChannel.addEntry, this.onAddEntry);
        electron_1.ipcMain.on(LogCommon_1.LogChannel.refreshEntries, this.onRefreshEntries);
        electron_1.ipcMain.on(LogCommon_1.LogChannel.removeEntries, this.onRemoveEntries);
    }
    unbindListeners() {
        electron_1.ipcMain.removeListener(LogCommon_1.LogChannel.addEntry, this.onAddEntry);
        electron_1.ipcMain.removeListener(LogCommon_1.LogChannel.refreshEntries, this.onRefreshEntries);
        electron_1.ipcMain.removeListener(LogCommon_1.LogChannel.removeEntries, this.onRemoveEntries);
    }
    addEntry(preEntry) {
        // Format entry
        const entry = Object.assign(Util_1.deepCopy(preEntry), { timestamp: Date.now() });
        // Add entry
        this.entries.push(entry);
        // Send the entry to the renderer
        this.sendToRenderer(LogCommon_1.LogChannel.refreshEntriesReply, this.entries.length - 1, [entry]);
    }
}
exports.LogMainApi = LogMainApi;
//# sourceMappingURL=LogMainApi.js.map