import { IModel , IHubRequestOptions , _getHttpAndHttpsUris , includes , } from "@esri/hub-common" ; import { getDomainsForSite , IDomainEntry , removeDomain , addDomain , } from "@esri/hub-common" ; import { updateAppRedirectUris } from "./update-app-redirect-uris" ; param { Object } site param { Array } uris param { IHubRequestOptions } hubRequestOptions export function updateSiteApplicationUris ( site , uris , hubRequestOptions ) { if ( hubRequestOptions . isPortal ) return Promise . resolve ( { } ) ; const redirectUris = uris . reduce ( ( acc , uri ) => { return acc . concat ( _getHttpAndHttpsUris ( uri ) ) ; } , [ ] ) ; return updateAppRedirectUris ( site . data . values . clientId , redirectUris , hubRequestOptions ) . then ( ( ) => { return getDomainsForSite ( site . item . id , hubRequestOptions ) ; } ) . then ( ( domainInfos ) => { const domainsToRemove = domainInfos . filter ( ( domain ) => ! includes ( uris , domain . hostname ) ) ; const hostnames = domainInfos . map ( ( domain ) => domain . hostname ) ; const domainsToAdd = uris . filter ( ( uri ) => ! includes ( hostnames , uri ) ) ; const domainPromises = [ ] ; domainsToRemove . forEach ( ( domain ) => domainPromises . push ( removeDomain ( domain . id , hubRequestOptions ) ) ) ; domainsToAdd . forEach ( ( uri ) => domainPromises . push ( addDomain ( { orgKey : hubRequestOptions . portalSelf . urlKey , orgId : hubRequestOptions . portalSelf . id , orgTitle : hubRequestOptions . portalSelf . name , hostname : uri , siteId : site . item . id , siteTitle : site . item . title , clientKey : site . data . values . clientId , sslOnly : domainInfos [ 0 ] ? ! ! domainInfos [ 0 ] . sslOnly : true , } , hubRequestOptions ) ) ) ; return Promise . all ( domainPromises ) ; } ) ; }