import assert from "power-assert"
import * as Hash from "../cake-hash"


function getArticleData() {
  return [
    {
      article: {
        id: "1",
        user_id: "1",
        title: "First Article",
        body: "First Article Body"
      },
      user: {
        id: "1",
        user: "mariano",
        password: "5f4dcc3b5aa765d61d8327deb882cf99"
      },
      comment: [
        {
          id: "1",
          article_id: "1",
          user_id: "2",
          comment: "First Comment for First Article"
        },
        {
          id: "2",
          article_id: "1",
          user_id: "4",
          comment: "Second Comment for Second Article"
        }
      ],
      tag: [
        {
          id: "1",
          tag: "tag1"
        },
        {
          id: "2",
          tag: "tag2"
        }
      ],
      deep: {
        nest: {
          test: {
            1: "foo",
            2: {
              and: {more: "stuff"}
            }
          }
        }
      }
    },
    {
      article: {
        id: "2",
        user_id: "1",
        title: "Second Article",
        body: "Second Article Body",
        published: "Y"
      },
      user: {
        id: "2",
        user: "mariano",
        password: "5f4dcc3b5aa765d61d8327deb882cf99"
      },
      comment: [],
      tag: []
    },
    {
      article: {
        id: "3",
        user_id: "1",
        title: "Third Article",
        body: "Third Article Body",
        published: "Y"
      },
      user: {
        id: "3",
        user: "mariano",
        password: "5f4dcc3b5aa765d61d8327deb882cf99"
      },
      comment: [],
      tag: []
    },
    {
      article: {
        id: "4",
        user_id: "1",
        title: "Fourth Article",
        body: "Fourth Article Body",
        published: "Y"
      },
      user: {
        id: "4",
        user: "mariano",
        password: "5f4dcc3b5aa765d61d8327deb882cf99"
      },
      comment: [],
      tag: []
    },
    {
      article: {
        id: "5",
        user_id: "1",
        title: "Fifth Article",
        body: "Fifth Article Body",
        published: "Y"
      },
      user: {
        id: "5",
        user: "mariano",
        password: "5f4dcc3b5aa765d61d8327deb882cf99"
      },
      comment: [],
      tag: []
    }
  ];
}


describe("cake-hash", () => {
  it("get()", () => {
    let data = ["abc", "def"];

    assert(Hash.get(data, "0") === "abc");
    assert(Hash.get(data, 0) === "abc");
    assert(Hash.get(data, "1") === "def");

    data = getArticleData();

    assert(Hash.get([], "1.article.title") == null);
    assert(Hash.get([], "") == null);
    assert(Hash.get([], null, "-") === "-");
    assert(Hash.get(data, "0.article.title") === "First Article");
    assert(Hash.get(data, "1.article.title") === "Second Article");
    assert(Hash.get(data, "5.article.title") == null);

    let defaultValue = ["empty"];
    assert(defaultValue === Hash.get(data, "5.article.title", defaultValue));
    assert(defaultValue === Hash.get([], "5.article.title", defaultValue));

    assert(Hash.get(data, "1.article.title.not_there") == null);
    assert.deepEqual(Hash.get(data, "1.article"), data[1]["article"]);
    assert.deepEqual(Hash.get(data, ["1", "article"]), data[1]["article"]);

    data = {a: {b: {c: {d: 1}}}};
    assert(Hash.get(data, "a.b.c.d") === 1);
  });

  it("extract() - Basic", () => {
    let data = getArticleData();
    assert.deepEqual(Hash.extract(data, ""), data);
    assert(Hash.extract(data, "0.article.title") === "First Article");
    assert(Hash.extract(data, "1.article.title") === "Second Article");
    assert.deepEqual(Hash.extract([false], "{n}.something.another_thing"), []);
  });

  it("extract() - NumericKey", () => {
    let data = getArticleData();
    assert.deepEqual(Hash.extract(data, "{n}.article.title"), [
      "First Article", "Second Article",
      "Third Article", "Fourth Article",
      "Fifth Article"
    ]);
    assert.deepEqual(Hash.extract(data, "0.comment.{n}.user_id"), ["2", "4"]);
  });

  it("extract() - NumericMixedKeys", () => {
    let data = {
      user: {
        0: {
          id: 4,
          name: "Neo"
        },
        1: {
          id: 5,
          name: "Morpheus"
        },
        stringKey: {
          name: "Fail"
        }
      }
    };
    assert.deepEqual(Hash.extract(data, "user.{n}.name"), ["Neo", "Morpheus"]);
  });

  it("extract() - NumericNonZero", () => {
    let data = {
      1: {
        user: {
          id: 1,
          name: "John"
        }
      },
      2: {
        user: {
          id: 2,
          name: "Bob"
        }
      },
      3: {
        user: {
          id: 3,
          name: "Tony"
        }
      }
    };
    assert.deepEqual(Hash.extract(data, "{n}.user.name"), ["John", "Bob", "Tony"]);
  });
});
@@ -847,16 +847,19 @@
     nest
+ing
 : %7B%0A    
@@ -4804,12 +4804,318 @@
 );%0A  %7D);
+%0A%0A  it(%22extract() - StringKey%22, () =%3E %7B%0A    let data = getArticleData();%0A    assert.deepEqual(Hash.extract(data, %22%7Bn%7D.%7Bs%7D.user%22), %5B%0A      %22mariano%22,%0A      %22mariano%22,%0A      %22mariano%22,%0A      %22mariano%22,%0A      %22mariano%22%0A    %5D);%0A    assert.deepEqual(Hash.extract(data, %22%7Bn%7D.%7Bs%7D.nesting.test.1%22), %5B%22foo%22%5D);%0A  %7D);
 %0A%7D);

Add extract() StringKey tests