import { CodeModel , ImplementationLocation , ParameterLocation } from "@autorest/codemodel" ; import { getLanguageMetadata } from "../utils/languageHelpers" ; export interface EndpointDetails { isCustom : boolean ; endpoint ? : string ; parameterName ? : string ; } export function transformBaseUrl ( codeModel ) { let endpoint = "" ; let isCustom = false ; const $host = ( codeModel . globalParameters || [ ] ) . find ( p => { const { name } = getLanguageMetadata ( p . language ) ; return name === "$host" && Boolean ( p . clientDefaultValue ) ; } ) ; let parameterName ; if ( ! $host ) { if ( codeModel . operationGroups && codeModel . operationGroups . length ) { const { requests } = codeModel . operationGroups [ 0 ] . operations [ 0 ] ; parameterName = getEndpointParameter ( codeModel ) ; isCustom = true ; endpoint = requests ! [ 0 ] . protocol . http ! . uri ; } } else { endpoint = $host . clientDefaultValue ; } return { parameterName , endpoint : endpoint , isCustom } ; } function getEndpointParameter ( codeModel ) { if ( ! codeModel . globalParameters || ! codeModel . globalParameters . length ) { return ; } const uriParameters = codeModel . globalParameters . filter ( gp => gp . implementation === ImplementationLocation . Client && gp . protocol . http ?. in === ParameterLocation . Uri ) ; if ( ! uriParameters . length || ! getLanguageMetadata ( uriParameters [ 0 ] . language ) . serializedName ) { return ; } return getLanguageMetadata ( uriParameters [ 0 ] . language ) . serializedName ; }