/* eslint-env node */
'use strict';
const Plugin = require('broccoli-plugin');
const path = require('path');

const RSVP = require('rsvp');
const mkdirp = require('mkdirp');
const clone = require('clone');
const fs = require('fs');

const { Bundler  } = require('polymer-bundler');
const parse5 = require('parse5');

module.exports = ElementBundler;
ElementBundler.prototype = Object.create(Plugin.prototype);
ElementBundler.prototype.constructor = ElementBundler;

function ElementBundler(inputTree, options) {
  if (!(this instanceof ElementBundler)) {
    return new ElementBundler(inputTree, options);
  }

  // Clone the options otherwise any alterations after this writer is called
  // will be picked up.
  this.options = clone(options) || {};
  this.inputTree = inputTree;
  this.outputFilepath = options.output || path.basename(options.input);
  Plugin.call(this, [inputTree]);
}

ElementBundler.prototype.bundle = function (options) {
  return new RSVP.Promise(function (resolve, reject) {
    mkdirp(path.dirname(options.output), function (error) {
      if (error) {
        reject(error);
      }

      // console.log(options);
      // console.log('relative:');
      // console.log('from:', options.input);
      // console.log('to:', process.cwd());
      // console.log(path.relative(process.cwd(), options.input));

      let inputPath = path.join('.', path.relative(process.cwd(), options.input));

      const bundler = new Bundler(options);
      bundler.generateManifest([ options.input ]).then((manifest) => {
        bundler.bundle(manifest).then((result) => {
          let html = parse5.serialize(result.documents.get(path.basename(options.input)).ast);

          fs.writeFileSync(options.output, html);
          resolve();
        }, (error) => reject(error)).catch((error) => reject(error));
      }, (error) => reject(error)).catch((error) => reject(error));
    });
  });
};

ElementBundler.prototype.build = function () {
  // We have to clone options again as bundle changes the hash which causes
  // the hash grow when called repeatedly.
  let options = clone(this.options);
  options.output = path.join(this.outputPath, this.outputFilepath);

  options.input = path.join(this.inputPaths[0], options.input);

  return this.bundle(options);
};

@@ -271,24 +271,87 @@
 -bundler');%0A
+const %7B Analyzer, FSUrlLoader %7D = require('polymer-analyzer');%0A
 const parse5
@@ -1027,25 +1027,16 @@
 Promise(
-function 
 (resolve
@@ -1044,16 +1044,19 @@
  reject)
+ =%3E
  %7B%0A    m
@@ -1094,25 +1094,16 @@
 ut),
- function
  (error)
  %7B%0A 
@@ -1090,32 +1090,35 @@
 output), (error)
+ =%3E
  %7B%0A      if (err
@@ -1168,29 +1168,29 @@
  // 
-console.log(options);
+Some debugging lines.
 %0A   
@@ -1211,19 +1211,15 @@
 log(
-'relative:'
+options
 );%0A 
@@ -1217,32 +1217,35 @@
 tions);%0A      //
+ //
  console.log('fr
@@ -1288,18 +1288,27 @@
 le.log('
-to
+process.cwd
 :', proc
@@ -1315,24 +1315,113 @@
 ess.cwd());%0A
+      // console.log('inputTree = ', this.inputTree);%0A      // console.log('relative:');%0A
       // con
@@ -1475,22 +1475,27 @@
 nput));%0A
-%0A
+ 
      
+// //
  let inp
@@ -1574,44 +1574,540 @@
     
+ //
  cons
-t bundler = new Bundler(options
+ole.log('trying to read file!');%0A      // fs.readFile(options.input, 'utf8', (err, data) =%3E %7B%0A      //   if (err) throw err;%0A%0A      //   console.log('data=', data);%0A      // %7D);%0A%0A      // FIXME do not use process.cwd to get relative project location, but an options parameter instead.%0A      let relative = path.relative(process.cwd(), options.input);%0A      const analyzer = new Analyzer(%7B%0A        urlLoader: new FSUrlLoader(path.resolve(process.cwd()))%0A      %7D);%0A%0A      const bundler = new Bundler(%7B%0A        analyzer%0A      %7D
 );%0A 
@@ -2138,29 +2138,24 @@
 ifest(%5B 
-options.input
+relative
  %5D).then
@@ -2286,36 +2286,16 @@
 get(
-path.basename(options.input)
+relative
 ).as

Fix unable to get document warning! 🔨