import { BinaryReader, BinaryWriter } from "../../../../binary";
import { bytesFromBase64, base64FromBytes, isSet } from "../../../../helpers";
import { GlobalDecoderRegistry } from "../../../../registry";
/** PacketStatus specifies the status of a RecvPacketResult. */
export var PacketStatus;
(function (PacketStatus) {
    /** PACKET_STATUS_UNSPECIFIED - PACKET_STATUS_UNSPECIFIED indicates an unknown packet status. */
    PacketStatus[PacketStatus["PACKET_STATUS_UNSPECIFIED"] = 0] = "PACKET_STATUS_UNSPECIFIED";
    /** PACKET_STATUS_SUCCESS - PACKET_STATUS_SUCCESS indicates a successful packet receipt. */
    PacketStatus[PacketStatus["PACKET_STATUS_SUCCESS"] = 1] = "PACKET_STATUS_SUCCESS";
    /** PACKET_STATUS_FAILURE - PACKET_STATUS_FAILURE indicates a failed packet receipt. */
    PacketStatus[PacketStatus["PACKET_STATUS_FAILURE"] = 2] = "PACKET_STATUS_FAILURE";
    /** PACKET_STATUS_ASYNC - PACKET_STATUS_ASYNC indicates an async packet receipt. */
    PacketStatus[PacketStatus["PACKET_STATUS_ASYNC"] = 3] = "PACKET_STATUS_ASYNC";
    PacketStatus[PacketStatus["UNRECOGNIZED"] = -1] = "UNRECOGNIZED";
})(PacketStatus || (PacketStatus = {}));
export const PacketStatusAmino = PacketStatus;
export function packetStatusFromJSON(object) {
    switch (object) {
        case 0:
        case "PACKET_STATUS_UNSPECIFIED":
            return PacketStatus.PACKET_STATUS_UNSPECIFIED;
        case 1:
        case "PACKET_STATUS_SUCCESS":
            return PacketStatus.PACKET_STATUS_SUCCESS;
        case 2:
        case "PACKET_STATUS_FAILURE":
            return PacketStatus.PACKET_STATUS_FAILURE;
        case 3:
        case "PACKET_STATUS_ASYNC":
            return PacketStatus.PACKET_STATUS_ASYNC;
        case -1:
        case "UNRECOGNIZED":
        default:
            return PacketStatus.UNRECOGNIZED;
    }
}
export function packetStatusToJSON(object) {
    switch (object) {
        case PacketStatus.PACKET_STATUS_UNSPECIFIED:
            return "PACKET_STATUS_UNSPECIFIED";
        case PacketStatus.PACKET_STATUS_SUCCESS:
            return "PACKET_STATUS_SUCCESS";
        case PacketStatus.PACKET_STATUS_FAILURE:
            return "PACKET_STATUS_FAILURE";
        case PacketStatus.PACKET_STATUS_ASYNC:
            return "PACKET_STATUS_ASYNC";
        case PacketStatus.UNRECOGNIZED:
        default:
            return "UNRECOGNIZED";
    }
}
function createBasePacket() {
    return {
        sequence: BigInt(0),
        sourceClient: "",
        destinationClient: "",
        timeoutTimestamp: BigInt(0),
        payloads: []
    };
}
/**
 * Packet defines a type that carries data across different chains through IBC
 * @name Packet
 * @package ibc.core.channel.v2
 * @see proto type: ibc.core.channel.v2.Packet
 */
