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

import { ModuleManager } from '../../module-manager'
import { asAddress, asAddressVector, asObject, asU16 } from '../../utils'

const MODULE_NAME = 'executor_layerzero'

export const ExecutorLayerzeroErrorCode = {
    // ExecutorLayerzero related errors (matching executor_layerzero.move)
    ExecutorLayerzero_EWorkerCapNotFromPackage: 1,
} as const

export class ExecutorLayerZero {
    public packageId: string
    public readonly client: SuiClient

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

    // === Set Functions ===

    /**
     * Initialize Executor LayerZero instance
     * Creates and shares a new Executor with the specified configuration
     * @param tx - The transaction to add the move call to
     * @param workerCap - Worker capability object ID or transaction argument
     * @param depositAddress - Address for fee deposits or transaction argument
     * @param supportedMessageLibs - Array of supported message library addresses or transaction argument
     * @param priceFeed - Price feed object address or transaction argument
     * @param workerFeeLib - Worker fee library address or transaction argument
     * @param defaultMultiplierBps - Default multiplier in basis points or transaction argument
     * @param owner - Owner address for the executor or transaction argument
     * @param admins - Array of admin addresses or transaction argument
     */
    initExecutorMoveCall(
        tx: Transaction,
        workerCap: string | TransactionArgument,
        depositAddress: string | TransactionArgument,
        supportedMessageLibs: string[] | TransactionArgument,
        priceFeed: string | TransactionArgument,
        workerFeeLib: string | TransactionArgument,
        defaultMultiplierBps: number | TransactionArgument,
        owner: string | TransactionArgument,
        admins: string[] | TransactionArgument
    ): void {
        tx.moveCall({
            target: this.#target('init_executor'),
            arguments: [
                asObject(tx, workerCap),
                asAddress(tx, depositAddress),
                asAddressVector(tx, supportedMessageLibs),
                asAddress(tx, priceFeed),
                asAddress(tx, workerFeeLib),
                asU16(tx, defaultMultiplierBps),
                asAddress(tx, owner),
                asAddressVector(tx, admins),
                tx.object(this.moduleManager.objects.workerRegistry),
            ],
        })
    }

    /**
     * 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: string = MODULE_NAME): string {
        return `${this.packageId}::${module_name}::${name}`
    }
}
