import {SitemapTransformer} from './sitemap_transformer';
import {Url} from '../url';
import {config} from '../config';
import {extend} from 'underscore';
const libxml = require('libxmljs');

const DEFAULT_OPTIONS = {
  urlTag: 'url'
};
export class XmlSource extends SitemapTransformer {
  constructor(options) {
    super(options);
    let myConfig = extend({}, DEFAULT_OPTIONS, options.options);
    this.parser = new libxml.SaxPushParser();
    this.parser.on("startElementNS", (element) => {
      if (element === myConfig.urlTag) {
        this.url = {};
      } else if (this.url) {
        this.url.nextAttribute = element;
      }
    }).on("endElementNS", (element) => {
      if (element === myConfig.urlTag) {
        try {
          delete this.url.nextAttribute;
          if (this.url.lastModified) {
            //config.log.trace(`PARSING ${this.url.lastModified} as a date`);
            this.url.lastModified = Date.parse(this.url.lastModified);
          }
          if (this.url.priority) {
            config.log.trace(`PARSING ${this.url.priority} as a float`);
            this.url.priority = parseFloat(this.url.priority);
          }
          let url = this._decorateUrl(new Url(this.url));
          if (url) {
            this.push(url);
          }
        } catch (err) {
          this.emit("error", err);
        }
        this.url = null;
      }
    }).on("characters", (text) => {
      if (this.url && this.url.nextAttribute) {
        switch(this.url.nextAttribute) {
          case 'lastmod':
            this.url.lastModified = `${this.url.lastModified || ''}${text}`.trim();
            break;
          case 'changefreq':
            this.url.changeFrequency = `${this.url.changeFrequency || ''}${text}`.trim();
            break;
          case 'priority':
            this.url.priority = `${this.url.priority || ''}${text}`.trim();
            break;
          case 'loc':
            this.url.url = `${this.url.url || ''}${text}`.trim();
            break;
        }
      }
    }).on("error", (err) => {
      this.emit("error", err);
    });
  }
  _transform(chunk, encoding, callback) {
    this.parser.push(chunk);
    callback();
  }
  open() {
    super.open();
    if (this.inputStream) {
      this.inputStream.pipe(this);
    }
  }
}

@@ -226,16 +226,153 @@
 g: 'url'
+,%0A  urlAttributes: %7B%0A    'lastmod': 'lastModified',%0A    'changefreq': 'changeFrequency',%0A    'priority': 'priority',%0A    'loc': 'url'%0A  %7D
 %0A%7D;%0Aexpo
@@ -1607,306 +1607,68 @@
     
-switch(this.url.nextAttribute) %7B%0A          case 'lastmod':%0A            this.url.lastModified = %60$%7Bthis.url.lastModified %7C%7C ''%7D$%7Btext%7D%60.trim();%0A            break;%0A          case 'changefreq':%0A            this.url.changeFrequency = %60$%7Bthis.url.changeFrequency %7C%7C ''%7D$%7Btext%7D%60.trim();%0A            break
+let urlAttr = myConfig.urlAttributes%5Bthis.url.nextAttribute%5D
 ;%0A  
@@ -1677,146 +1677,23 @@
     
- 
- case 'priority':%0A            this.url.priority = %60$%7Bthis.url.priority %7C%7C ''%7D$%7Btext%7D%60.trim();%0A            break;%0A          case 'loc':%0A  
+if (urlAttr) %7B%0A
     
@@ -1706,20 +1706,25 @@
 this.url
-.
+%5B
 url
+Attr%5D
  = %60$%7Bth
@@ -1729,20 +1729,25 @@
 this.url
-.
+%5B
 url
+Attr%5D
  %7C%7C ''%7D$
@@ -1766,27 +1766,8 @@
 ();%0A
-            break;%0A
     

Allow mapping of xml tags to url attributes via configuration