import { ExpressionRunnerShorthandConfig , expressionRunnerSimpleConfigDefault , expressionRunnerRunButtonOnlyConfigDefault , expressionRunnerSingleStepConfigDefault , expressionRunnerPredefinedConfigDefault , isExpressionRunnerSimpleConfig , isExpressionRunnerRunButtonOnlyConfig , isExpressionRunnerSingleStepConfig } from 'scripts/lib/expressionRunnerShorthandConfig' ; import { ExpressionContainer , SteppedExpressionContainer } from 'src/types/ExpressionContainerTypes' ; import { InitializeInstruction , ExpressionRunnerContextProps , expressionRunnerContextDefault } from 'src/types/ExpressionRunnerTypes' ; import { ExpressionRunnerProps } from 'src/types/ExpressionRunnerTypes' ; export interface ExpressionRunnerConfig { initialExpressionContainer ? : SteppedExpressionContainer ; initialExpressionContainers ? : SteppedExpressionContainer [ ] ; hidePriorities : ExpressionRunnerContextProps [ 'hidePriorities' ] ; hideBottomRightBadges : ExpressionRunnerContextProps [ 'hideBottomRightBadges' ] ; hideControls : boolean ; explanationsVisibility : 'visible' | 'hidden' | 'hiddenInitialPausedOnly' ; initializeInstructions : readonly InitializeInstruction [ ] ; lastAllowedExpressionState ? : ExpressionContainer [ 'previouslyChangedExpressionState' ] ; lastAllowedExpressionStateAfterIterations ? : number ; hideRunButton ? : boolean ; speed : number ; showAllShowSteps ? : boolean ; skipAlphaConvert ? : boolean ; skipActive ? : boolean ; showDefaultAndActiveOnly ? : boolean ; skipToTheEnd : boolean ; hideFuncUnboundBadgeOnExplanation : boolean ; highlightOverridesCallArgAndFuncUnboundOnly : boolean ; bottomRightBadgeOverrides : ExpressionRunnerContextProps [ 'bottomRightBadgeOverrides' ] ; highlightOverrides : ExpressionRunnerContextProps [ 'highlightOverrides' ] ; highlightOverrideActiveAfterStart : boolean ; highlightFunctions : boolean ; convert ? : ExpressionRunnerProps [ 'convert' ] ; crossed ? : boolean ; applicativeOrder ? : boolean ; } const expressionRunnerDefaults = { hidePriorities : expressionRunnerContextDefault . hidePriorities , hideBottomRightBadges : expressionRunnerContextDefault . hideBottomRightBadges , hideControls : false , explanationsVisibility : 'visible' , initializeInstructions : [ ] , skipToTheEnd : false , hideRunButton : false , speed : 1 , hideFuncUnboundBadgeOnExplanation : false , highlightOverridesCallArgAndFuncUnboundOnly : false , bottomRightBadgeOverrides : expressionRunnerContextDefault . bottomRightBadgeOverrides , highlightOverrides : expressionRunnerContextDefault . highlightOverrides , highlightOverrideActiveAfterStart : expressionRunnerContextDefault . highlightOverrideActiveAfterStart , highlightFunctions : false } ; const buildInitializeInstructions = ( { nextIterations , isDone , initialState } ) : readonly InitializeInstruction [ ] => nextIterations ? [ ... Array ( nextIterations ) . fill ( { type : 'nextIteration' } ) , { type : 'stepForwardUntilPreviouslyChangedExpressionState' , state : initialState } ] : [ isDone ? { type : 'stepForwardUntilTheEnd' } : { type : 'stepForwardUntilPreviouslyChangedExpressionState' , state : initialState } ] ; function mergeWithDefault < A extends Record < string , any > , B extends Record < string , any > > ( base , defaults ) { const result = { ... base } ; Object . keys ( defaults ) . forEach ( key => { if ( base [ key ] === undefined ) { result [ key ] = defaults [ key ] ; } } ) ; return result as A & B ; } const buildExpressionRunnerConfigFromShorthand = ( config ) : ExpressionRunnerConfig => { let runnerProps ; if ( isExpressionRunnerSimpleConfig ( config ) ) { const { initialExpressionContainer , initialState , isDone , skipAlphaConvert , applicativeOrder , skipActive , showDefaultAndActiveOnly , nextIterations , showPriorities , showAllShowSteps , explanationsVisibility , bottomRightBadgeOverrides , highlightOverrides , highlightOverrideActiveAfterStart , highlightOverridesCallArgAndFuncUnboundOnly , highlightFunctions , crossed } = mergeWithDefault < typeof config , typeof expressionRunnerSimpleConfigDefault > ( config , expressionRunnerSimpleConfigDefault ) ; runnerProps = { initialExpressionContainer , hideControls : true , hidePriorities : ! showPriorities , explanationsVisibility , showAllShowSteps , highlightOverridesCallArgAndFuncUnboundOnly , skipAlphaConvert , applicativeOrder , skipActive , showDefaultAndActiveOnly , bottomRightBadgeOverrides , highlightOverrides , highlightFunctions , highlightOverrideActiveAfterStart , initializeInstructions : buildInitializeInstructions ( { nextIterations , isDone , initialState } ) , crossed } ; } else if ( isExpressionRunnerRunButtonOnlyConfig ( config ) ) { const { initialExpressionContainer , initialState , skipToTheEnd , hideFuncUnboundBadgeOnExplanation , showPriorities , lastAllowedExpressionState , lastAllowedExpressionStateAfterIterations , nextIterations , showAllShowSteps , speed , skipAlphaConvert , applicativeOrder , skipActive , showDefaultAndActiveOnly , highlightOverrides , explanationsVisibility } = mergeWithDefault < typeof config , typeof expressionRunnerRunButtonOnlyConfigDefault > ( config , expressionRunnerRunButtonOnlyConfigDefault ) ; runnerProps = { speed , initialExpressionContainer , hidePriorities : ! showPriorities , highlightOverrides , showAllShowSteps , hideFuncUnboundBadgeOnExplanation , skipToTheEnd , skipAlphaConvert , applicativeOrder , skipActive , showDefaultAndActiveOnly , explanationsVisibility : explanationsVisibility || 'hiddenInitialPausedOnly' , lastAllowedExpressionState , lastAllowedExpressionStateAfterIterations , initializeInstructions : buildInitializeInstructions ( { nextIterations , initialState } ) } ; } else if ( isExpressionRunnerSingleStepConfig ( config ) ) { const { initialExpressionContainer , initialState , finalState , hideFuncUnboundBadgeOnExplanation , showPriorities , explanationsVisibility , showAllShowSteps , nextIterations , skipActive , showDefaultAndActiveOnly } = mergeWithDefault < typeof config , typeof expressionRunnerSingleStepConfigDefault > ( config , expressionRunnerSingleStepConfigDefault ) ; runnerProps = { initialExpressionContainer , hidePriorities : ! showPriorities , hideFuncUnboundBadgeOnExplanation , hideRunButton : true , explanationsVisibility , lastAllowedExpressionState : finalState , lastAllowedExpressionStateAfterIterations : nextIterations , showAllShowSteps , skipActive , showDefaultAndActiveOnly , initializeInstructions : buildInitializeInstructions ( { nextIterations , initialState } ) } ; } else { const { initialExpressionContainers , hideFuncUnboundBadgeOnExplanation , showPriorities , explanationsVisibility , showAllShowSteps , applicativeOrder , nextIterations , skipToTheEnd , convert } = mergeWithDefault < typeof config , typeof expressionRunnerPredefinedConfigDefault > ( config , expressionRunnerPredefinedConfigDefault ) ; runnerProps = { initialExpressionContainers , hidePriorities : ! showPriorities , hideFuncUnboundBadgeOnExplanation , hideRunButton : false , applicativeOrder , explanationsVisibility , lastAllowedExpressionStateAfterIterations : nextIterations , showAllShowSteps , skipToTheEnd , convert } ; } return mergeWithDefault < typeof runnerProps , typeof expressionRunnerDefaults > ( runnerProps , expressionRunnerDefaults ) ; } ; export default buildExpressionRunnerConfigFromShorthand ;