import { Block , BlockClass , isBlockClass } from "../../BlockTree" ; import { isFalseCondition , isTrueCondition } from "../ElementAnalysis" ; import { ErrorCallback , Validator } from "./Validator" ; param err export const classPairsValidator = ( analysis , _templateAnalysis , err ) => { let classPerBlock = new Map ( ) ; for ( let container of analysis . classesFound ( false ) ) { if ( isBlockClass ( container ) ) { if ( analysis . isFromComposition ( container ) ) { continue ; } for ( let block of checkExisting ( classPerBlock , classPerBlock , container , err ) ) { classPerBlock . set ( block , container ) ; } } } for ( let dyn of analysis . dynamicClasses ) { let trueBlocks = new Map < Block , BlockClass > ( ) ; if ( isTrueCondition ( dyn ) ) { for ( let container of dyn . whenTrue ) { if ( analysis . isFromComposition ( container ) ) { continue ; } if ( isBlockClass ( container ) ) { let blocks = checkExisting ( classPerBlock , trueBlocks , container , err ) ; for ( let block of blocks ) { trueBlocks . set ( block , container ) ; } } } } let falseBlocks = new Map < Block , BlockClass > ( ) ; if ( isFalseCondition ( dyn ) ) { for ( let container of dyn . whenFalse ) { if ( isBlockClass ( container ) ) { if ( analysis . isFromComposition ( container ) ) { continue ; } let blocks = checkExisting ( classPerBlock , falseBlocks , container , err ) ; for ( let block of blocks ) { trueBlocks . set ( block , container ) ; } } } } for ( let [ block , container ] of trueBlocks . entries ( ) ) { classPerBlock . set ( block , container ) ; } for ( let [ block , container ] of falseBlocks . entries ( ) ) { classPerBlock . set ( block , container ) ; } } } ; function checkExisting ( classPerBlock , tmpClassPerBlock , container , err ) { let blocks = new Array < Block > ( ) ; let mainBlock = container . block ; let blockHierarchy = mainBlock . getAncestors ( ) ; blockHierarchy . unshift ( mainBlock ) ; for ( let block of blockHierarchy ) { let otherClass = classPerBlock . get ( block ) || tmpClassPerBlock . get ( block ) ; if ( otherClass ) { err ( `Classes "${ container . name }" and "${ otherClass . name }" from the same block${ block !== mainBlock ? " hierarchy" : "" } are not allowed on the same element at the same time.` ) ; } else { blocks . push ( block ) ; } } return blocks ; }