'use strict' ; import { JSEDIFunctionalGroup } from './JSEDINotation' ; import { X12Segment } from './X12Segment' ; import { GSSegmentHeader } from './X12SegmentHeader' ; import { X12Transaction } from './X12Transaction' ; import { defaultSerializationOptions , X12SerializationOptions } from './X12SerializationOptions' ; export class X12FunctionalGroup { description param { X12SerializationOptions } options constructor ( options ? ) { this . transactions = new Array < X12Transaction > ( ) ; this . options = defaultSerializationOptions ( options ) ; } header : X12Segment ; trailer : X12Segment ; transactions : X12Transaction [ ] ; options : X12SerializationOptions ; description param { string [ ] } elements setHeader ( elements ) { this . header = new X12Segment ( GSSegmentHeader . tag , this . options ) ; this . header . setElements ( elements ) ; this . _setTrailer ( ) ; } description returns { X12Transaction } addTransaction ( ) { const transaction = new X12Transaction ( this . options ) ; this . transactions . push ( transaction ) ; this . trailer . replaceElement ( `${ this . transactions . length }` , 1 ) ; return transaction ; } description param { X12SerializationOptions } options returns { string } toString ( options ? ) { options = options !== undefined ? defaultSerializationOptions ( options ) : this . options ; let edi = this . header . toString ( options ) ; if ( options . format ) { edi += options . endOfLine ; } for ( let i = 0 ; i < this . transactions . length ; i ++ ) { edi += this . transactions [ i ] . toString ( options ) ; if ( options . format ) { edi += options . endOfLine ; } } edi += this . trailer . toString ( options ) ; return edi ; } description returns { object } toJSON ( ) { const jsen = new JSEDIFunctionalGroup ( this . header . elements . map ( x => x . value ) ) ; this . transactions . forEach ( transaction => { const jsenTransaction = jsen . addTransaction ( transaction . header . elements . map ( x => x . value ) ) ; transaction . segments . forEach ( segment => { jsenTransaction . addSegment ( segment . tag , segment . elements . map ( x => x . value ) ) ; } ) ; } ) ; return jsen as object ; } private description private _setTrailer ( ) { this . trailer = new X12Segment ( GSSegmentHeader . trailer , this . options ) ; this . trailer . setElements ( [ `${ this . transactions . length }` , this . header . valueOf ( 6 ) ] ) ; } }