"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.USED_NONCES_DISCRIMINATOR = void 0;
exports.getUsedNoncesDiscriminatorBytes = getUsedNoncesDiscriminatorBytes;
exports.getUsedNoncesEncoder = getUsedNoncesEncoder;
exports.getUsedNoncesDecoder = getUsedNoncesDecoder;
exports.getUsedNoncesCodec = getUsedNoncesCodec;
exports.decodeUsedNonces = decodeUsedNonces;
exports.fetchUsedNonces = fetchUsedNonces;
exports.fetchMaybeUsedNonces = fetchMaybeUsedNonces;
exports.fetchAllUsedNonces = fetchAllUsedNonces;
exports.fetchAllMaybeUsedNonces = fetchAllMaybeUsedNonces;
exports.getUsedNoncesSize = getUsedNoncesSize;
var tslib_1 = require("tslib");
var kit_1 = require("@solana/kit");
exports.USED_NONCES_DISCRIMINATOR = new Uint8Array([
    60, 112, 18, 72, 138, 181, 100, 138,
]);
function getUsedNoncesDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.USED_NONCES_DISCRIMINATOR);
}
function getUsedNoncesEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ["discriminator", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ["remoteDomain", (0, kit_1.getU32Encoder)()],
        ["firstNonce", (0, kit_1.getU64Encoder)()],
        ["usedNonces", (0, kit_1.getArrayEncoder)((0, kit_1.getU64Encoder)(), { size: 100 })],
    ]), function (value) { return (tslib_1.__assign(tslib_1.__assign({}, value), { discriminator: exports.USED_NONCES_DISCRIMINATOR })); });
}
function getUsedNoncesDecoder() {
    return (0, kit_1.getStructDecoder)([
        ["discriminator", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ["remoteDomain", (0, kit_1.getU32Decoder)()],
        ["firstNonce", (0, kit_1.getU64Decoder)()],
        ["usedNonces", (0, kit_1.getArrayDecoder)((0, kit_1.getU64Decoder)(), { size: 100 })],
    ]);
}
function getUsedNoncesCodec() {
    return (0, kit_1.combineCodec)(getUsedNoncesEncoder(), getUsedNoncesDecoder());
}
function decodeUsedNonces(encodedAccount) {
    return (0, kit_1.decodeAccount)(encodedAccount, getUsedNoncesDecoder());
}
function fetchUsedNonces(rpc, address, config) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var maybeAccount;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4, fetchMaybeUsedNonces(rpc, address, config)];
                case 1:
                    maybeAccount = _a.sent();
                    (0, kit_1.assertAccountExists)(maybeAccount);
                    return [2, maybeAccount];
            }
        });
    });
}
function fetchMaybeUsedNonces(rpc, address, config) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var maybeAccount;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4, (0, kit_1.fetchEncodedAccount)(rpc, address, config)];
                case 1:
                    maybeAccount = _a.sent();
                    return [2, decodeUsedNonces(maybeAccount)];
            }
        });
    });
}
function fetchAllUsedNonces(rpc, addresses, config) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var maybeAccounts;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4, fetchAllMaybeUsedNonces(rpc, addresses, config)];
                case 1:
                    maybeAccounts = _a.sent();
                    (0, kit_1.assertAccountsExist)(maybeAccounts);
                    return [2, maybeAccounts];
            }
        });
    });
}
function fetchAllMaybeUsedNonces(rpc, addresses, config) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var maybeAccounts;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4, (0, kit_1.fetchEncodedAccounts)(rpc, addresses, config)];
                case 1:
                    maybeAccounts = _a.sent();
                    return [2, maybeAccounts.map(function (maybeAccount) { return decodeUsedNonces(maybeAccount); })];
            }
        });
    });
}
function getUsedNoncesSize() {
    return 820;
}
//# sourceMappingURL=usedNonces.js.map