"use strict";
var index_1 = require("../index");
var book_1 = require("./book");
var shelf_1 = require("./shelf");
var shop_1 = require("./shop");
var founder_1 = require("./founder");
var enableLogging = false;
function logStore(store) {
    if (enableLogging) {
        store.subscribe(function () {
            console.log("");
            console.log(JSON.stringify(store.getState()));
            console.log("");
        });
    }
    return store;
}
describe("I", function () {
    it("has an initial state available via cursor", function () {
        {
            var store = shop_1.Shop.reduce.store();
            var shop = index_1.snapshot(store);
            var shelf = shop_1.Shop.shelves(shop, "fiction");
            var book_2 = shelf_1.Shelf.books(shelf, 109423);
            book_2(book_1.Book.setTitle("1985"));
            expect(store.getState().shelves.get("fiction").books.get(109423).title).toEqual("1985");
        }
        var book = index_1.snapshot(logStore(book_1.Book.reduce.store())).state;
        expect(book.title).toEqual("");
        expect(book.price).toEqual(0);
        expect(book.authors.count()).toEqual(0);
        expect(JSON.stringify(book)).toEqual("{\"title\":\"\",\"price\":0,\"authors\":[]}");
    });
    it("can be updated via cursors", function () {
        var store = logStore(shelf_1.Shelf.reduce.store());
        var shelf1 = index_1.snapshot(store);
        var shelf2 = shelf1(shelf_1.Shelf.setDescription("Romance"));
        expect(JSON.stringify(shelf2.state)).toEqual("{\"description\":\"Romance\",\"books\":{}}");
        var shelf3 = shelf2(shelf_1.Shelf.books.update(1001, book_1.Book.setTitle("1985")));
        expect(JSON.stringify(shelf3.state)).toEqual("{\"description\":\"Romance\",\"books\":{\"1001\":{\"title\":\"1985\",\"price\":0,\"authors\":[]}}}");
        var firstBook1 = shelf_1.Shelf.books(shelf3, 1001);
        expect(firstBook1.state.title).toEqual("1985");
        expect(firstBook1.state.price).toEqual(0);
        var firstBook2 = firstBook1(book_1.Book.setPrice(5.99));
        expect(JSON.stringify(store.getState())).toEqual("{\"description\":\"Romance\",\"books\":{\"1001\":{\"title\":\"1985\",\"price\":5.99,\"authors\":[]}}}");
        expect(firstBook2.state.price).toEqual(5.99);
    });
    it("supports references and cursors through them", function () {
        var store = logStore(founder_1.Founder.reduce.store());
        var founder1 = index_1.snapshot(store)(founder_1.Founder.setName("Samuel J. Borders"));
        var shop1 = founder_1.Founder.shop(founder1);
        shop1(shop_1.Shop.setName("Borders"));
        expect(store.getState().name).toEqual("Samuel J. Borders");
        expect(store.getState().shop.name).toEqual("Borders");
    });
    it("supports removing items from collections", function () {
        var store = logStore(shelf_1.Shelf.reduce.store());
        var shelf1 = index_1.snapshot(store)(shelf_1.Shelf.setDescription("Romance"));
        expect(JSON.stringify(shelf1.state)).toEqual("{\"description\":\"Romance\",\"books\":{}}");
        var shelf2 = shelf1(shelf_1.Shelf.books.update(1001, book_1.Book.setTitle("1985")))(shelf_1.Shelf.books.update(1002, book_1.Book.setTitle("Indiana Smith")))(shelf_1.Shelf.books.update(1003, book_1.Book.setTitle("Gone With The Runs")));
        ;
        expect(JSON.stringify(shelf2.state)).toEqual("{\"description\":\"Romance\",\"books\":{\"1001\":{\"title\":\"1985\",\"price\":0,\"authors\":[]},\"1002\":{\"title\":\"Indiana Smith\",\"price\":0,\"authors\":[]},\"1003\":{\"title\":\"Gone With The Runs\",\"price\":0,\"authors\":[]}}}");
        var shelf3 = shelf2(shelf_1.Shelf.books.remove(1002));
        expect(JSON.stringify(shelf3.state)).toEqual("{\"description\":\"Romance\",\"books\":{\"1001\":{\"title\":\"1985\",\"price\":0,\"authors\":[]},\"1003\":{\"title\":\"Gone With The Runs\",\"price\":0,\"authors\":[]}}}");
        // Alternatively, remove via cursor
        shelf_1.Shelf.books(shelf3, 1003).remove();
        expect(JSON.stringify(store.getState())).toEqual("{\"description\":\"Romance\",\"books\":{\"1001\":{\"title\":\"1985\",\"price\":0,\"authors\":[]}}}");
    });
    it("supports nested layers of cursors", function () {
        var store = logStore(shop_1.Shop.reduce.store());
        var shop1 = index_1.snapshot(store);
        var shop2 = shop1(shop_1.Shop.setName("Buy the Book, Inc."));
        var shop3 = shop2(shop_1.Shop.shelves.add("ADV"));
        var advShelf1 = shop_1.Shop.shelves(shop3, "ADV");
        expect(advShelf1.state.description).toEqual("");
        var advShelf2 = advShelf1(shelf_1.Shelf.setDescription("Adventure"));
        expect(advShelf2.state.description).toEqual("Adventure");
        var advShelf3 = advShelf2(shelf_1.Shelf.books.update(1002, book_1.Book.setTitle("Indiana Smith")));
        expect(JSON.stringify(store.getState())).toEqual("{\"name\":\"Buy the Book, Inc.\",\"shelves\":{\"ADV\":{\"description\":\"Adventure\",\"books\":{\"1002\":{\"title\":\"Indiana Smith\",\"price\":0,\"authors\":[]}}}}}");
        var firstBook1 = shelf_1.Shelf.books(advShelf3, 1002);
        expect(firstBook1.state.title).toEqual("Indiana Smith");
        var firstBook2 = firstBook1(book_1.Book.setPrice(4.99));
        var firstBook3 = firstBook2(book_1.Book.addAuthor("Jim Orwell"));
        expect(JSON.stringify(store.getState())).toEqual("{\"name\":\"Buy the Book, Inc.\",\"shelves\":{\"ADV\":{\"description\":\"Adventure\",\"books\":{\"1002\":{\"title\":\"Indiana Smith\",\"price\":4.99,\"authors\":[\"Jim Orwell\"]}}}}}");
        expect(firstBook3.state.title).toEqual("Indiana Smith");
        expect(firstBook3.state.price).toEqual(4.99);
        expect(firstBook3.state.authors.first()).toEqual("Jim Orwell");
    });
    it("supports properties with magic reducers (experimental)", function () {
        var store = logStore(book_1.Book.reduce.store());
        var title1 = book_1.Book.title(index_1.snapshot(store));
        title1(index_1.replace("Star Warts"));
        expect(store.getState().title).toEqual("Star Warts");
        expect(book_1.Book.empty.title).toEqual("");
        var title2 = book_1.Book.title(index_1.snapshot(store));
        expect(title2.state).toEqual("Star Warts");
    });
    it("supports $ for piping on cursors", function () {
        var store = logStore(shop_1.Shop.reduce.store());
        var shop1 = index_1.snapshot(store);
        var shop2 = shop1(shop_1.Shop.shelves.add("ADV"));
        var advShelf1 = shop2.$(shop_1.Shop.shelves, "ADV");
        shop1.$(shop_1.Shop.shelves, "ADV")
            .$(shelf_1.Shelf.books, 123)
            .$(book_1.Book.title)(index_1.replace("The Tiger Who Came To Tea"));
        expect(JSON.stringify(store.getState())).toEqual("{\"name\":\"\",\"shelves\":{\"ADV\":{\"description\":\"\",\"books\":{\"123\":{\"title\":\"The Tiger Who Came To Tea\",\"price\":0,\"authors\":[]}}}}}");
    });
});
//# sourceMappingURL=immutoSpec.js.map