import { PageMaker } from '../types' ; import { Page } from '../book' ; const indexOfNextReorderablePage = ( pages , startIndex ) => { for ( let i = startIndex ; i < pages . length ; i += 1 ) { const pg = pages [ i ] ; if ( ! pg . isOutOfFlow && ! pg . avoidReorder ) return i ; } return null ; } ; const orderPages = ( pages , makeNewPage ) => { const orderedPages = pages . slice ( ) ; for ( let i = 0 ; i < orderedPages . length ; i += 1 ) { const page = orderedPages [ i ] ; const isLeft = i % 2 !== 0 ; if ( ( isLeft && page . alwaysRight ) || ( ! isLeft && page . alwaysLeft ) ) { if ( page . isOutOfFlow ) { const indexToSwap = indexOfNextReorderablePage ( orderedPages , i + 1 ) ; if ( ! indexToSwap ) { break ; } const pageToMoveUp = orderedPages [ indexToSwap ] ; orderedPages . splice ( indexToSwap , 1 ) ; orderedPages . splice ( i , 0 , pageToMoveUp ) ; } else { orderedPages . splice ( i , 0 , makeNewPage ( ) ) ; } } } return orderedPages ; } ; export default orderPages ;