import { Position } from "./position" ; import { PAGE_HEIGHT , PAGE_WIDTH } from "../../plugy-stash/dimensions" ; import { LayoutItem , LayoutResult } from "./types" ; function runeIndex ( rune ) { return Number ( rune . code . slice ( 1 ) ) ; } export function runesLayout < T extends LayoutItem > ( [ runes , ] ) { const positions = new Map < T , Position > ( ) ; runes . sort ( ( a , b ) => runeIndex ( a ) - runeIndex ( b ) ) ; const groups = [ ] ; let currentRune = 0 ; for ( let i = 1 ; i < 34 ; i ++ ) { const group = [ ] ; while ( currentRune < runes . length && runeIndex ( runes [ currentRune ] ) === i ) { group . push ( runes [ currentRune ] ) ; currentRune ++ ; } groups . push ( group ) ; } let currentPage = 0 ; let row = - 2 ; let col = 0 ; function positionGroup ( group , fullPage ) { for ( const item of group ) { if ( col + 1 > PAGE_WIDTH ) { row ++ ; col = 0 ; } if ( row + 1 > PAGE_HEIGHT ) { currentPage ++ ; row = 0 ; col = 0 ; if ( ! fullPage ) { return positionGroup ( group , true ) ; } } positions . set ( item , { page : currentPage , rows : [ row ] , cols : [ col ] } ) ; col ++ ; } } for ( const group of groups ) { row += 2 ; col = 0 ; positionGroup ( group , row === 0 ) ; } return { nbPages : currentPage + 1 , positions } ; }