import { getType } from '../lib/get_type' ; export function castProvider ( types ) { return function cast ( node , toTypeNames ) { if ( ! toTypeNames || toTypeNames . length === 0 ) return node ; const fromTypeName = getType ( node ) ; if ( toTypeNames . includes ( fromTypeName ) ) return node ; const fromTypeDef = types [ fromTypeName ] ; for ( let i = 0 ; i < toTypeNames . length ; i ++ ) { if ( fromTypeDef && fromTypeDef . castsTo ( toTypeNames [ i ] ) ) { return fromTypeDef . to ( node , toTypeNames [ i ] , types ) ; } const toTypeDef = types [ toTypeNames [ i ] ] ; if ( toTypeDef && toTypeDef . castsFrom ( fromTypeName ) ) return toTypeDef . from ( node , types ) ; } throw new Error ( `Can not cast '${ fromTypeName }' to any of '${ toTypeNames . join ( ', ' ) }'` ) ; } ; }