export const Packet = {
    typeUrl: "/ibc.core.channel.v2.Packet",
    aminoType: "cosmos-sdk/Packet",
    is(o) {
        return o && (o.$typeUrl === Packet.typeUrl || typeof o.sequence === "bigint" && typeof o.sourceClient === "string" && typeof o.destinationClient === "string" && typeof o.timeoutTimestamp === "bigint" && Array.isArray(o.payloads) && (!o.payloads.length || Payload.is(o.payloads[0])));
    },
    isAmino(o) {
        return o && (o.$typeUrl === Packet.typeUrl || typeof o.sequence === "bigint" && typeof o.source_client === "string" && typeof o.destination_client === "string" && typeof o.timeout_timestamp === "bigint" && Array.isArray(o.payloads) && (!o.payloads.length || Payload.isAmino(o.payloads[0])));
    },
    encode(message, writer = BinaryWriter.create()) {
        if (message.sequence !== BigInt(0)) {
            writer.uint32(8).uint64(message.sequence);
        }
        if (message.sourceClient !== "") {
            writer.uint32(18).string(message.sourceClient);
        }
        if (message.destinationClient !== "") {
            writer.uint32(26).string(message.destinationClient);
        }
        if (message.timeoutTimestamp !== BigInt(0)) {
            writer.uint32(32).uint64(message.timeoutTimestamp);
        }
        for (const v of message.payloads) {
            Payload.encode(v, writer.uint32(42).fork()).ldelim();
        }
        return writer;
    },
    decode(input, length) {
        const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
        let end = length === undefined ? reader.len : reader.pos + length;
        const message = createBasePacket();
        while (reader.pos < end) {
            const tag = reader.uint32();
            switch (tag >>> 3) {
                case 1:
                    message.sequence = reader.uint64();
                    break;
                case 2:
                    message.sourceClient = reader.string();
                    break;
                case 3:
                    message.destinationClient = reader.string();
                    break;
                case 4:
                    message.timeoutTimestamp = reader.uint64();
                    break;
                case 5:
                    message.payloads.push(Payload.decode(reader, reader.uint32()));
                    break;
                default:
                    reader.skipType(tag & 7);
                    break;
            }
        }
        return message;
    },
    fromPartial(object) {
        const message = createBasePacket();
        message.sequence = object.sequence !== undefined && object.sequence !== null ? BigInt(object.sequence.toString()) : BigInt(0);
        message.sourceClient = object.sourceClient ?? "";
        message.destinationClient = object.destinationClient ?? "";
        message.timeoutTimestamp = object.timeoutTimestamp !== undefined && object.timeoutTimestamp !== null ? BigInt(object.timeoutTimestamp.toString()) : BigInt(0);
        message.payloads = object.payloads?.map(e => Payload.fromPartial(e)) || [];
        return message;
    },
    fromAmino(object) {
        const message = createBasePacket();
        if (object.sequence !== undefined && object.sequence !== null) {
            message.sequence = BigInt(object.sequence);
        }
        if (object.source_client !== undefined && object.source_client !== null) {
            message.sourceClient = object.source_client;
        }
        if (object.destination_client !== undefined && object.destination_client !== null) {
            message.destinationClient = object.destination_client;
        }
        if (object.timeout_timestamp !== undefined && object.timeout_timestamp !== null) {
            message.timeoutTimestamp = BigInt(object.timeout_timestamp);
        }
        message.payloads = object.payloads?.map(e => Payload.fromAmino(e)) || [];
        return message;
    },
    toAmino(message) {
        const obj = {};
        obj.sequence = message.sequence !== BigInt(0) ? message.sequence?.toString() : undefined;
        obj.source_client = message.sourceClient === "" ? undefined : message.sourceClient;
        obj.destination_client = message.destinationClient === "" ? undefined : message.destinationClient;
        obj.timeout_timestamp = message.timeoutTimestamp !== BigInt(0) ? message.timeoutTimestamp?.toString() : undefined;
        if (message.payloads) {
            obj.payloads = message.payloads.map(e => e ? Payload.toAmino(e) : undefined);
        }
        else {
            obj.payloads = message.payloads;
        }
        return obj;
    },
    fromAminoMsg(object) {
        return Packet.fromAmino(object.value);
    },
    toAminoMsg(message) {
        return {
            type: "cosmos-sdk/Packet",
            value: Packet.toAmino(message)
        };
    },
    fromProtoMsg(message) {
        return Packet.decode(message.value);
    },
    toProto(message) {
        return Packet.encode(message).finish();
    },
    toProtoMsg(message) {
        return {
            typeUrl: "/ibc.core.channel.v2.Packet",
            value: Packet.encode(message).finish()
        };
    },
    registerTypeUrl() {
        if (!GlobalDecoderRegistry.registerExistingTypeUrl(Packet.typeUrl)) {
            return;
        }
        Payload.registerTypeUrl();
    }
};
function createBasePayload() {
    return {
        sourcePort: "",
        destinationPort: "",
        version: "",
        encoding: "",
        value: new Uint8Array()
    };
}
/**
 * Payload contains the source and destination ports and payload for the application (version, encoding, raw bytes)
 * @name Payload
 * @package ibc.core.channel.v2
 * @see proto type: ibc.core.channel.v2.Payload
 */
