'use strict' ; import type DataEventEmitter from '../DataEventEmitter' ; import type JSON from '../../Core/JSON' ; import DataModifier from './DataModifier.js' ; import DataTable from '../DataTable.js' ; import U from '../../Core/Utilities.js' ; const { merge } = U ; private class GroupModifier extends DataModifier { /**
     * Default options to group table rows.
     */ public static readonly defaultOptions : GroupModifier . Options = { modifier : 'Group' , groupColumn : '' } ; param { GroupModifier . Options } options public constructor ( options ? ) { super ( ) ; this . options = merge ( GroupModifier . defaultOptions , options ) ; } /**
     * Options of the group modifier.
     */ public options : GroupModifier . Options ; param { DataTable } table param { DataEventEmitter . EventDetail } eventDetail return { DataTable } public modifyTable < T extends DataTable > ( table , eventDetail ? ) { const modifier = this ; modifier . emit ( { type : 'modify' , detail : eventDetail , table } ) ; const byGroups = [ ] , tableGroups = [ ] , valueGroups = [ ] , groupColumn = ( modifier . options . groupColumn || table . getColumnNames ( ) [ 0 ] ) , valueColumn = ( table . getColumn ( groupColumn ) || [ ] ) , { invalidValues , validValues } = modifier . options , modified = table . modified = table . clone ( true , eventDetail ) ; let value , valueIndex ; for ( let i = 0 , iEnd = valueColumn . length ; i < iEnd ; ++ i ) { value = valueColumn [ i ] ; if ( typeof value !== 'undefined' ) { if ( value instanceof DataTable || ( invalidValues && invalidValues . indexOf ( value ) >= 0 ) || ( validValues && validValues . indexOf ( value ) === - 1 ) ) { continue ; } valueIndex = valueGroups . indexOf ( value ) ; if ( valueIndex === - 1 ) { const newTable = new DataTable ( ) ; newTable . setRows ( [ table . getRowObject ( i ) || { } ] ) ; byGroups . push ( groupColumn ) ; tableGroups . push ( newTable ) ; valueGroups . push ( value ) ; } else { tableGroups [ valueIndex ] . setRows ( [ table . getRow ( i ) || [ ] ] ) ; } } } modified . deleteColumns ( ) ; modified . setColumns ( { groupBy : byGroups , table : tableGroups , value : valueGroups } ) ; modifier . emit ( { type : 'afterModify' , detail : eventDetail , table } ) ; return table ; } } /**
 * Additionally provided types for modifier events and options, and JSON
 * conversion.
 */ namespace GroupModifier { /**
     * Options to configure the modifier.
     */ export interface Options extends DataModifier . Options { /**
         * Column to group by values.
         */ groupColumn : string ; /**
         * Array of invalid group values.
         */ invalidValues ? : Array < JSON . Primitive > ; /**
         * Array of valid group values.
         */ validValues ? : Array < JSON . Primitive > ; } } DataModifier . addModifier ( GroupModifier ) ; declare module './ModifierType' { interface ModifierTypeRegistry { Group : typeof GroupModifier ; } } export default GroupModifier ;