import { failSafe } from "@esri/hub-common" ; import { IHubRequestOptions } from "@esri/hub-common" ; import { UserSession } from "@esri/arcgis-rest-auth" ; import { getItemResources , removeItemResource } from "@esri/arcgis-rest-portal" ; import { _getImageCropIdsFromLayout } from "./_get-image-crop-ids-from-layout" ; import { ILayout , IEntry } from "./types" ; /**
 * THIS UTIL IS NO LONGER IN USE BUT MAY BE IN THE FUTURE - TATE
 */ param { String } id param { Object } layout param { IHubRequestOptions } hubRequestOptions export function removeUnusedResources ( id , layout , hubRequestOptions ) { const layoutImageCropIds = _getImageCropIdsFromLayout ( layout ) ; return getItemResources ( id , hubRequestOptions ) . then ( response => { const itemResourcesOnAGO = ( response . resources || [ ] ) . map ( extractResourceProperty ) ; const imageItemResourcesOnAGO = itemResourcesOnAGO . filter ( resourceStartsWithImageSource ) ; delete hubRequestOptions . params ; const itemResourcesToRemove = getUnusedItemCrops ( layoutImageCropIds , imageItemResourcesOnAGO ) ; return removeUnusedResourcesFromAGO ( id , itemResourcesToRemove , hubRequestOptions . authentication ) ; } ) ; } function extractResourceProperty ( entry ) { return entry . resource ; } function getUnusedItemCrops ( layoutImageCropIds , itemImageResources ) { if ( ! layoutContainsImageCards ( layoutImageCropIds ) ) { return itemImageResources ; } return itemImageResources . filter ( isNotACurrentImageCropId ( layoutImageCropIds ) ) ; } function layoutContainsImageCards ( layoutImageCropIds ) { return layoutImageCropIds . length > 0 ; } function resourceStartsWithImageSource ( agoResource ) { return agoResource . indexOf ( "hub-image-card-crop-" ) === 0 ; } function isNotACurrentImageCropId ( imageCropIds ) { const cropRegex = new RegExp ( `-crop-(${ imageCropIds . join ( "|" ) }).png$` ) ; return ( resource ) => ! resource . match ( cropRegex ) ; } function removeUnusedResourcesFromAGO ( id , unusedCrops , authentication ) { const failSaveRemoveItemResources = failSafe ( removeItemResource , { success : true } ) ; return Promise . all ( unusedCrops . map ( ( resource ) => failSaveRemoveItemResources ( { id , resource , authentication } ) ) ) ; }