export const Payload = {
    typeUrl: "/ibc.core.channel.v2.Payload",
    aminoType: "cosmos-sdk/Payload",
    is(o) {
        return o && (o.$typeUrl === Payload.typeUrl || typeof o.sourcePort === "string" && typeof o.destinationPort === "string" && typeof o.version === "string" && typeof o.encoding === "string" && (o.value instanceof Uint8Array || typeof o.value === "string"));
    },
    isAmino(o) {
        return o && (o.$typeUrl === Payload.typeUrl || typeof o.source_port === "string" && typeof o.destination_port === "string" && typeof o.version === "string" && typeof o.encoding === "string" && (o.value instanceof Uint8Array || typeof o.value === "string"));
    },
    encode(message, writer = BinaryWriter.create()) {
        if (message.sourcePort !== "") {
            writer.uint32(10).string(message.sourcePort);
        }
        if (message.destinationPort !== "") {
            writer.uint32(18).string(message.destinationPort);
        }
        if (message.version !== "") {
            writer.uint32(26).string(message.version);
        }
        if (message.encoding !== "") {
            writer.uint32(34).string(message.encoding);
        }
        if (message.value.length !== 0) {
            writer.uint32(42).bytes(message.value);
        }
        return writer;
    },
    decode(input, length) {
        const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
        let end = length === undefined ? reader.len : reader.pos + length;
        const message = createBasePayload();
        while (reader.pos < end) {
            const tag = reader.uint32();
            switch (tag >>> 3) {
                case 1:
                    message.sourcePort = reader.string();
                    break;
                case 2:
                    message.destinationPort = reader.string();
                    break;
                case 3:
                    message.version = reader.string();
                    break;
                case 4:
                    message.encoding = reader.string();
                    break;
                case 5:
                    message.value = reader.bytes();
                    break;
                default:
                    reader.skipType(tag & 7);
                    break;
            }
        }
        return message;
    },
    fromPartial(object) {
        const message = createBasePayload();
        message.sourcePort = object.sourcePort ?? "";
        message.destinationPort = object.destinationPort ?? "";
        message.version = object.version ?? "";
        message.encoding = object.encoding ?? "";
        message.value = object.value ?? new Uint8Array();
        return message;
    },
    fromAmino(object) {
        const message = createBasePayload();
        if (object.source_port !== undefined && object.source_port !== null) {
            message.sourcePort = object.source_port;
        }
        if (object.destination_port !== undefined && object.destination_port !== null) {
            message.destinationPort = object.destination_port;
        }
        if (object.version !== undefined && object.version !== null) {
            message.version = object.version;
        }
        if (object.encoding !== undefined && object.encoding !== null) {
            message.encoding = object.encoding;
        }
        if (object.value !== undefined && object.value !== null) {
            message.value = bytesFromBase64(object.value);
        }
        return message;
    },
    toAmino(message) {
        const obj = {};
        obj.source_port = message.sourcePort === "" ? undefined : message.sourcePort;
        obj.destination_port = message.destinationPort === "" ? undefined : message.destinationPort;
        obj.version = message.version === "" ? undefined : message.version;
        obj.encoding = message.encoding === "" ? undefined : message.encoding;
        obj.value = message.value ? base64FromBytes(message.value) : undefined;
        return obj;
    },
    fromAminoMsg(object) {
        return Payload.fromAmino(object.value);
    },
    toAminoMsg(message) {
        return {
            type: "cosmos-sdk/Payload",
            value: Payload.toAmino(message)
        };
    },
    fromProtoMsg(message) {
        return Payload.decode(message.value);
    },
    toProto(message) {
        return Payload.encode(message).finish();
    },
    toProtoMsg(message) {
        return {
            typeUrl: "/ibc.core.channel.v2.Payload",
            value: Payload.encode(message).finish()
        };
    },
    registerTypeUrl() { }
};
function createBaseAcknowledgement() {
    return {
        appAcknowledgements: []
    };
}
/**
 * Acknowledgement contains a list of all ack results associated with a single packet.
 * In the case of a successful receive, the acknowledgement will contain an app acknowledgement
 * for each application that received a payload in the same order that the payloads were sent
 * in the packet.
 * If the receive is not successful, the acknowledgement will contain a single app acknowledgment
 * which will be a constant error acknowledgment as defined by the IBC v2 protocol.
 * @name Acknowledgement
 * @package ibc.core.channel.v2
 * @see proto type: ibc.core.channel.v2.Acknowledgement
 */
