import { SandDance } from '@msrvida/sanddance-react' ; export interface RolePrefs { [ columnName ] : Partial < SandDance . specs . Insight > ; } export interface SpecTypePrefs { [ role ] : RolePrefs ; } export interface Prefs { [ chart ] : SpecTypePrefs ; } export function initPrefs ( prefs , partialInsight ) { if ( partialInsight ) { const specTypePrefs = prefs [ partialInsight . chart ] || { } ; prefs [ partialInsight . chart ] = specTypePrefs ; for ( let _role in partialInsight . columns ) { let role = _role as SandDance . specs . InsightColumnRoles ; if ( role === 'color' || role === 'x' ) { let rolePrefs = specTypePrefs [ role ] || { } ; specTypePrefs [ role ] = rolePrefs ; let column = partialInsight . columns [ role ] ; let copySignalValue = ( signalName ) => { if ( partialInsight . signalValues && partialInsight . signalValues [ signalName ] && rolePrefs [ column ] ) { const signalValues = rolePrefs [ column ] . signalValues || { } ; signalValues [ signalName ] = partialInsight . signalValues [ signalName ] ; rolePrefs [ column ] . signalValues = signalValues ; } } ; switch ( role ) { case 'color' : rolePrefs [ column ] = { scheme : partialInsight . scheme , colorBin : partialInsight . colorBin } ; copySignalValue ( SandDance . constants . SignalNames . ColorBinCount ) ; break ; case 'x' : copySignalValue ( SandDance . constants . SignalNames . XBins ) ; break ; } } } } } export function saveSignalValuePref ( prefs , chart , role , column , signalName , signalValue ) { const partialInsight = savePref ( prefs , chart , role , column , { signalValues : { } } ) ; partialInsight . signalValues [ signalName ] = signalValue ; } export function copyPrefToNewState ( prefs , chart , role , columnName ) { const specTypePrefs = SandDance . VegaDeckGl . util . deepMerge ( { } , prefs [ '*' ] , prefs [ chart ] ) ; const rolePrefs = SandDance . VegaDeckGl . util . deepMerge ( { } , specTypePrefs [ '*' ] , specTypePrefs [ role ] ) ; const partialInsight = SandDance . VegaDeckGl . util . deepMerge ( { } , rolePrefs [ '*' ] , rolePrefs [ columnName ] ) ; return partialInsight ; } export function savePref ( prefs , chart , role , column , partialInsight ) { const SpecTypePrefs = prefs [ chart ] || { } ; prefs [ chart ] = SpecTypePrefs ; const rolePrefs = SpecTypePrefs [ role ] || { } ; SpecTypePrefs [ role ] = rolePrefs ; rolePrefs [ column ] = SandDance . VegaDeckGl . util . deepMerge ( { } , rolePrefs [ column ] , partialInsight ) ; return rolePrefs [ column ] ; }