import { Cell , ExpandableRows , WorksheetError , WorksheetItem } from './types' ; export const mergeCells = < T extends WorksheetItem > ( oldCells , newCells ) => newCells . reduce ( ( accum , newCell ) => { const index = oldCells . findIndex ( ( oldCell ) => oldCell . rowIndex === newCell . rowIndex && oldCell . columnIndex === newCell . columnIndex ) ; if ( index > - 1 ) { accum [ index ] = newCell ; return accum ; } return accum . concat ( newCell ) ; } , [ ... oldCells ] ) ; export const deleteCells = < T extends WorksheetItem > ( oldCells , newCells ) => oldCells . filter ( ( oldCell ) => ! newCells . find ( ( newCell ) => newCell . columnIndex === oldCell . columnIndex && newCell . rowIndex === oldCell . rowIndex ) ) ; export const editedRows = < T extends WorksheetItem > ( editedCells , rows ) => editedCells . reduce < T [ ] > ( ( accum , { rowIndex } ) => { const row = rows [ rowIndex ] ; if ( accum . find ( ( editedRow ) => editedRow === row ) ) { return accum ; } else { return [ ... accum , row ] ; } } , [ ] ) ; export const invalidRows = < T extends WorksheetItem > ( invalidCells , rows ) => { const mapObj = new Map ( ) ; invalidCells . forEach ( ( { rowIndex , hash } ) => { const row = rows [ rowIndex ] ; if ( mapObj . has ( row ) ) { const errors = mapObj . get ( row ) ; mapObj . set ( row , new Set ( [ ... errors , hash ] ) ) ; } else { mapObj . set ( row , new Set ( [ hash ] ) ) ; } } ) ; return Array . from ( mapObj ) . map < WorksheetError < T > > ( ( [ item , errors ] ) => ( { item , errors : Array . from ( errors ) } ) ) ; } ; export const getHiddenRows = ( expandableRows ) => { return Object . values ( expandableRows ) . flat ( ) ; } ;