export const Acknowledgement = {
    typeUrl: "/ibc.core.channel.v2.Acknowledgement",
    aminoType: "cosmos-sdk/Acknowledgement",
    is(o) {
        return o && (o.$typeUrl === Acknowledgement.typeUrl || Array.isArray(o.appAcknowledgements) && (!o.appAcknowledgements.length || o.appAcknowledgements[0] instanceof Uint8Array || typeof o.appAcknowledgements[0] === "string"));
    },
    isAmino(o) {
        return o && (o.$typeUrl === Acknowledgement.typeUrl || Array.isArray(o.app_acknowledgements) && (!o.app_acknowledgements.length || o.app_acknowledgements[0] instanceof Uint8Array || typeof o.app_acknowledgements[0] === "string"));
    },
    encode(message, writer = BinaryWriter.create()) {
        for (const v of message.appAcknowledgements) {
            writer.uint32(10).bytes(v);
        }
        return writer;
    },
    decode(input, length) {
        const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
        let end = length === undefined ? reader.len : reader.pos + length;
        const message = createBaseAcknowledgement();
        while (reader.pos < end) {
            const tag = reader.uint32();
            switch (tag >>> 3) {
                case 1:
                    message.appAcknowledgements.push(reader.bytes());
                    break;
                default:
                    reader.skipType(tag & 7);
                    break;
            }
        }
        return message;
    },
    fromPartial(object) {
        const message = createBaseAcknowledgement();
        message.appAcknowledgements = object.appAcknowledgements?.map(e => e) || [];
        return message;
    },
    fromAmino(object) {
        const message = createBaseAcknowledgement();
        message.appAcknowledgements = object.app_acknowledgements?.map(e => bytesFromBase64(e)) || [];
        return message;
    },
    toAmino(message) {
        const obj = {};
        if (message.appAcknowledgements) {
            obj.app_acknowledgements = message.appAcknowledgements.map(e => base64FromBytes(e));
        }
        else {
            obj.app_acknowledgements = message.appAcknowledgements;
        }
        return obj;
    },
    fromAminoMsg(object) {
        return Acknowledgement.fromAmino(object.value);
    },
    toAminoMsg(message) {
        return {
            type: "cosmos-sdk/Acknowledgement",
            value: Acknowledgement.toAmino(message)
        };
    },
    fromProtoMsg(message) {
        return Acknowledgement.decode(message.value);
    },
    toProto(message) {
        return Acknowledgement.encode(message).finish();
    },
    toProtoMsg(message) {
        return {
            typeUrl: "/ibc.core.channel.v2.Acknowledgement",
            value: Acknowledgement.encode(message).finish()
        };
    },
    registerTypeUrl() { }
};
function createBaseRecvPacketResult() {
    return {
        status: 0,
        acknowledgement: new Uint8Array()
    };
}
/**
 * RecvPacketResult speecifies the status of a packet as well as the acknowledgement bytes.
 * @name RecvPacketResult
 * @package ibc.core.channel.v2
 * @see proto type: ibc.core.channel.v2.RecvPacketResult
 */
export const RecvPacketResult = {
    typeUrl: "/ibc.core.channel.v2.RecvPacketResult",
    aminoType: "cosmos-sdk/RecvPacketResult",
    is(o) {
        return o && (o.$typeUrl === RecvPacketResult.typeUrl || isSet(o.status) && (o.acknowledgement instanceof Uint8Array || typeof o.acknowledgement === "string"));
    },
    isAmino(o) {
        return o && (o.$typeUrl === RecvPacketResult.typeUrl || isSet(o.status) && (o.acknowledgement instanceof Uint8Array || typeof o.acknowledgement === "string"));
    },
    encode(message, writer = BinaryWriter.create()) {
        if (message.status !== 0) {
            writer.uint32(8).int32(message.status);
        }
        if (message.acknowledgement.length !== 0) {
            writer.uint32(18).bytes(message.acknowledgement);
        }
        return writer;
    },
    decode(input, length) {
        const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
        let end = length === undefined ? reader.len : reader.pos + length;
        const message = createBaseRecvPacketResult();
        while (reader.pos < end) {
            const tag = reader.uint32();
            switch (tag >>> 3) {
                case 1:
                    message.status = reader.int32();
                    break;
                case 2:
                    message.acknowledgement = reader.bytes();
                    break;
                default:
                    reader.skipType(tag & 7);
                    break;
            }
        }
        return message;
    },
    fromPartial(object) {
        const message = createBaseRecvPacketResult();
        message.status = object.status ?? 0;
        message.acknowledgement = object.acknowledgement ?? new Uint8Array();
        return message;
    },
    fromAmino(object) {
        const message = createBaseRecvPacketResult();
        if (object.status !== undefined && object.status !== null) {
            message.status = object.status;
        }
        if (object.acknowledgement !== undefined && object.acknowledgement !== null) {
            message.acknowledgement = bytesFromBase64(object.acknowledgement);
        }
        return message;
    },
    toAmino(message) {
        const obj = {};
        obj.status = message.status === 0 ? undefined : message.status;
        obj.acknowledgement = message.acknowledgement ? base64FromBytes(message.acknowledgement) : undefined;
        return obj;
    },
    fromAminoMsg(object) {
        return RecvPacketResult.fromAmino(object.value);
    },
    toAminoMsg(message) {
        return {
            type: "cosmos-sdk/RecvPacketResult",
            value: RecvPacketResult.toAmino(message)
        };
    },
    fromProtoMsg(message) {
        return RecvPacketResult.decode(message.value);
    },
    toProto(message) {
        return RecvPacketResult.encode(message).finish();
    },
    toProtoMsg(message) {
        return {
            typeUrl: "/ibc.core.channel.v2.RecvPacketResult",
            value: RecvPacketResult.encode(message).finish()
        };
    },
    registerTypeUrl() { }
};
