import { __assign } from "tslib";
import { object, number, optional, string, array, record, coerce, instance, pattern, boolean, defaulted, union, type, } from "superstruct";
import { CHAIN_IDs, UNDEFINED_MESSAGE_HASH } from "../../../constants";
import { BigNumber, EvmAddress, RawAddress, SvmAddress, toAddressType, toBytes32 } from "../../../utils";
var PositiveIntegerStringSS = pattern(string(), /\d+/);
var Web3AddressSS = pattern(string(), /^0x[a-fA-F0-9]{64}$/);
var BigNumberType = coerce(instance(BigNumber), union([string(), number()]), function (value) {
    try {
        // Attempt to convert the string to a BigNumber
        return BigNumber.from(value);
    }
    catch (error) {
        // In case of any error during conversion, return the original value
        // This will lead to a validation error, as the resulting value won't match the expected BigNumber type
        return value;
    }
});
// Accept any concrete implementation of `Address` (Evm, Svm, or Raw) but avoid using the
// abstract `Address` class directly to keep TypeScript happy.
var AddressInstanceSS = union([instance(EvmAddress), instance(SvmAddress), instance(RawAddress)]);
var AddressType = coerce(AddressInstanceSS, string(), function (value) {
    // Addresses are posted to arweave in their native format (base16 for EVM, base58 for SVM). The chainId for
    // for the event data is not directly available, so infer it based on the shape of the address. RawAddress
    // will be instantiated if the address format does not match the expected family.
    var chainId = value.startsWith("0x") ? CHAIN_IDs.MAINNET : CHAIN_IDs.SOLANA;
    return toAddressType(value, chainId);
});
var Web3AddressType = coerce(Web3AddressSS, string(), function (value) {
    return toBytes32(value);
});
var FillTypeSS = number();
var V3RelayDataSS = {
    inputToken: AddressType,
    inputAmount: BigNumberType,
    outputToken: AddressType,
    outputAmount: BigNumberType,
    fillDeadline: number(),
    exclusiveRelayer: AddressType,
    exclusivityDeadline: number(),
    originChainId: number(),
    depositor: AddressType,
    recipient: AddressType,
    depositId: BigNumberType,
    message: string(),
};
export var SortableEventSS = {
    blockNumber: number(),
    logIndex: number(),
    txnRef: optional(string()),
    txnIndex: optional(number()),
    transactionHash: optional(string()),
    transactionIndex: optional(number()),
};
var V3DepositSS = {
    messageHash: defaulted(string(), UNDEFINED_MESSAGE_HASH),
    fromLiteChain: defaulted(boolean(), false),
    toLiteChain: defaulted(boolean(), false),
    destinationChainId: number(),
    quoteTimestamp: number(),
    relayerFeePct: optional(BigNumberType),
    speedUpSignature: optional(string()),
    updatedRecipient: optional(AddressType),
    updatedOutputAmount: optional(BigNumberType),
    updatedMessage: optional(string()),
};
var _V3DepositWithBlockSS = __assign(__assign(__assign({ quoteBlockNumber: number() }, V3DepositSS), SortableEventSS), V3RelayDataSS);
var V3DepositWithBlockSS = object(_V3DepositWithBlockSS);
var V3DepositWithBlockLpFeeSS = object(__assign(__assign({}, _V3DepositWithBlockSS), { lpFeePct: BigNumberType }));
var V3RelayExecutionEventInfoSS = object({
    updatedOutputAmount: BigNumberType,
    fillType: FillTypeSS,
    updatedRecipient: AddressType,
    updatedMessage: optional(string()),
    updatedMessageHash: defaulted(string(), UNDEFINED_MESSAGE_HASH),
});
var V3FillSS = __assign(__assign({}, V3RelayDataSS), { message: optional(string()), messageHash: defaulted(string(), UNDEFINED_MESSAGE_HASH), destinationChainId: number(), relayer: AddressType, repaymentChainId: number(), relayExecutionInfo: V3RelayExecutionEventInfoSS, quoteTimestamp: number() });
var V3FillWithBlockSS = __assign(__assign({}, SortableEventSS), V3FillSS);
var BundleFillV3SS = object(__assign(__assign({}, V3FillWithBlockSS), { lpFeePct: BigNumberType }));
var nestedV3DepositRecordSS = record(PositiveIntegerStringSS, record(Web3AddressType, array(V3DepositWithBlockSS)));
var nestedV3DepositRecordWithLpFeePctSS = record(PositiveIntegerStringSS, record(Web3AddressType, array(V3DepositWithBlockLpFeeSS)));
var nestedV3BundleFillsSS = record(
// Must be a chainId
PositiveIntegerStringSS, record(Web3AddressType, object({
    fills: array(BundleFillV3SS),
    refunds: record(Web3AddressType, BigNumberType),
    totalRefundAmount: BigNumberType,
    realizedLpFees: BigNumberType,
})));
export var BundleDataSS = type({
    bundleDepositsV3: nestedV3DepositRecordSS,
    expiredDepositsToRefundV3: nestedV3DepositRecordSS,
    unexecutableSlowFills: nestedV3DepositRecordWithLpFeePctSS,
    bundleSlowFillsV3: nestedV3DepositRecordWithLpFeePctSS,
    bundleFillsV3: nestedV3BundleFillsSS,
});
//# sourceMappingURL=SuperstructUtils.js.map