'use strict';

var tansu = require('tansu');
var html = require('./html');
var sheetRouter = require('sheet-router');
var walk = require('sheet-router/walk');
var href = require('sheet-router/href');

function createModel(model) {
  return Object.assign({}, model, {
    models: Object.assign({}, model.models, {
      location: {
        state: window.location,
        reducers: {
          set: function set(state, location) {
            window.history.pushState('', '', location);
            return Object.assign({}, state, {
              location: window.location
            });
          }
        }
      }
    })
  });
}

module.exports = function (opts) {
  if (opts.model.models) {
    opts.model.models = {};
  }
  var model = createModel(opts.model);
  var routes = opts.routes;
  var router = sheetRouter({ thunk: 'match' }, routes);

  var dom = void 0;

  return function () {
    var state = void 0;
    var prev = void 0;

    function subscribe(_state, _prev) {
      state = _state;
      prev = _prev;
      router(window.location.href);
    }

    var store = tansu(subscribe)(model);

    state = store.state;
    prev = store.state;

    walk(router, function (route, handler) {
      return function (params) {
        return function () {
          var newDom = handler(state, prev, store.methods);
          if (dom) {
            dom = html.update(dom, newDom);
          }
          return newDom;
        };
      };
    });

    href(function (href) {
      store.methods.location.set(href.pathname);
    });

    dom = router(window.location.href);
    return dom;
  };
};
@@ -668,16 +668,17 @@
 %7B%0A  if (
+!
 opts.mod
@@ -765,36 +765,8 @@
 l);%0A
-  var routes = opts.routes;%0A
   va
@@ -808,16 +808,21 @@
 tch' %7D, 
+opts.
 routes);
@@ -847,34 +847,8 @@
  0;%0A
-%0A  return function () %7B%0A  
   va
@@ -865,18 +865,16 @@
 void 0;%0A
-  
   var pr
@@ -889,17 +889,22 @@
 d 0;%0A%0A  
- 
+return
  functio
@@ -905,24 +905,81 @@
 unction 
-subscrib
+() %7B%0A    var hooks = %7B%0A      onStateChange: function onStateChang
 e(_state
@@ -985,24 +985,26 @@
 e, _prev) %7B%0A
+  
       state 
@@ -1015,24 +1015,26 @@
 tate;%0A      
+  
 prev = _prev
@@ -1035,16 +1035,18 @@
  _prev;%0A
+  
       ro
@@ -1077,18 +1077,112 @@
 f);%0A    
-%7D%0A
+  %7D,%0A%0A      onMethodCall: opts.onMethodCall %7C%7C function () %7B%0A        return null;%0A      %7D%0A    %7D;
 %0A    var
@@ -1200,17 +1200,13 @@
 nsu(
-subscribe
+hooks
 )(mo

Update dist