import { Fragment } from "htmlbars/compiler/fragment";
import { compileAST } from "htmlbars/compiler/compile";
import { HydrationCompiler } from "htmlbars/compiler/hydration";
import { Hydration2 } from "htmlbars/compiler/hydration2";
import { Compiler2 } from "htmlbars/compiler/pass2";
import { Range } from "htmlbars/runtime/range";
import { preprocess } from "htmlbars/parser";

function equalHTML(fragment, html) {
  var div = document.createElement("div");
  div.appendChild(fragment.cloneNode(true));

  QUnit.push(div.innerHTML === html, div.innerHTML, html);
}

var dom = {
  createDocumentFragment: function () {
    return document.createDocumentFragment();
  },
  createElement: function (name) {
    return document.createElement(name);
  },
  appendText: function (element, string) {
    element.textContent = string;
  }
}

function hydrationFor(html, options) {
  var ast = preprocess(html, options),
      fragment = new Fragment(options),
      compiler2 = new Compiler2(options);
  fragment.compile(ast);
  var template = compiler2.compile(fragment.opcodes, {
    children: fragment.children
  })(dom);
  var hydration = new HydrationCompiler(compileAST, options);
  hydration.compile(ast);
  return {
    fragment: template({}),
    opcodes: hydration.opcodes
  };
}

module('fragment');

test('compiles a fragment', function () {
  var hydration = hydrationFor("<div>{{foo}} bar {{baz}}</div>");

  equalHTML(hydration.fragment, "<div> bar </div>");
});

test('hydrates a fragment', function () {
  var hydration = hydrationFor("<div>{{foo blah bar=baz}} bar {{baz}}</div>");
  var clone = hydration.fragment.cloneNode(true);
  var hydrate2 = new Hydration2();
  var program = hydrate2.compile(hydration.opcodes)(Range);
  var mustaches = program(clone);

  equal(mustaches.length, 2);

  mustaches[0][2].range.appendChild(document.createTextNode('A'));
  mustaches[1][2].range.appendChild(document.createTextNode('B'));

  equalHTML(clone, "<div>A bar B</div>");
});

@@ -1801,16 +1801,386 @@
 h, 2);%0A%0A
+  equal(mustaches%5B0%5D%5B0%5D, %22foo%22);%0A  deepEqual(mustaches%5B0%5D%5B1%5D, %5B%5B%22blah%22%5D%5D);%0A  deepEqual(mustaches%5B0%5D%5B2%5D.types, %5B%22id%22%5D);%0A  deepEqual(mustaches%5B0%5D%5B2%5D.hash, %7Bbar:%5B%22baz%22%5D%7D);%0A  deepEqual(mustaches%5B0%5D%5B2%5D.hashTypes, %7Bbar:%22id%22%7D);%0A  equal(mustaches%5B0%5D%5B2%5D.escaped, true);%0A%0A  equal(mustaches%5B1%5D%5B0%5D, %22baz%22);%0A  deepEqual(mustaches%5B1%5D%5B1%5D, %5B%5D);%0A  equal(mustaches%5B1%5D%5B2%5D.escaped, true);%0A%0A
   mustac

add more tests