import { Atom } from '../core/atom' ; import type { ModelPrivate } from './model-private' ; import { contentDidChange } from './listeners' ; import { register as registerCommand } from '../editor/commands' ; import { arrayIndex , arrayCell } from './array-utils' ; import { Style } from '../public/core' ; export * from './array-utils' ; /**
 * Join all the cells at the indicated row into a single list of atoms
 */ export function arrayJoinColumns ( row , separator = ',' , style ? ) { if ( ! row ) return [ ] ; let result = [ new Atom ( 'first' ) ] ; let sep = null ; for ( let cell of row ) { if ( cell && cell . length > 0 && cell [ 0 ] . type === 'first' ) { cell = cell . slice ( 1 ) ; } if ( cell && cell . length > 0 ) { if ( sep ) { result . push ( sep ) ; } else { sep = new Atom ( 'mpunct' , { value : separator , style } ) ; } result = result . concat ( cell ) ; } } return result ; } /**
 * Join all the rows into a single atom list
 */ export function arrayJoinRows ( array , separators = [ ';' , ',' ] , style ? ) { let result = [ new Atom ( 'first' ) ] ; let sep = null ; for ( const row of array ) { if ( sep ) { result . push ( sep ) ; } else { sep = new Atom ( 'mpunct' , { value : separators [ 0 ] , style } ) ; } result = result . concat ( arrayJoinColumns ( row , separators [ 1 ] ) ) ; } return result ; } /**
 * Return the number of non-empty cells in that column
 */ export function arrayColumnCellCount ( array , col ) { let result = 0 ; const colRow = { col , row : 0 } ; while ( colRow . row < array . length ) { const cell = arrayCell ( array , colRow ) ; if ( cell && cell . length > 0 ) { let cellLength = cell . length ; if ( cell [ 0 ] . type === 'first' ) cellLength -= 1 ; if ( cellLength > 0 ) { result += 1 ; } } colRow . row += 1 ; } return result ; } /**
 * Remove the indicated column from the array
 */ export function arrayRemoveColumn ( array , col ) { let row = 0 ; while ( row < array . length ) { if ( array [ row ] [ col ] ) { array [ row ] . splice ( col , 1 ) ; } row += 1 ; } } /**
 * Remove the indicated row from the array
 */ export function arrayRemoveRow ( array , row ) { array . splice ( row , 1 ) ; } /**
 * Return the first non-empty cell, row by row
 */ export function arrayFirstCellByRow ( array ) { const colRow = { col : 0 , row : 0 } ; while ( colRow . row < array . length && ! arrayCell ( array , colRow ) ) { colRow . row += 1 ; } return arrayCell ( array , colRow ) ? `cell${ arrayIndex ( array , colRow ) }` : '' ; } /**
 * Internal primitive to add a column/row in a matrix
 */ function addCell ( _model , _where ) { } export function convertParentToArray ( _model ) { } export function addRowAfter ( model ) { convertParentToArray ( model ) ; addCell ( model , 'after row' ) ; contentDidChange ( model ) ; return true ; } export function addRowBefore ( model ) { convertParentToArray ( model ) ; addCell ( model , 'before row' ) ; contentDidChange ( model ) ; return true ; } export function addColumnAfter ( model ) { convertParentToArray ( model ) ; addCell ( model , 'after column' ) ; contentDidChange ( model ) ; return true ; } export function addColumnBefore ( model ) { convertParentToArray ( model ) ; addCell ( model , 'before column' ) ; contentDidChange ( model ) ; return true ; } registerCommand ( { addRowAfter , addColumnAfter , addRowBefore , addColumnBefore , } , { target : 'model' , category : 'array-edit' } ) ;