var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _RewardsIntegrationService_instances, _RewardsIntegrationService_deps, _RewardsIntegrationService_messenger, _RewardsIntegrationService_getChainIdForNetwork;
import { BASIS_POINTS_DIVISOR, BUILDER_FEE_CONFIG } from "../constants/hyperLiquidConfig.mjs";
import { PERPS_CONSTANTS } from "../constants/perpsConfig.mjs";
import { getSelectedEvmAccountFromMessenger } from "../utils/accountUtils.mjs";
import { ensureError } from "../utils/errorUtils.mjs";
import { formatAccountToCaipAccountId } from "../utils/rewardsUtils.mjs";
/**
 * RewardsIntegrationService
 *
 * Handles rewards-related operations and fee discount calculations.
 * Stateless service that coordinates with RewardsController and NetworkController.
 *
 * Instance-based service with constructor injection of platform dependencies.
 */
export class RewardsIntegrationService {
    /**
     * Create a new RewardsIntegrationService instance
     *
     * @param deps - Platform dependencies for logging, metrics, etc.
     * @param messenger - Controller messenger for cross-controller communication.
     */
    constructor(deps, messenger) {
        _RewardsIntegrationService_instances.add(this);
        _RewardsIntegrationService_deps.set(this, void 0);
        _RewardsIntegrationService_messenger.set(this, void 0);
        __classPrivateFieldSet(this, _RewardsIntegrationService_deps, deps, "f");
        __classPrivateFieldSet(this, _RewardsIntegrationService_messenger, messenger, "f");
    }
    /**
     * Calculate user fee discount from rewards
     * Returns discount in basis points (e.g., 6500 = 65% discount)
     *
     * @returns The fee discount in basis points, or undefined if unavailable.
     */
    async calculateUserFeeDiscount() {
        try {
            const evmAccount = getSelectedEvmAccountFromMessenger(__classPrivateFieldGet(this, _RewardsIntegrationService_messenger, "f"));
            if (!evmAccount) {
                __classPrivateFieldGet(this, _RewardsIntegrationService_deps, "f").debugLogger.log('RewardsIntegrationService: No EVM account found for fee discount');
                return undefined;
            }
            // Get the chain ID via DI network controller
            const networkState = __classPrivateFieldGet(this, _RewardsIntegrationService_messenger, "f").call('NetworkController:getState');
            const { selectedNetworkClientId } = networkState;
            const chainId = __classPrivateFieldGet(this, _RewardsIntegrationService_instances, "m", _RewardsIntegrationService_getChainIdForNetwork).call(this, selectedNetworkClientId);
            if (!chainId) {
                __classPrivateFieldGet(this, _RewardsIntegrationService_deps, "f").logger.error(new Error('Chain ID not found for fee discount calculation'), {
                    tags: { feature: PERPS_CONSTANTS.FeatureName },
                    context: {
                        name: 'RewardsIntegrationService.calculateUserFeeDiscount',
                        data: {
                            selectedNetworkClientId,
                        },
                    },
                });
                return undefined;
            }
            // Use pure utility function for CAIP formatting (pass logger for error reporting)
            const caipAccountId = formatAccountToCaipAccountId(evmAccount.address, chainId, __classPrivateFieldGet(this, _RewardsIntegrationService_deps, "f").logger);
            if (!caipAccountId) {
                __classPrivateFieldGet(this, _RewardsIntegrationService_deps, "f").logger.error(new Error('Failed to format CAIP account ID for fee discount'), {
                    tags: { feature: PERPS_CONSTANTS.FeatureName },
                    context: {
                        name: 'RewardsIntegrationService.calculateUserFeeDiscount',
                        data: {
                            address: evmAccount.address,
                            chainId,
                            selectedNetworkClientId,
                        },
                    },
                });
                return undefined;
            }
            // Use rewards via DI (no RewardsController in Core yet).
            // The rewards controller needs the perps MetaMask builder base fee in
            // bips to convert an absolute VIP fee into a discount fraction.
            const discountBips = await __classPrivateFieldGet(this, _RewardsIntegrationService_deps, "f").rewards.getPerpsDiscountForAccount(caipAccountId, BUILDER_FEE_CONFIG.MaxFeeDecimal * BASIS_POINTS_DIVISOR);
            // null = subscription state not hydrated yet; surface as undefined so
            // callers don't treat it as a definitive "no discount" answer.
            if (discountBips === null) {
                __classPrivateFieldGet(this, _RewardsIntegrationService_deps, "f").debugLogger.log('RewardsIntegrationService: Fee discount unavailable (subscription state not hydrated)', { address: evmAccount.address, caipAccountId });
                return undefined;
            }
            __classPrivateFieldGet(this, _RewardsIntegrationService_deps, "f").debugLogger.log('RewardsIntegrationService: Fee discount calculated', {
                address: evmAccount.address,
                caipAccountId,
                discountBips,
                discountPercentage: discountBips / 100,
            });
            return discountBips;
        }
        catch (error) {
            __classPrivateFieldGet(this, _RewardsIntegrationService_deps, "f").logger.error(ensureError(error, 'RewardsIntegrationService.calculateUserFeeDiscount'), {
                tags: { feature: PERPS_CONSTANTS.FeatureName },
                context: {
                    name: 'RewardsIntegrationService.calculateUserFeeDiscount',
                    data: {},
                },
            });
            return undefined;
        }
    }
}
_RewardsIntegrationService_deps = new WeakMap(), _RewardsIntegrationService_messenger = new WeakMap(), _RewardsIntegrationService_instances = new WeakSet(), _RewardsIntegrationService_getChainIdForNetwork = function _RewardsIntegrationService_getChainIdForNetwork(networkClientId) {
    try {
        const networkClient = __classPrivateFieldGet(this, _RewardsIntegrationService_messenger, "f").call('NetworkController:getNetworkClientById', networkClientId);
        return networkClient.configuration.chainId;
    }
    catch {
        // Network client may not exist
        return undefined;
    }
};
//# sourceMappingURL=RewardsIntegrationService.mjs.map