import { PROPS } from '../constants' ; import { EnhancerContext , EnhancerResult , StringKeyMap } from '../types' ; import { isFunction } from '../util' ; type ParameterValues < T > = ( ( t ? : T ) => unknown ) | unknown ; type Props < T > = keyof T | Array < keyof T > ; const generateInvoke = ( hookAlias , params ) : string => { const mapParam = ( p ) : string => { const value = params [ p ] ; if ( isFunction ( value ) ) { return `${ p }(${ PROPS })` ; } return p ; } ; return `${ hookAlias }(${ Object . keys ( params ) . map ( mapParam ) . join ( ', ' ) })` ; } ; const generateContainerCode = < T > ( props , generateNewVariable ) : string [ ] => { if ( ! Array . isArray ( props ) ) { return [ `const ${ props } = ` , '' ] ; } if ( props . length === 0 ) { return [ '' , '' ] ; } const resultAlias = generateNewVariable ( ) ; return [ `const ${ resultAlias } = ` , props . map ( ( p , i ) => `const ${ p } = ${ resultAlias }[${ i }];` ) . join ( '\n' ) , ] ; } ; const normalizeProps = < T > ( props ? ) : string [ ] => { if ( ! props ) { return [ ] ; } if ( props instanceof Array ) { return props as string [ ] ; } return [ props as string ] ; } ; const normalizedParameterValues = < T > ( parameterValues , generateNewVariable ) : StringKeyMap < unknown > => { const wrappedInArray = Array . isArray ( parameterValues ) ? parameterValues : [ parameterValues ] ; const params = { } ; wrappedInArray . forEach ( ( value ) => { params [ generateNewVariable ( ) ] = value ; } ) ; return params ; } ; export default < T > ( hook , parameterValues , props ? ) => ( { generateNewVariable } ) : EnhancerResult => { const hookAlias = generateNewVariable ( ) ; const params = normalizedParameterValues ( parameterValues , generateNewVariable ) ; const containerCoder = generateContainerCode ( props , generateNewVariable ) ; const invokeCode = generateInvoke ( hookAlias , params ) ; return { dependencies : { [ hookAlias ] : hook , ... params , } , initialize : `
      ${ containerCoder [ 0 ] }${ invokeCode };
      ${ containerCoder [ 1 ] }
    ` , props : normalizeProps ( props ) , } ; } ;