import { max } from '../../utility/kit';

export class Matrix {
    constructor(isDataRow) {
        this.isDataRow = isDataRow;
    }

    build(table) {
        const isDataRow = this.isDataRow;
        const rows = table.rows;
        const matrix = [];

        const cursor = new Array(rows.length);
        cursor.fill(0);

        let currentRow = 0;
        for (let i = 0, rowsLength = rows.length; i < rowsLength; i++) {
            const tr = rows[i];
            if (!isDataRow(tr)) {
                continue;
            }

            const cells = tr.cells;
            for (let j = 0, cellsLength = cells.length; j < cellsLength; j++) {
                const td = cells[j];

                const colSpan = td.colSpan;
                const rowSpan = td.rowSpan;

                for (let k = 0; k < rowSpan; k++) {
                    const rowIndex = currentRow + k;
                    const colIndex = cursor[rowIndex];
                    let cursorRow = matrix[rowIndex];
                    if (!cursorRow) {
                        cursorRow = [];
                        matrix[rowIndex] = cursorRow;
                    }

                    for (let m = 0; m < colSpan; m++) {
                        cursorRow[colIndex + m] = td;
                    }

                    cursor[rowIndex] = colIndex + colSpan;
                }
            }

            currentRow++;
        }

        return matrix;
    }
}
@@ -33,16 +33,71 @@
 ty/kit';
+%0Aimport %7B AppError %7D from '../../infrastructure/error';
 %0A%0Aexport
@@ -320,22 +320,22 @@
   const 
-cursor
+offset
  = new A
@@ -361,22 +361,22 @@
         
-cursor
+offset
 .fill(0)
@@ -393,23 +393,19 @@
  let cur
-rentRow
+sor
  = 0;%0A  
@@ -426,26 +426,22 @@
  i = 0, 
-rowsLength
+height
  = rows.
@@ -456,18 +456,14 @@
 i %3C 
-rowsLength
+height
 ; i+
@@ -638,25 +638,19 @@
  j = 0, 
-cellsLeng
+wid
 th = cel
@@ -668,17 +668,11 @@
 j %3C 
-cellsLeng
+wid
 th; 
@@ -715,17 +715,16 @@
 lls%5Bj%5D;%0A
-%0A
         
@@ -741,59 +741,26 @@
 nst 
-colSpan = td.colSpan;%0A                const
+%7B colSpan,
  rowSpan
  = t
@@ -759,21 +759,15 @@
 Span
+ %7D
  = td
-.rowSpan
 ;%0A%0A 
@@ -794,16 +794,16 @@
 let 
-k
+y
  = 0; 
-k
+y
  %3C r
@@ -810,17 +810,17 @@
 owSpan; 
-k
+y
 ++) %7B%0A  
@@ -847,33 +847,23 @@
 nst 
-rowIndex = currentRow + k
+yi = cursor + y
 ;%0A  
@@ -890,34 +890,22 @@
 nst 
-colIndex = cursor%5BrowIndex
+xi = offset%5Byi
 %5D;%0A 
@@ -927,23 +927,17 @@
     let 
-cursorR
+r
 ow = mat
@@ -940,24 +940,18 @@
  matrix%5B
-rowIndex
+yi
 %5D;%0A     
@@ -970,23 +970,17 @@
    if (!
-cursorR
+r
 ow) %7B%0A  
@@ -1001,23 +1001,17 @@
         
-cursorR
+r
 ow = %5B%5D;
@@ -1046,27 +1046,15 @@
 rix%5B
-rowIndex%5D = cursorR
+yi%5D = r
 ow;%0A
@@ -1109,16 +1109,16 @@
 let 
-m
+x
  = 0; 
-m
+x
  %3C c
@@ -1125,17 +1125,17 @@
 olSpan; 
-m
+x
 ++) %7B%0A  
@@ -1160,30 +1160,18 @@
     
-cursorRow%5BcolIndex + m
+row%5Bxi + x
 %5D = 
@@ -1221,35 +1221,23 @@
     
-cursor%5BrowIndex%5D = colIndex
+offset%5Byi%5D = xi
  + c
@@ -1296,52 +1296,498 @@
  cur
-rentRow++;%0A        %7D%0A%0A        return matrix;
+sor++;%0A        %7D%0A%0A        this.assertFlatness(matrix);%0A        return matrix;%0A    %7D%0A%0A    assertFlatness(matrix) %7B%0A        if (matrix.length) %7B%0A            const height = matrix.length;%0A            const width = matrix%5B0%5D.length;%0A            for (let i = 1; i %3C height; i++) %7B%0A                if (matrix%5Bi%5D.length !== width) %7B%0A                    throw new AppError('matrix', %60Matrix is not flat, expect width $%7Bwidth%7D, actual $%7Bmatrix%5Bi%5D.length%7D%60);%0A                %7D%0A            %7D%0A        %7D
 %0A   

add flat assert