import { getProp , cloneObject } from "@esri/hub-common" ; import { IInitiativeModel } from "@esri/hub-common" ; protected export const isIndicator = function ( obj ) { let result = false ; if ( Array . isArray ( obj . fields ) && obj . fields . length > 0 ) { result = true ; } return result ; } ; protected export const convertIndicatorField = function ( field ) { return { id : field . fieldName , name : field . label , optional : field . optional || false , description : field . tooltip , supportedTypes : [ ... field . supportedTypes ] } ; } ; protected export const convertIndicatorToDefinition = function ( ind ) { const def = { id : ind . fieldName , type : "Data" , name : ind . label || ind . fieldName , optional : ind . optional || false , definition : { description : ind . label || ind . fieldName , supportedTypes : [ ... ind . layerOptions . supportedTypes ] , geometryTypes : [ ... ind . layerOptions . geometryTypes ] , fields : ind . fields . map ( convertIndicatorField ) } } ; return def ; } ; protected export function convertIndicatorsToDefinitions ( indicatorsHash ) { if ( ! indicatorsHash . fields || ! Array . isArray ( indicatorsHash . fields ) ) { indicatorsHash . fields = [ ] ; } const defs = indicatorsHash . fields . map ( convertIndicatorToDefinition ) ; return defs ; } protected export const extractIndicators = function ( values ) { return Object . keys ( values ) . reduce ( ( acc , prop ) => { const obj = values [ prop ] ; if ( isIndicator ( obj ) ) { const clone = cloneObject ( obj ) ; clone . id = prop ; acc . push ( clone ) ; } return acc ; } , [ ] ) ; } ; protected export const flattenField = function ( field ) { return { id : field . id , name : field . field . name , alias : field . field . alias , type : field . field . type } ; } ; protected export const convertIndicatorValueToSource = function ( indicator ) { return { type : "Feature Layer" , url : indicator . url , itemId : indicator . itemId , layerId : indicator . layerId , name : indicator . name || indicator . id , mappings : indicator . fields . map ( flattenField ) } ; } ; protected export const convertIndicator = function ( indicator ) { const result = { id : indicator . id , type : "Data" , name : indicator . name || indicator . id , definition : { description : indicator . name || indicator . id } , source : convertIndicatorValueToSource ( indicator ) } ; return result ; } ; protected export const convertInitiativeIndicators = function ( values ) { return extractIndicators ( values ) . map ( convertIndicator ) ; } ; protected export const convertStep = function ( step ) { const templates = step . templates || [ ] ; const items = step . items || [ ] ; return { title : step . title , description : step . description , id : step . id , templateIds : templates . map ( byId ) , itemIds : items . map ( byId ) } ; } ; protected export const convertSteps = function ( steps , values ) { if ( steps && Array . isArray ( steps ) ) { return steps . map ( stepName => { return convertStep ( values [ stepName ] ) ; } ) ; } else { return [ ] ; } } ; protected param { * } entry returns export function byId ( entry ) { return entry . id ; } protected param { IInitiativeModel } model param { string } portalUrl returns { IInitiativeModel } export function upgradeToTwoDotZero ( model , portalUrl ? ) { const curVersion = getProp ( model , "item.properties.schemaVersion" ) ; if ( curVersion < 2 ) { const clone = cloneObject ( model ) as IInitiativeModel ; clone . item . properties . schemaVersion = 2.0 ; clone . data . steps = convertSteps ( clone . data . values . steps , clone . data . values ) ; if ( clone . data . values . steps ) { clone . data . values . steps . forEach ( ( propName ) => { delete clone . data . values [ propName ] ; } ) ; delete clone . data . values . steps ; } clone . data . indicators = convertInitiativeIndicators ( clone . data . values ) ; return clone ; } else { return model ; } }