"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WrappedMeta = exports.getWrappedMeta = exports.deriveWrappedMetaKey = exports.deriveWrappedMintKey = exports.deriveTokenMetadataKey = void 0;
const web3_js_1 = require("@solana/web3.js");
const utils_1 = require("../../../utils");
const utils_2 = require("../../utils");
var tokenMetadata_1 = require("../../utils/tokenMetadata");
Object.defineProperty(exports, "deriveTokenMetadataKey", { enumerable: true, get: function () { return tokenMetadata_1.deriveTokenMetadataKey; } });
function deriveWrappedMintKey(tokenBridgeProgramId, tokenChain, tokenAddress) {
    if (tokenChain == utils_1.CHAIN_ID_SOLANA) {
        throw new Error("tokenChain == CHAIN_ID_SOLANA does not have wrapped mint key");
    }
    if (typeof tokenAddress == "string") {
        tokenAddress = utils_1.tryNativeToUint8Array(tokenAddress, tokenChain);
    }
    return utils_2.deriveAddress([
        Buffer.from("wrapped"),
        (() => {
            const buf = Buffer.alloc(2);
            buf.writeUInt16BE(tokenChain);
            return buf;
        })(),
        tokenAddress,
    ], tokenBridgeProgramId);
}
exports.deriveWrappedMintKey = deriveWrappedMintKey;
function deriveWrappedMetaKey(tokenBridgeProgramId, mint) {
    return utils_2.deriveAddress([Buffer.from("meta"), new web3_js_1.PublicKey(mint).toBuffer()], tokenBridgeProgramId);
}
exports.deriveWrappedMetaKey = deriveWrappedMetaKey;
function getWrappedMeta(connection, tokenBridgeProgramId, mint, commitment) {
    return __awaiter(this, void 0, void 0, function* () {
        return connection
            .getAccountInfo(deriveWrappedMetaKey(tokenBridgeProgramId, mint), commitment)
            .then((info) => WrappedMeta.deserialize(utils_2.getAccountData(info)));
    });
}
exports.getWrappedMeta = getWrappedMeta;
class WrappedMeta {
    constructor(chain, tokenAddress, originalDecimals) {
        this.chain = chain;
        this.tokenAddress = tokenAddress;
        this.originalDecimals = originalDecimals;
    }
    static deserialize(data) {
        if (data.length != 35) {
            throw new Error("data.length != 35");
        }
        const chain = data.readUInt16LE(0);
        const tokenAddress = data.subarray(2, 34);
        const originalDecimals = data.readUInt8(34);
        return new WrappedMeta(chain, tokenAddress, originalDecimals);
    }
}
exports.WrappedMeta = WrappedMeta;
