import { loadConfig } from '../core/config.js'
import { BybitRestClient } from '../lib/bybit/rest.js'

function parseEntries(args: string[]): Array<{ coin: string; amountStr: string }> {
  return args.map(entry => {
    const [coin, amountStr] = entry.split(':')
    if (!coin || !amountStr) {
      throw new Error(`Invalid fund entry: ${entry}. Expected COIN:AMOUNT`)
    }

    return {
      coin: coin.toUpperCase(),
      amountStr
    }
  })
}

async function main(): Promise<void> {
  const entries = parseEntries(process.argv.slice(2))
  if (entries.length === 0) {
    throw new Error('Usage: npm run demo:fund -- USDT:10000 BTC:1')
  }

  const config = loadConfig()
  const client = new BybitRestClient({
    demoApiKey: config.demoApiKey,
    demoApiSecret: config.demoApiSecret,
    recvWindow: config.recvWindow
  })

  const result = await client.applyDemoFunds(entries)
  console.log(JSON.stringify(result, null, 2))
}

main().catch(error => {
  console.error(error)
  process.exit(1)
})
