import { prettyPrintXML, platform } from 'substance'

export default class ArticleSerializer {
  export (doc, config) {
    let articleConfig = config.getConfiguration('article')

    // TODO: depending on the article type we should use a specifc exporter
    let exporter = articleConfig.createExporter('jats')
    let res = exporter.export(doc)
    let jats = res.jats

    // TODO: and depending on the article type we should use a specific transformation
    let transformation = articleConfig.getTransformation('jats')
    transformation.export(jats)

    // for the purpose of debugging
    if (platform.inBrowser) {
      console.info('saving jats', jats.getNativeElement())
    }

    return prettyPrintXML(jats)
  }
}

@@ -45,16 +45,76 @@
 bstance'
+%0Aimport %7B DEFAULT_JATS_SCHEMA_ID %7D from './ArticleConstants'
 %0A%0Aexport
@@ -244,377 +244,854 @@
  // 
-TODO: depending on the article type we should use a specifc exporter%0A    let exporter = articleConfig.createExporter('jats')%0A    let res = exporter.export(doc)%0A    let jats = res.jats%0A
+EXPERIMENTAL: I am not sure yet, if this is the right way to use%0A    // exportes and transformers.%0A    // During import we store the original docType on the document instance%0A    // Now we use this to create the corresponding exporter%0A    let docType = doc.docType %7C%7C DEFAULT_JATS_SCHEMA_ID%0A%0A    let exporter = articleConfig.createExporter(docType, doc)%0A    if (!exporter) %7B%0A      console.error(%60No exporter registered for %22$%7BdocType%7D%22. Falling back to default JATS importer, but with unpredictable result.%60)
 %0A    
+  
 // 
-TODO: and depending on the article type we should use a specific transformation%0A    let transformation = articleConfig.getTransformation('jats')%0A    transformation.export(jats)%0A
+Falling back to default importer%0A      exporter = articleConfig.createExporter('jats', doc)%0A    %7D%0A    let res = exporter.export(doc)%0A    let jats = res.jats%0A%0A    let transformation = articleConfig.getTransformation(docType)%0A    if (transformation) %7B%0A      transformation.export(jats)%0A    %7D%0A%0A    let xmlStr = prettyPrintXML(jats)
 %0A   
@@ -1186,16 +1186,22 @@
 g jats',
+ %7B el:
  jats.ge
@@ -1216,16 +1216,31 @@
 lement()
+, xml: xmlStr %7D
 )%0A    %7D%0A
@@ -1251,35 +1251,21 @@
  return 
-prettyPrintXML(jats)
+xmlStr
 %0A  %7D%0A%7D%0A

Use the original docType to create a corresponding exporter.