"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogStore = void 0;
const threads_core_1 = require("@textile/threads-core");
const keybook_1 = require("./keybook");
/**
 * LogStore is an internal store of log information.
 * Currently, it simply wraps a Key Store for managing local Thread and Log keys.
 */
class LogStore {
    constructor(keys) {
        this.keys = keys;
    }
    /**
     * Create a new LogStore from an existing datastore instance.
     * @param store
     */
    static fromDatastore(store) {
        return new LogStore(new keybook_1.KeyBook(store));
    }
    /**
     * Close the underlying key store.
     */
    close() {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.keys.close();
            return;
        });
    }
    /**
     * Threads returns all threads in the store.
     */
    threads() {
        return __awaiter(this, void 0, void 0, function* () {
            return this.keys.threads();
        });
    }
    /**
     * Logs returns all logs under the given thread.
     * @param id Thread ID.
     */
    logs(id) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.keys.logs(id);
        });
    }
    /**
     * AddThread adds a thread with keys.
     * @param info Thread information.
     */
    addThread(info) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            if (((_a = info.key) === null || _a === void 0 ? void 0 : _a.service) === undefined) {
                throw new Error('Network key required');
            }
            yield this.keys.addServiceKey(info.id, info.key.service);
            info.key.read && (yield this.keys.addReadKey(info.id, info.key.read));
        });
    }
    /**
     * deleteThread removes a thread from the store.
     * @param id Thread ID.
     */
    deleteThread(id) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.keys.clearThreadKeys(id);
        });
    }
    /**
     * threadInfo returns info about a thread.
     * @param id Thread ID.
     */
    threadInfo(id) {
        return __awaiter(this, void 0, void 0, function* () {
            const service = yield this.keys.serviceKey(id);
            if (service === undefined)
                throw new Error('Missing network key');
            const read = yield this.keys.readKey(id);
            const key = new threads_core_1.ThreadKey(service, read);
            const info = {
                id,
                key,
            };
            return info;
        });
    }
    /**
     * addLog adds a log to a thread.
     * @param id Thread ID.
     * @param info Log information.
     */
    addLog(id, info) {
        return __awaiter(this, void 0, void 0, function* () {
            if (info.pubKey)
                yield this.keys.addPubKey(id, info.id, info.pubKey);
            if (info.privKey)
                yield this.keys.addPrivKey(id, info.id, info.privKey);
            return;
        });
    }
    /**
     * deleteLog removes a log under the given thread.
     * @param id Thread ID.
     * @param log Log ID.
     */
    deleteLog(id, log) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.keys.clearLogKeys(id, log);
        });
    }
    /**
     * logInfo returns info about a given log.
     * @param id The Thread ID.
     * @param log The Log ID.
     */
    logInfo(id, log) {
        return __awaiter(this, void 0, void 0, function* () {
            const privKey = yield this.keys.privKey(id, log);
            const pubKey = (yield this.keys.pubKey(id, log)) || (privKey === null || privKey === void 0 ? void 0 : privKey.public);
            const info = {
                id: log,
                privKey,
                pubKey,
            };
            return info;
        });
    }
}
exports.LogStore = LogStore;
//# sourceMappingURL=index.js.map