import { PropertyValues } from 'lit' ; import { HomeAssistant } from 'custom-card-helpers' ; import { Floor3dCardConfig } from './types' ; param { ... object } objects returns { object } export function mergeDeep ( ... objects ) { const isObject = ( obj ) : boolean => obj && typeof obj === 'object' ; return objects . reduce ( ( prev , obj ) => { Object . keys ( obj ) . forEach ( ( key ) => { const pVal = prev [ key ] ; const oVal = obj [ key ] ; if ( Array . isArray ( pVal ) && Array . isArray ( oVal ) ) { prev [ key ] = pVal . concat ( ... oVal ) ; } else if ( isObject ( pVal ) && isObject ( oVal ) ) { prev [ key ] = mergeDeep ( pVal , oVal ) ; } else { prev [ key ] = oVal ; } } ) ; return prev ; } , { } ) ; } export function mapRange ( num , in_min , in_max , out_min , out_max ) { return ( ( num - in_min ) * ( out_max - out_min ) ) / ( in_max - in_min ) + out_min ; } export function hasConfigOrEntitiesChanged ( element , changedProps , forceUpdate ) { if ( changedProps . has ( 'config' ) || forceUpdate ) { return true ; } for ( const config of element . _configArray ) { if ( config . entity ) { const oldHass = changedProps . get ( 'hass' ) as HomeAssistant | undefined ; if ( oldHass ) { if ( oldHass . states [ config . entity ] !== element . hass ! . states [ config . entity ] ) { return true ; } else { continue ; } } return true ; } } return false ; } export function createConfigArray ( config ) { const configArray = [ ] ; if ( config . entities ) { for ( const entityConfig of config . entities ) { if ( typeof entityConfig == 'string' ) { const clonedObject = mergeDeep ( { } , config ) ; delete clonedObject . entities ; const stringConfig = mergeDeep ( clonedObject , { entity : entityConfig } ) ; configArray . push ( stringConfig ) ; } else if ( typeof entityConfig == 'object' ) { const clonedObject = mergeDeep ( { } , config ) ; delete clonedObject . entities ; const objectConfig = mergeDeep ( clonedObject , entityConfig ) ; configArray . push ( objectConfig ) ; } } } else { configArray . push ( config ) ; } return configArray ; } export function createObjectGroupConfigArray ( config ) { const configArray = [ ] ; if ( config . entities ) { for ( const entityConfig of config . entities ) { if ( entityConfig . object_id ) { if ( entityConfig . object_id . charAt ( 0 ) == '<' && entityConfig . object_id . charAt ( entityConfig . object_id . length - 1 ) == '>' ) { const group_name = entityConfig . object_id . substr ( 1 , entityConfig . object_id . length - 2 ) ; for ( const objectGroupConfig of config . object_groups ) { if ( objectGroupConfig . object_group == group_name ) { const objectConfig = mergeDeep ( { } , { entity : entityConfig . entity , objects : objectGroupConfig . objects } ) ; configArray . push ( objectConfig ) ; break ; } } } else if ( entityConfig . object_id !== '' ) { const objectConfig = mergeDeep ( { } , { entity : entityConfig . entity , objects : [ { object_id : entityConfig . object_id } ] } ) ; configArray . push ( objectConfig ) ; } } } } return configArray ; } export function createEditorObjectGroupConfigArray ( config ) { const configArray = [ ] ; if ( config . object_groups ) { for ( const objectGroupConfig of config . object_groups ) { if ( typeof objectGroupConfig == 'string' ) { const stringConfig = mergeDeep ( { } , { object_group : objectGroupConfig } ) ; configArray . push ( stringConfig ) ; } else if ( typeof objectGroupConfig == 'object' ) { const objectConfig = mergeDeep ( { } , objectGroupConfig ) ; configArray . push ( objectConfig ) ; } } } else { configArray . push ( config ) ; } return configArray ; } export function createEditorConfigArray ( config ) { const configArray = [ ] ; if ( config . entities ) { for ( const entityConfig of config . entities ) { if ( typeof entityConfig == 'string' ) { const stringConfig = mergeDeep ( { } , { entity : entityConfig } ) ; configArray . push ( stringConfig ) ; } else if ( typeof entityConfig == 'object' ) { const objectConfig = mergeDeep ( { } , entityConfig ) ; configArray . push ( objectConfig ) ; } } } else { configArray . push ( config ) ; } return configArray ; } export function arrayMove ( arr , fromIndex , toIndex ) { const element = arr [ fromIndex ] ; const newArray = arr . slice ( ) ; newArray . splice ( fromIndex , 1 ) ; newArray . splice ( toIndex , 0 , element ) ; return newArray ; }