import { SuiClient } from '@mysten/sui/client'
import { Transaction, TransactionArgument, TransactionResult } from '@mysten/sui/transactions'

import { ModuleManager } from '../../module-manager'
import { ObjectOptions } from '../../types'

const MODULE_NAME = 'dvn_fee_lib'

export const DVNFeeLibErrorCode = {
    // DVN Fee Lib related errors - aligned with Move constants
    EEidNotSupported: 1,
    EInvalidDVNOptions: 2,
} as const

export class DVNFeeLib {
    public packageId: string
    public readonly client: SuiClient
    private readonly objects: ObjectOptions

    constructor(
        packageId: string,
        client: SuiClient,
        objects: ObjectOptions,
        private readonly moduleManager: ModuleManager
    ) {
        this.packageId = packageId
        this.client = client
        this.objects = objects
    }

    // === Witness Functions ===

    /**
     * Create a LayerZero witness for DVN Fee Lib package whitelist registration
     * @param tx - The transaction to add the move call to
     * @returns Transaction result containing the LayerZero witness
     */
    createLayerZeroWitnessMoveCall(tx: Transaction): TransactionResult {
        return tx.moveCall({
            target: `${this.packageId}::dvn_fee_lib_witness::new`,
            arguments: [],
        })
    }

    // === Set Functions ===

    /**
     * Create get fee call for DVN fee calculation
     * Note: This is typically called by the DVN, not directly by users
     * This initiates the fee calculation process by creating a child call to the price feed
     * @param tx - The transaction to add the move call to
     * @param call - The call transaction result containing fee parameters
     * @returns Transaction result containing the fee calculation call
     */
    getFeeMoveCall(tx: Transaction, call: TransactionArgument): TransactionResult {
        return tx.moveCall({
            target: this.#target('get_fee'),
            arguments: [tx.object(this.objects.dvnFeeLib), call],
        })
    }

    /**
     * Confirm get fee call with price feed integration
     * Note: This is typically called by the DVN, not directly by users
     * This completes the fee calculation by processing the price feed result and applying premium
     * @param tx - The transaction to add the move call to
     * @param feelibCall - The fee library call transaction result
     * @param priceFeedCall - The price feed call transaction result
     */
    confirmGetFeeMoveCall(tx: Transaction, feelibCall: TransactionArgument, priceFeedCall: TransactionArgument): void {
        tx.moveCall({
            target: this.#target('confirm_get_fee'),
            arguments: [tx.object(this.objects.dvnFeeLib), feelibCall, priceFeedCall],
        })
    }

    // === View Functions ===

    /**
     * Generate the full target path for move calls
     * @param name - The function name to call
     * @param module_name - The module name (defaults to MODULE_NAME)
     * @returns The full module path for the move call
     * @private
     */
    #target(name: string, module_name = MODULE_NAME): string {
        return `${this.packageId}::${module_name}::${name}`
    }
}
