'use strict' ; import { JSEDINotation } from './JSEDINotation' ; import { X12FunctionalGroup } from './X12FunctionalGroup' ; import { X12Segment } from './X12Segment' ; import { ISASegmentHeader } from './X12SegmentHeader' ; import { defaultSerializationOptions , X12SerializationOptions } from './X12SerializationOptions' ; export class X12Interchange { description param { string | X12SerializationOptions } segmentTerminator param { string } elementDelimiter param { X12SerializationOptions } options constructor ( segmentTerminator ? , elementDelimiter ? , options ? ) { this . functionalGroups = new Array < X12FunctionalGroup > ( ) ; if ( typeof segmentTerminator === 'string' ) { this . segmentTerminator = segmentTerminator ; if ( typeof elementDelimiter === 'string' ) { this . elementDelimiter = elementDelimiter ; } else { throw new TypeError ( 'Parameter "elementDelimiter" must be type of string.' ) ; } } else { this . options = defaultSerializationOptions ( segmentTerminator ) ; this . segmentTerminator = this . options . segmentTerminator ; this . elementDelimiter = this . options . elementDelimiter ; } if ( this . options === undefined ) { this . options = defaultSerializationOptions ( options ) ; } } header : X12Segment ; trailer : X12Segment ; functionalGroups : X12FunctionalGroup [ ] ; segmentTerminator : string ; elementDelimiter : string ; options : X12SerializationOptions ; description param { string [ ] } elements setHeader ( elements ) { this . header = new X12Segment ( ISASegmentHeader . tag , this . options ) ; this . header . setElements ( elements ) ; this . _setTrailer ( ) ; } description param { X12SerializationOptions } options returns { X12FunctionalGroup } addFunctionalGroup ( options ? ) { options = options !== undefined ? defaultSerializationOptions ( options ) : this . options ; const functionalGroup = new X12FunctionalGroup ( options ) ; this . functionalGroups . push ( functionalGroup ) ; this . trailer . replaceElement ( `${ this . functionalGroups . length }` , 1 ) ; return functionalGroup ; } 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 . functionalGroups . length ; i ++ ) { edi += this . functionalGroups [ i ] . toString ( options ) ; if ( options . format ) { edi += options . endOfLine ; } } edi += this . trailer . toString ( options ) ; return edi ; } description returns { JSEDINotation } toJSEDINotation ( ) { const jsen = new JSEDINotation ( this . header . elements . map ( x => x . value . trim ( ) ) , this . options ) ; this . functionalGroups . forEach ( functionalGroup => { const jsenFunctionalGroup = jsen . addFunctionalGroup ( functionalGroup . header . elements . map ( x => x . value ) ) ; functionalGroup . transactions . forEach ( transaction => { const jsenTransaction = jsenFunctionalGroup . addTransaction ( transaction . header . elements . map ( x => x . value ) ) ; transaction . segments . forEach ( segment => { jsenTransaction . addSegment ( segment . tag , segment . elements . map ( x => x . value ) ) ; } ) ; } ) ; } ) ; return jsen ; } description returns { object } toJSON ( ) { return this . toJSEDINotation ( ) as object ; } private description private _setTrailer ( ) { this . trailer = new X12Segment ( ISASegmentHeader . trailer , this . options ) ; this . trailer . setElements ( [ `${ this . functionalGroups . length }` , this . header . valueOf ( 13 ) ] ) ; } }