import { DialogRef } from './dialog-ref' ; const BASKET_GROUP = { } ; /**
 * A dumb stack implementation over an array.
 */ export class DialogRefStack < T > { private _stack : DialogRef < T > [ ] ; private _stackMap : Map < DialogRef < T > , any > ; get length ( ) : number { return this . _stack . length ; } constructor ( ) { this . _stack = [ ] ; this . _stackMap = new Map < DialogRef < T > , any > ( ) ; } closeAll ( result = null ) { for ( let i = 0 , len = this . _stack . length ; i < len ; i ++ ) { this . _stack . pop ( ) . close ( result ) ; } } push ( dialogRef , group ? ) { if ( this . _stack . indexOf ( dialogRef ) === - 1 ) { this . _stack . push ( dialogRef ) ; this . _stackMap . set ( dialogRef , group || BASKET_GROUP ) ; } } param dialogRef pushManaged ( dialogRef , group ? ) { this . push ( dialogRef , group ) ; dialogRef . onDestroy . subscribe ( ( ) => this . remove ( dialogRef ) ) ; } pop ( ) { const dialogRef = this . _stack . pop ( ) ; this . _stackMap . delete ( dialogRef ) ; return dialogRef ; } param dialogRef remove ( dialogRef ) { let idx = this . indexOf ( dialogRef ) ; if ( idx > - 1 ) { this . _stack . splice ( idx , 1 ) ; this . _stackMap . delete ( dialogRef ) ; } } index ( index ) { return this . _stack [ index ] ; } indexOf ( dialogRef ) { return this . _stack . indexOf ( dialogRef ) ; } groupOf ( dialogRef ) { return this . _stackMap . get ( dialogRef ) ; } groupBy ( group ) { let arr = [ ] ; if ( group ) { this . _stackMap . forEach ( ( value , key ) => { if ( value === group ) { arr . push ( key ) ; } } ) ; } return arr ; } groupLength ( group ) { let count = 0 ; if ( group ) { this . _stackMap . forEach ( ( value ) => { if ( value === group ) { count ++ ; } } ) ; } return count ; } }