import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';
import { Bootstrap } from '@tinymanorg/tinyman-js-sdk';
import { getAlgodClient, extractNetwork } from '../../../algorand-client.js';
import { withCommonParams } from '../../commonParams.js';
export const bootstrapTools = [
    {
        name: 'api_tinyman_get_pool_creation_quote',
        description: 'Get quote for creating a new Tinyman pool',
        inputSchema: withCommonParams({
            type: 'object',
            properties: {
                asset1Id: {
                    type: 'integer',
                    description: 'ID of the first asset'
                },
                asset2Id: {
                    type: 'integer',
                    description: 'ID of the second asset'
                },
                initiatorAddr: {
                    type: 'string',
                    description: 'Address of the account creating the pool'
                },
                version: {
                    type: 'string',
                    enum: ['v1_1', 'v2'],
                    description: 'Tinyman protocol version',
                    default: 'v2'
                }
            },
            required: ['asset1Id', 'asset2Id', 'initiatorAddr']
        })
    }
];
export async function handleBootstrapTools(args) {
    const { name, asset1Id, asset2Id, initiatorAddr, version = 'v2' } = args;
    const network = extractNetwork(args);
    const algodClient = getAlgodClient(network);
    if (network === 'localnet') {
        throw new McpError(ErrorCode.InvalidRequest, 'Tinyman is not available on localnet');
    }
    const tinymanNetwork = network;
    if (name === 'api_tinyman_get_pool_creation_quote') {
        try {
            const quote = await (version === 'v2'
                ? Bootstrap.v2.generateTxns({
                    client: algodClient,
                    network: tinymanNetwork,
                    asset_1: {
                        id: asset1Id.toString(),
                        unit_name: ''
                    },
                    asset_2: {
                        id: asset2Id.toString(),
                        unit_name: ''
                    },
                    initiatorAddr
                })
                : Bootstrap.v1_1.generateTxns({
                    client: algodClient,
                    network: tinymanNetwork,
                    asset_1: {
                        id: asset1Id.toString(),
                        unit_name: ''
                    },
                    asset_2: {
                        id: asset2Id.toString(),
                        unit_name: ''
                    },
                    initiatorAddr
                }));
            return {
                version,
                asset1Id,
                asset2Id,
                transactionCount: quote.length,
                estimatedFees: quote.reduce((sum, txn) => sum + Number(txn.txn.fee), 0)
            };
        }
        catch (error) {
            throw new McpError(ErrorCode.InternalError, `Failed to get pool creation quote: ${error instanceof Error ? error.message : String(error)}`);
        }
    }
    throw new McpError(ErrorCode.MethodNotFound, `Unknown bootstrap tool: ${name}`);
}
