"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BundleDataSS = exports.SortableEventSS = exports.AddressType = void 0;
const superstruct_1 = require("superstruct");
const ethers_1 = require("ethers");
const viem_1 = require("viem");
const kit_1 = require("@solana/kit");
const tronweb_1 = require("tronweb");
const constants_1 = require("../../../constants");
const utils_1 = require("../../../utils");
const PositiveIntegerStringSS = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /\d+/);
const Web3AddressSS = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^0x[a-fA-F0-9]{64}$/);
const BigNumberType = (0, superstruct_1.coerce)((0, superstruct_1.instance)(utils_1.BigNumber), (0, superstruct_1.union)([(0, superstruct_1.string)(), (0, superstruct_1.number)()]), (value) => {
    try {
        return utils_1.BigNumber.from(value);
    }
    catch {
        return value;
    }
});
const AddressInstanceSS = (0, superstruct_1.union)([
    (0, superstruct_1.instance)(utils_1.EvmAddress),
    (0, superstruct_1.instance)(utils_1.SvmAddress),
    (0, superstruct_1.instance)(utils_1.TvmAddress),
    (0, superstruct_1.instance)(utils_1.RawAddress),
]);
exports.AddressType = (0, superstruct_1.coerce)(AddressInstanceSS, (0, superstruct_1.string)(), (value) => {
    try {
        if (value.length === 42 && (0, viem_1.isAddress)(value, { strict: false }))
            return utils_1.EvmAddress.from(value);
        if (value.length === 34 && tronweb_1.TronWeb.isAddress(value))
            return utils_1.TvmAddress.from(value);
        if ((0, kit_1.isAddress)(value))
            return utils_1.SvmAddress.from(value);
    }
    catch {
    }
    return new utils_1.RawAddress(value.startsWith("0x") ? ethers_1.utils.arrayify(value) : utils_1.bs58.decode(value));
});
const Web3AddressType = (0, superstruct_1.coerce)(Web3AddressSS, (0, superstruct_1.string)(), (value) => {
    return (0, utils_1.toBytes32)(value);
});
const FillTypeSS = (0, superstruct_1.number)();
const V3RelayDataSS = {
    inputToken: exports.AddressType,
    inputAmount: BigNumberType,
    outputToken: exports.AddressType,
    outputAmount: BigNumberType,
    fillDeadline: (0, superstruct_1.number)(),
    exclusiveRelayer: exports.AddressType,
    exclusivityDeadline: (0, superstruct_1.number)(),
    originChainId: (0, superstruct_1.number)(),
    depositor: exports.AddressType,
    recipient: exports.AddressType,
    depositId: BigNumberType,
    message: (0, superstruct_1.string)(),
};
exports.SortableEventSS = {
    blockNumber: (0, superstruct_1.number)(),
    logIndex: (0, superstruct_1.number)(),
    txnRef: (0, superstruct_1.optional)((0, superstruct_1.string)()),
    txnIndex: (0, superstruct_1.optional)((0, superstruct_1.number)()),
    transactionHash: (0, superstruct_1.optional)((0, superstruct_1.string)()),
    transactionIndex: (0, superstruct_1.optional)((0, superstruct_1.number)()),
};
const V3DepositSS = {
    messageHash: (0, superstruct_1.defaulted)((0, superstruct_1.string)(), constants_1.UNDEFINED_MESSAGE_HASH),
    fromLiteChain: (0, superstruct_1.defaulted)((0, superstruct_1.boolean)(), false),
    toLiteChain: (0, superstruct_1.defaulted)((0, superstruct_1.boolean)(), false),
    destinationChainId: (0, superstruct_1.number)(),
    quoteTimestamp: (0, superstruct_1.number)(),
    relayerFeePct: (0, superstruct_1.optional)(BigNumberType),
    speedUpSignature: (0, superstruct_1.optional)((0, superstruct_1.string)()),
    updatedRecipient: (0, superstruct_1.optional)(exports.AddressType),
    updatedOutputAmount: (0, superstruct_1.optional)(BigNumberType),
    updatedMessage: (0, superstruct_1.optional)((0, superstruct_1.string)()),
};
const _V3DepositWithBlockSS = {
    quoteBlockNumber: (0, superstruct_1.number)(),
    ...V3DepositSS,
    ...exports.SortableEventSS,
    ...V3RelayDataSS,
};
const V3DepositWithBlockSS = (0, superstruct_1.object)(_V3DepositWithBlockSS);
const V3DepositWithBlockLpFeeSS = (0, superstruct_1.object)({
    ..._V3DepositWithBlockSS,
    lpFeePct: BigNumberType,
});
const V3RelayExecutionEventInfoSS = (0, superstruct_1.object)({
    updatedOutputAmount: BigNumberType,
    fillType: FillTypeSS,
    updatedRecipient: exports.AddressType,
    updatedMessage: (0, superstruct_1.optional)((0, superstruct_1.string)()),
    updatedMessageHash: (0, superstruct_1.defaulted)((0, superstruct_1.string)(), constants_1.UNDEFINED_MESSAGE_HASH),
});
const V3FillSS = {
    ...V3RelayDataSS,
    message: (0, superstruct_1.optional)((0, superstruct_1.string)()),
    messageHash: (0, superstruct_1.defaulted)((0, superstruct_1.string)(), constants_1.UNDEFINED_MESSAGE_HASH),
    destinationChainId: (0, superstruct_1.number)(),
    relayer: exports.AddressType,
    repaymentChainId: (0, superstruct_1.number)(),
    relayExecutionInfo: V3RelayExecutionEventInfoSS,
    quoteTimestamp: (0, superstruct_1.number)(),
};
const V3FillWithBlockSS = {
    ...exports.SortableEventSS,
    ...V3FillSS,
};
const BundleFillV3SS = (0, superstruct_1.object)({
    ...V3FillWithBlockSS,
    lpFeePct: BigNumberType,
});
const nestedV3DepositRecordSS = (0, superstruct_1.record)(PositiveIntegerStringSS, (0, superstruct_1.record)(Web3AddressType, (0, superstruct_1.array)(V3DepositWithBlockSS)));
const nestedV3DepositRecordWithLpFeePctSS = (0, superstruct_1.record)(PositiveIntegerStringSS, (0, superstruct_1.record)(Web3AddressType, (0, superstruct_1.array)(V3DepositWithBlockLpFeeSS)));
const nestedV3BundleFillsSS = (0, superstruct_1.record)(PositiveIntegerStringSS, (0, superstruct_1.record)(Web3AddressType, (0, superstruct_1.object)({
    fills: (0, superstruct_1.array)(BundleFillV3SS),
    refunds: (0, superstruct_1.record)(Web3AddressType, BigNumberType),
    totalRefundAmount: BigNumberType,
    realizedLpFees: BigNumberType,
})));
exports.BundleDataSS = (0, superstruct_1.type)({
    bundleDepositsV3: nestedV3DepositRecordSS,
    expiredDepositsToRefundV3: nestedV3DepositRecordSS,
    unexecutableSlowFills: nestedV3DepositRecordWithLpFeePctSS,
    bundleSlowFillsV3: nestedV3DepositRecordWithLpFeePctSS,
    bundleFillsV3: nestedV3BundleFillsSS,
});
//# sourceMappingURL=SuperstructUtils.js.map