package peergos.shared.messaging;

import peergos.shared.cbor.*;
import peergos.shared.io.ipfs.multihash.*;

import java.util.*;

public class MessageRef implements Cborable {

    public final Multihash envelopeHash;

    public MessageRef(Multihash envelopeHash) {
        this.envelopeHash = envelopeHash;
    }

    @Override
    public CborObject toCbor() {
        Map<String, Cborable> result = new TreeMap<>();
        result.put("h", new CborObject.CborByteArray(envelopeHash.toBytes()));
        return CborObject.CborMap.build(result);
    }

    public static MessageRef fromCbor(Cborable cbor) {
        if (! (cbor instanceof CborObject.CborMap))
            throw new IllegalStateException("Incorrect cbor: " + cbor);
        CborObject.CborMap m = (CborObject.CborMap) cbor;
        Multihash h = Multihash.decode(m.getByteArray("h"));
        return new MessageRef(h);
    }

    @Override
    public String toString() {
        return envelopeHash.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MessageRef that = (MessageRef) o;
        return Objects.equals(envelopeHash, that.envelopeHash);
    }

    @Override
    public int hashCode() {
        return Objects.hash(envelopeHash);
    }
}

@@ -28,16 +28,53 @@
 aging;%0A%0A
+import jsinterop.annotations.JsType;%0A
 import p
@@ -159,16 +159,23 @@
 util.*;%0A
+@JsType
 %0Apublic 

add JSType to MessageRef