"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const kyber_1 = require("@dedis/kyber");
const moment_1 = tslib_1.__importDefault(require("moment"));
const light_1 = require("protobufjs/light");
const protobuf_1 = require("../protobuf");
class PopPartyStruct extends light_1.Message {
    constructor(props) {
        super(props);
        this.finalizations = this.finalizations || [];
        this.miners = this.miners || [];
        this.previous = Buffer.from(this.previous || protobuf_1.EMPTY_BUFFER);
        this.next = Buffer.from(this.next || protobuf_1.EMPTY_BUFFER);
        /* Protobuf aliases */
        Object.defineProperty(this, "miningreward", {
            get() {
                return this.miningReward;
            },
            set(value) {
                this.miningReward = value;
            },
        });
    }
    /**
     * @see README#Message classes
     */
    static register() {
        protobuf_1.registerMessage("personhood.PopPartyStruct", PopPartyStruct, PopDesc, Attendees, LRSTag);
    }
    /**
     * Replace the current attendees by the new ones and sort them, so that different
     * organizers scanning in a different order get the same result.
     *
     * @param publics Public keys of the new attendees
     */
    updateAttendes(publics) {
        const keys = publics.map((p) => p.toProto());
        keys.sort((a, b) => Buffer.compare(a, b));
        this.attendees.keys.splice(0, this.attendees.keys.length, ...keys);
    }
}
exports.PopPartyStruct = PopPartyStruct;
class FinalStatement extends light_1.Message {
    /**
     * @see README#Message classes
     */
    static register() {
        protobuf_1.registerMessage("personhood.FinalStatement", FinalStatement, PopDesc, Attendees);
    }
}
exports.FinalStatement = FinalStatement;
class PopDesc extends light_1.Message {
    constructor(props) {
        super(props);
    }
    /**
     * Getter for the timestamp
     * @returns the timestamp as a number
     */
    get timestamp() {
        return this.datetime.toNumber();
    }
    /**
     * Format the timestamp into a human readable string
     * @returns a string of the time
     */
    get dateString() {
        return new Date(this.timestamp).toString().replace(/ GMT.*/, "");
    }
    /**
     * Format the timestamp to a unique string
     * @returns the string
     */
    get uniqueName() {
        const d = new Date(this.timestamp);
        return moment_1.default(d).format("YY-MM-DD HH:mm");
    }
    /**
     * @see README#Message classes
     */
    static register() {
        protobuf_1.registerMessage("personhood.PopDesc", PopDesc);
    }
    /**
     * Helper to encode the statement using protobuf
     * @returns the bytes
     */
    toBytes() {
        return Buffer.from(PopDesc.encode(this).finish());
    }
}
exports.PopDesc = PopDesc;
class Attendees extends light_1.Message {
    constructor(properties) {
        super(properties);
        this.keys = this.keys.slice() || [];
    }
    /**
     * Get the keys as kyber points
     * @returns a list of points
     */
    get publics() {
        return this.keys.map((k) => kyber_1.PointFactory.fromProto(k));
    }
    /**
     * @see README#Message classes
     */
    static register() {
        protobuf_1.registerMessage("personhood.Attendees", Attendees);
    }
    /**
     * Helper to encode the attendees using protobuf
     * @returns the bytes
     */
    toBytes() {
        return Buffer.from(Attendees.encode(this).finish());
    }
}
exports.Attendees = Attendees;
class LRSTag extends light_1.Message {
    constructor(props) {
        super(props);
        this.tag = Buffer.from(this.tag || protobuf_1.EMPTY_BUFFER);
    }
    /**
     * @see README#Message classes
     */
    static register() {
        protobuf_1.registerMessage("personhood.LRSTag", LRSTag);
    }
}
exports.LRSTag = LRSTag;
PopPartyStruct.register();
FinalStatement.register();
PopDesc.register();
Attendees.register();
LRSTag.register();
