'use strict' ; import { JSEDITransaction } from './JSEDINotation' ; import { X12Segment } from './X12Segment' ; import { STSegmentHeader } from './X12SegmentHeader' ; import { X12TransactionMap } from './X12TransactionMap' ; import { defaultSerializationOptions , X12SerializationOptions } from './X12SerializationOptions' ; export class X12Transaction { description param { X12SerializationOptions } options constructor ( options ? ) { this . segments = new Array < X12Segment > ( ) ; this . options = defaultSerializationOptions ( options ) ; } header : X12Segment ; trailer : X12Segment ; segments : X12Segment [ ] ; options : X12SerializationOptions ; description param { string [ ] } elements setHeader ( elements ) { this . header = new X12Segment ( STSegmentHeader . tag , this . options ) ; this . header . setElements ( elements ) ; this . _setTrailer ( ) ; } description param { string } tag param { string [ ] } elements returns { X12Segment } addSegment ( tag , elements ) { const segment = new X12Segment ( tag , this . options ) ; segment . setElements ( elements ) ; this . segments . push ( segment ) ; this . trailer . replaceElement ( `${ this . segments . length + 2 }` , 1 ) ; return segment ; } description param { object } input param { object } map param { object } macro fromObject ( input , map , macro ? ) { const mapper = new X12TransactionMap ( map , this , this . options . txEngine ) ; mapper . fromObject ( input , macro ) ; } description param { object } map param { Function } helper returns { object } toObject ( map , helper ? ) { const mapper = new X12TransactionMap ( map , this , helper ) ; return mapper . toObject ( ) ; } 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 . segments . length ; i ++ ) { edi += this . segments [ i ] . toString ( options ) ; if ( options . format ) { edi += options . endOfLine ; } } edi += this . trailer . toString ( options ) ; return edi ; } description returns { object } toJSON ( ) { const jsen = new JSEDITransaction ( this . header . elements . map ( x => x . value ) ) ; this . segments . forEach ( segment => { jsen . addSegment ( segment . tag , segment . elements . map ( x => x . value ) ) ; } ) ; return jsen as object ; } private description private _setTrailer ( ) { this . trailer = new X12Segment ( STSegmentHeader . trailer , this . options ) ; this . trailer . setElements ( [ `${ this . segments . length + 2 }` , this . header . valueOf ( 2 ) ] ) ; } }