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

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

const MODULE_NAME = 'blocked_message_lib'

export const BlockedMessageLibErrorCode = {
    // BlockedMessageLib related errors (with BlockedMessageLib_ prefix)
    BlockedMessageLib_ENotImplemented: 1,
} as const

export class BlockedMessageLib {
    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 ===

    /**
     * Quote messaging fees (always throws not implemented error)
     * @param tx - The transaction to add the move call to
     * @param call - The call transaction result or transaction argument
     */
    quoteMoveCall(tx: Transaction, call: TransactionArgument): void {
        tx.moveCall({
            target: this.#target('quote'),
            arguments: [tx.object(this.objects.blockedMessageLib), call],
        })
    }

    /**
     * Send message (always throws not implemented error)
     * @param tx - The transaction to add the move call to
     * @param call - The call transaction result or transaction argument
     */
    sendMoveCall(tx: Transaction, call: TransactionArgument): void {
        tx.moveCall({
            target: this.#target('send'),
            arguments: [tx.object(this.objects.blockedMessageLib), call],
        })
    }

    /**
     * Set configuration (always throws not implemented error)
     * @param tx - The transaction to add the move call to
     * @param call - The call transaction result or transaction argument
     */
    setConfigMoveCall(tx: Transaction, call: TransactionArgument): void {
        tx.moveCall({
            target: this.#target('set_config'),
            arguments: [tx.object(this.objects.blockedMessageLib), call],
        })
    }

    // === View Functions ===

    /**
     * Get blocked message library version
     * @param tx - The transaction to add the move call to
     * @returns Transaction result containing the version information
     */
    versionMoveCall(tx: Transaction): TransactionResult {
        return tx.moveCall({
            target: this.#target('version'),
            arguments: [],
        })
    }

    /**
     * Get blocked message library version
     * @returns Promise<[string, string, string]> - Tuple of [major, minor, endpoint] version strings
     */
    async getVersion(): Promise<[string, string, string]> {
        return executeSimulate(
            this.client,
            (tx) => {
                this.versionMoveCall(tx)
            },
            (result) => [
                bcs.U64.parse(result[0].value).toString(),
                bcs.U8.parse(result[1].value).toString(),
                bcs.U8.parse(result[2].value).toString(),
            ]
        )
    }

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