import { values } from 'lodash' ; import { Contract , EventArgDeclaration , EventDeclaration , getFullSignatureAsSymbolForEvent } from 'typechain' ; import { codegenOutputType } from './types' ; export function codegenEventsDeclarations ( contract ) { return values ( contract . events ) . map ( ( e ) => { if ( e . length === 1 ) { return codegenSingleEventsDeclaration ( e [ 0 ] ) ; } else { return codegenOverloadEventsDeclaration ( e ) ; } } ) . join ( '\n' ) ; } export function codegenAllPossibleEvents ( contract ) { const allPossibleEvents = values ( contract . events ) . map ( ( e ) => e [ 0 ] ) . filter ( ( e ) => ! e . isAnonymous ) . map ( ( e ) => e . name ) ; if ( allPossibleEvents . length === 0 ) { return `type AllEvents = never` ; } return `type AllEvents = ${ allPossibleEvents . join ( ' | ' ) };` ; } export function codegenEventsEmitters ( contract ) { return values ( contract . events ) . filter ( ( e ) => ! e [ 0 ] . isAnonymous ) . map ( ( e ) => { if ( e . length === 1 ) { return codegenSingleEventsEmitter ( e [ 0 ] ) ; } else { return '' ; } } ) . join ( '\n' ) ; } function codegenSingleEventsEmitter ( e , overloadName ? , overloadType ? ) { return `${ overloadName ?? e . name }(cb?: Callback<${ overloadType ?? e . name }>): EventEmitter;` ; } function codegenOverloadEventsDeclaration ( e ) { const eventsDecls = e . map ( ( e ) => codegenSingleEventsDeclaration ( e , getFullSignatureAsSymbolForEvent ( e ) ) ) ; const union = `type ${ e [ 0 ] . name } = ${ e . map ( ( e ) => getFullSignatureAsSymbolForEvent ( e ) ) . join ( '|' ) }` ; return `
  ${ eventsDecls . join ( '\n' ) }

  ${ union }
  ` ; } function codegenSingleEventsDeclaration ( e , overloadName ? ) { return `
  export interface ${ overloadName ?? e . name } {
    name: "${ e . name }"
    args: ${ codegenOutputTypesForEvents ( e . inputs ) }
  }
  ` ; } function codegenOutputTypesForEvents ( outputs ) { return `{
    ${ outputs . map ( ( param ) => ( param . name ? `${ param . name } : ${ codegenOutputType ( param . type ) }, ` : '' ) ) . join ( '\n' ) }
    ${ outputs . map ( ( param , index ) => index . toString ( ) + ':' + codegenOutputType ( param . type ) ) . join ( ', ' ) }
  }` ; }