import { IRequestOptions } from "@esri/arcgis-rest-request" ; import { IInitiativeModel , camelize , IModel } from "@esri/hub-common" ; import { getInitiative } from "./get" ; import { createInitiativeModelFromTemplate , IInitiativeGroupIds , IInitiativeTemplateOptions } from "./templates" ; import { addInitiative } from "./add" ; import { copyImageResources , copyEmbeddedImageResources } from "./util" ; import { shareItemWithGroup , IGroupSharingOptions } from "@esri/arcgis-rest-portal" ; import { getProp } from "@esri/hub-common" ; export param { string | any } template param { string } title param { any } groupIds param { IRequestOptions } requestOptions returns { Promise < IInitiativeModel > } private export function activateInitiative ( template , title , groupIds , requestOptions ) { const ro = { ... requestOptions } as IRequestOptions ; const state = { initiativeKey : camelize ( title ) } as any ; let promise ; if ( typeof template === "string" ) { promise = getInitiative ( template , ro ) ; } else { promise = Promise . resolve ( template ) ; } return promise . then ( async ( templateItemModel ) => { state . template = templateItemModel ; const options = { title , description : title , initiativeKey : state . initiativeKey , groupIds } as IInitiativeTemplateOptions ; state . initiativeModel = createInitiativeModelFromTemplate ( state . template , options ) ; return addInitiative ( state . initiativeModel , ro ) ; } ) . then ( ( newModel ) => { state . initiativeModel = newModel ; const assets = getProp ( state , "template.assets" ) ; if ( assets ) { return copyEmbeddedImageResources ( newModel . item . id , newModel . item . owner , assets , ro ) ; } else { const { id , owner } = newModel . item ; const wellKnownAssets = [ "detail-image.jpg" , "icon-dark.png" , "icon-light.png" ] ; return copyImageResources ( state . template . item . id , id , owner , wellKnownAssets , ro ) ; } } ) . then ( ( ) : Promise < any > => { const collaborationGroupId = getProp ( state , "initiativeModel.item.properties.collaborationGroupId" ) ; if ( collaborationGroupId ) { const shareOptions = { id : state . initiativeModel . item . id , groupId : collaborationGroupId , confirmItemControl : true , ... requestOptions } as IGroupSharingOptions ; return shareItemWithGroup ( shareOptions ) ; } else { return Promise . resolve ( { success : true } ) ; } } ) . then ( ( ) => { return state . initiativeModel ; } ) ; }