"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SequenceTracker = exports.getSequenceTracker = exports.deriveEmitterSequenceKey = void 0;
const web3_js_1 = require("@solana/web3.js");
const connect_sdk_solana_1 = require("@wormhole-foundation/connect-sdk-solana");
function deriveEmitterSequenceKey(emitter, wormholeProgramId) {
    return connect_sdk_solana_1.utils.deriveAddress([Buffer.from('Sequence'), new web3_js_1.PublicKey(emitter).toBytes()], wormholeProgramId);
}
exports.deriveEmitterSequenceKey = deriveEmitterSequenceKey;
async function getSequenceTracker(connection, emitter, wormholeProgramId, commitment) {
    return connection
        .getAccountInfo(deriveEmitterSequenceKey(emitter, wormholeProgramId), commitment)
        .then((info) => SequenceTracker.deserialize(connect_sdk_solana_1.utils.getAccountData(info)));
}
exports.getSequenceTracker = getSequenceTracker;
class SequenceTracker {
    sequence;
    bump;
    emitterType;
    constructor(sequence, bump, emitterType) {
        this.sequence = sequence;
        this.bump = bump;
        this.emitterType = emitterType;
    }
    static deserialize(data) {
        if (data.length !== 8 && data.length !== 10) {
            throw new Error('data.length != 8 or data.length != 10');
        }
        let bump, emitterType;
        const sequence = data.readBigUInt64LE(0);
        if (data.length === 10) {
            bump = data[8];
            emitterType = data[9];
        }
        return new SequenceTracker(sequence, bump, emitterType);
    }
    value() {
        return this.sequence;
    }
}
exports.SequenceTracker = SequenceTracker;
//# sourceMappingURL=sequence.js.map