import { LayerDescriptor } from '../../../common/descriptor_types' ; import { MapState } from './types' ; import { copyPersistentState , TRACKED_LAYER_DESCRIPTOR } from '../copy_persistent_state' ; export function getLayerIndex ( list , layerId ) { return list . findIndex ( ( { id } ) => layerId === id ) ; } export function findLayerById ( state , layerId ) { return state . layerList . find ( ( { id } ) => layerId === id ) ; } export function clearLayerProp ( state , layerId , propName ) { if ( ! layerId ) { return state ; } const { layerList } = state ; const layerIdx = getLayerIndex ( layerList , layerId ) ; if ( layerIdx === - 1 ) { return state ; } const updatedLayer = { ... layerList [ layerIdx ] , } ; delete updatedLayer [ propName ] ; const updatedList = [ ... layerList . slice ( 0 , layerIdx ) , updatedLayer , ... layerList . slice ( layerIdx + 1 ) , ] ; return { ... state , layerList : updatedList } ; } export function updateLayerInList ( state , layerId , attribute , newValue ? ) { if ( ! layerId ) { return state ; } const { layerList } = state ; const layerIdx = getLayerIndex ( layerList , layerId ) ; if ( layerIdx === - 1 ) { return state ; } const updatedLayer = { ... layerList [ layerIdx ] , [ attribute ] : newValue || newValue === '' || newValue === 0 ? newValue : ! ( layerList [ layerIdx ] [ attribute ] as boolean ) , } ; const updatedList = [ ... layerList . slice ( 0 , layerIdx ) , updatedLayer , ... layerList . slice ( layerIdx + 1 ) , ] ; return { ... state , layerList : updatedList } ; } export function updateLayerSourceDescriptorProp ( state , layerId , propName , value ) { const { layerList } = state ; const layerIdx = getLayerIndex ( layerList , layerId ) ; const updatedLayer = { ... layerList [ layerIdx ] , sourceDescriptor : { ... layerList [ layerIdx ] . sourceDescriptor , [ propName ] : value , } , } ; const updatedList = [ ... layerList . slice ( 0 , layerIdx ) , updatedLayer , ... layerList . slice ( layerIdx + 1 ) , ] as LayerDescriptor [ ] ; return { ... state , layerList : updatedList } ; } export function trackCurrentLayerState ( state , layerId ) { const layer = findLayerById ( state , layerId ) ; const layerCopy = copyPersistentState ( layer ) ; return updateLayerInList ( state , layerId , TRACKED_LAYER_DESCRIPTOR , layerCopy ) ; } export function removeTrackedLayerState ( state , layerId ) { const layer = findLayerById ( state , layerId ) ; if ( ! layer ) { return state ; } const copyLayer = { ... layer } ; delete copyLayer [ TRACKED_LAYER_DESCRIPTOR ] ; return { ... state , layerList : setLayer ( state . layerList , copyLayer ) , } ; } export function rollbackTrackedLayerState ( state , layerId ) { const layer = findLayerById ( state , layerId ) ; if ( ! layer ) { return state ; } const trackedLayerDescriptor = layer [ TRACKED_LAYER_DESCRIPTOR ] ; const rolledbackLayer = { ... layer , ... trackedLayerDescriptor } ; delete rolledbackLayer [ TRACKED_LAYER_DESCRIPTOR ] ; return { ... state , layerList : setLayer ( state . layerList , rolledbackLayer ) , } ; } export function setLayer ( layerList , layerDescriptor ) { const layerIndex = getLayerIndex ( layerList , layerDescriptor . id ) ; if ( layerIndex === - 1 ) { return layerList ; } const newLayerList = [ ... layerList ] ; newLayerList [ layerIndex ] = layerDescriptor ; return newLayerList ; }