import { EventEmitter } from 'events'

export type AsterWsChannel = 'tickers' | 'trades' | 'books' | 'klines'

export interface AsterStreamArg {
  channel: AsterWsChannel | string
  symbol?: string
  interval?: string
  depth?: number
  [key: string]: any
}

export interface AsterWsConfig {
  wsUrl: string
  pingIntervalMs?: number
  pongTimeoutMs?: number
  autoReconnect?: boolean
  reconnectIntervalMs?: number
}

export type AsterAuthType = 'none' | 'signer'

export interface AsterAuthConfig {
  type: AsterAuthType
  user?: string
  signer?: string
  privateKey?: string
  loginMethod?: 'login'
}

export interface AsterTicker {
  symbol: string
  price: number
  change24h?: number
  changePercent24h?: number
  high24h?: number
  low24h?: number
  volume24h?: number
  ts: number
}
export interface AsterTrade {
  symbol: string
  price: number
  size: number
  side: 'buy' | 'sell'
  ts: number
}
export interface AsterDepth {
  symbol: string
  bids: Array<[number, number]>
  asks: Array<[number, number]>
  ts: number
}
export interface AsterKline {
  symbol: string
  interval: string
  open: number
  high: number
  low: number
  close: number
  volume: number
  openTime: number
  closeTime: number
}

export interface AsterWsClientEvents {
  open: () => void
  close: (code: number, reason: string) => void
  error: (err: Error) => void
  ticker: (t: AsterTicker) => void
  trade: (t: AsterTrade) => void
  depth: (d: AsterDepth) => void
  kline: (k: AsterKline) => void
  raw: (msg: any) => void
}

export type AsterWsClientEmitter = EventEmitter & {
  on<U extends keyof AsterWsClientEvents>(event: U, listener: AsterWsClientEvents[U]): AsterWsClientEmitter
  emit<U extends keyof AsterWsClientEvents>(event: U, ...args: Parameters<AsterWsClientEvents[U]>): boolean
}
