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

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

const MODULE_NAME = 'executor_ptb_builder'

export class ExecutorPtbBuilder {
    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
    }

    // === Set Functions ===

    /**
     * Creates a transaction to build Executor PTB
     * @param tx - The transaction to add the move call to
     * @param executorAddress - Executor object address or transaction argument
     * @param feelibAddress - Executor fee lib object address or transaction argument
     * @param priceFeedAddress - Price feed object address or transaction argument
     * @returns Transaction result containing get_fee and assign_job move calls
     */
    buildExecutorPtbMoveCall(
        tx: Transaction,
        executorAddress: string | TransactionArgument,
        feelibAddress: string | TransactionArgument,
        priceFeedAddress: string | TransactionArgument
    ): TransactionResult {
        return tx.moveCall({
            target: this.#target('build_executor_ptb'),
            arguments: [asObject(tx, executorAddress), asObject(tx, feelibAddress), asObject(tx, priceFeedAddress)],
        })
    }

    /**
     * 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}`
    }
}
