'use strict' ; import { JSEDINotation } from './JSEDINotation' ; import { X12Interchange } from './X12Interchange' ; import { X12Parser } from './X12Parser' ; import { defaultSerializationOptions , X12SerializationOptions } from './X12SerializationOptions' ; export class X12Generator { description param { JSEDINotation } jsen param { X12SerializationOptions } options constructor ( jsen ? , options ? ) { this . jsen = jsen === undefined ? new JSEDINotation ( ) : jsen ; if ( typeof jsen === 'object' && jsen . options !== undefined ) { this . options = defaultSerializationOptions ( jsen . options ) ; } if ( options !== undefined || this . options === undefined ) { this . options = defaultSerializationOptions ( options ) ; } this . interchange = new X12Interchange ( this . options ) ; } private jsen : JSEDINotation ; private interchange : X12Interchange ; private options : X12SerializationOptions ; description param { JSEDINotation } jsen setJSEDINotation ( jsen ) { this . jsen = jsen ; } description returns { JSEDINotation } getJSEDINotation ( ) { return this . jsen ; } description param { X12SerializationOptions } options setOptions ( options ) { this . options = defaultSerializationOptions ( options ) ; } description returns { X12SerializationOptions } getOptions ( ) { return this . options ; } description returns { X12Interchange } validate ( ) { this . _generate ( ) ; return new X12Parser ( true ) . parse ( this . interchange . toString ( this . options ) ) as X12Interchange ; } description returns { string } toString ( ) { return this . validate ( ) . toString ( this . options ) ; } private description private _generate ( ) { const genInterchange = new X12Interchange ( this . options ) ; genInterchange . setHeader ( this . jsen . header ) ; this . jsen . functionalGroups . forEach ( functionalGroup => { const genFunctionalGroup = genInterchange . addFunctionalGroup ( ) ; genFunctionalGroup . setHeader ( functionalGroup . header ) ; functionalGroup . transactions . forEach ( transaction => { const genTransaction = genFunctionalGroup . addTransaction ( ) ; genTransaction . setHeader ( transaction . header ) ; transaction . segments . forEach ( segment => { genTransaction . addSegment ( segment . tag , segment . elements ) ; } ) ; } ) ; } ) ; this . interchange = genInterchange ; } }