import { Block , trap , assert , CompileError , Path , Dictionary } from "./exports" ; /** A Choice is a sum (discriminated union). It is a block where all the fields
 * (called options) are inputs, and exactly one of them is "chosen", meaning it
 * has a value and is accessible with a ?-guarded path */ export class Choice extends Block { /** 0-based index of choice. defaults to first choice */ choiceIndex : number = 0 ; get choice ( ) { return this . fields [ this . choiceIndex ] ; } /** set choice and initialize option value. Returns the option */ setChoice ( index ) { assert ( index >= 0 && index < this . fields . length ) ; this . choice . uneval ( ) ; this . choiceIndex = index ; return this . fields [ index ] ; } /** initialize to first value */ isBlank ( ) { return ( this . choiceIndex === 0 && this . choice . value ! . isBlank ( ) ) ; } /** evaluate */ eval ( ) { if ( ! this . analyzing ) { this . choice . eval ( ) ; return ; } this . fields . forEach ( option => { assert ( option . io === 'input' && option . conditional ) ; } ) ; this . fields [ 0 ] . eval ( ) ; this . fields . slice ( 1 ) . forEach ( option => { if ( option . evaluated || option . deferral ) { return ; } option . evaluated = undefined ; option . deferral = ( ( ) => { option . eval ( ) ; } ) ; this . workspace . analysisQueue . push ( option ) ; } ) ; } /** value equality */ equals ( other ) { return ( other instanceof Choice && this . choiceIndex == other . choiceIndex && this . choice . equals ( other . choice ) ) ; } copy ( srcPath , dstPath ) { let to = super . copy ( srcPath , dstPath ) ; to . choiceIndex = this . choiceIndex ; return to ; } dump ( ) { return { [ this . choice . name ! ] : this . choice . dump ( ) } ; } }