import { utils as anchorUtils, BN } from "@coral-xyz/anchor";
import { PublicKey } from "@solana/web3.js";
import { BigNumber, ethers } from "ethers";
/**
 * Converts an integer to a 32-byte Uint8Array.
 */
export function intToU8Array32(num) {
    var bigIntValue = BigInt(num instanceof BN ? num.toString() : num);
    if (bigIntValue < 0)
        throw new Error("Input must be a non-negative integer or BN");
    var hexString = bigIntValue.toString(16).padStart(64, "0"); // 32 bytes = 64 hex chars
    var u8Array = Array.from(Buffer.from(hexString, "hex"));
    return u8Array;
}
/**
 * Converts a 32-byte Uint8Array to a bigint.
 */
export function u8Array32ToInt(u8Array) {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    var isValidArray = function (arr) { return Array.isArray(arr) && arr.every(Number.isInteger); };
    if ((u8Array instanceof Uint8Array || isValidArray(u8Array)) && u8Array.length === 32) {
        return Array.from(u8Array).reduce(function (num, byte) { return (num << BigInt(8)) | BigInt(byte); }, BigInt(0));
    }
    throw new Error("Input must be a Uint8Array or an array of 32 numbers.");
}
/**
 * Converts a 32-byte Uint8Array to a BigNumber.
 */
export function u8Array32ToBigNumber(u8Array) {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    var isValidArray = function (arr) { return Array.isArray(arr) && arr.every(Number.isInteger); };
    if ((u8Array instanceof Uint8Array || isValidArray(u8Array)) && u8Array.length === 32) {
        var hexString = "0x" + Buffer.from(u8Array).toString("hex");
        return BigNumber.from(hexString);
    }
    throw new Error("Input must be a Uint8Array or an array of 32 numbers.");
}
/**
 * Converts a string to a PublicKey.
 */
export function strPublicKey(publicKey) {
    return new PublicKey(publicKey).toString();
}
/**
 * Converts an EVM address to a Solana PublicKey.
 */
export var evmAddressToPublicKey = function (address) {
    var bytes32Address = "0x000000000000000000000000".concat(address.replace("0x", ""));
    return new PublicKey(ethers.utils.arrayify(bytes32Address));
};
/**
 * Converts a Solana PublicKey to an EVM address.
 */
export var publicKeyToEvmAddress = function (publicKey) {
    // Convert the input to a PublicKey if it's a string
    var pubKeyBuffer = typeof publicKey === "string" ? new PublicKey(publicKey).toBuffer() : publicKey.toBuffer();
    // Extract the last 20 bytes to get the Ethereum address
    var addressBuffer = pubKeyBuffer.slice(-20);
    // Convert the buffer to a hex string and prepend '0x'
    return "0x".concat(addressBuffer.toString("hex"));
};
/**
 * Converts a base58 string to a bytes32 string.
 */
export var fromBase58ToBytes32 = function (input) {
    var decodedBytes = anchorUtils.bytes.bs58.decode(input);
    return "0x" + Buffer.from(decodedBytes).toString("hex");
};
/**
 * Converts a bytes32 string to an EVM address.
 */
export var fromBytes32ToAddress = function (input) {
    var hexString = input.startsWith("0x") ? input.slice(2) : input;
    if (hexString.length !== 64) {
        throw new Error("Invalid bytes32 string");
    }
    var address = hexString.slice(-40);
    return "0x" + address;
};
/**
 * Converts EVM or SVM address to a bytes32 string.
 */
export var addressOrBase58ToBytes32 = function (input) {
    if (ethers.utils.isAddress(input)) {
        return ethers.utils.hexZeroPad(input, 32);
    }
    else if (ethers.utils.isHexString(input, 32)) {
        return input;
    }
    else {
        return fromBase58ToBytes32(input);
    }
};
//# sourceMappingURL=conversionUtils.js.map