text: const assert = require('assert')
  const trie = require('./trie')
  
  module.exports = Wayfarer
  
  // create a router
  // str -> obj
  function Wayfarer (dft) {
    if (!(this instanceof Wayfarer)) return new Wayfarer(dft)
  
    const _default = (dft || '').replace(/^\//, '')
    const _trie = trie()
  
    emit._trie = _trie
    emit.emit = emit
    emit.on = on
    emit._wayfarer = true
  
    return emit
  
    // define a route
    // (str, fn) -> obj
    function on (route, cb) {
      assert.equal(typeof route, 'string')
      assert.equal(typeof cb, 'function')
  
      route = route || '/'
  
      if (cb && cb._wayfarer && cb._trie) {
        _trie.mount(route, cb._trie.trie)
      } else {
        const node = _trie.create(route)
        node.cb = cb
      }
  
      return emit
    }
  
    // match and call a route
    // (str, obj?) -> null
    function emit (route) {
      assert.notEqual(route, undefined, "'route' must be defined")
      const args = Array.prototype.slice.apply(arguments)
  
      const node = _trie.match(route)
      if (node && node.cb) {
        args[0] = node.params
        return node.cb.apply(null, args)
      }
  
      const dft = _trie.match(_default)
      if (dft && dft.cb) {
        args[0] = dft.params
        return dft.cb.apply(null, args)
      }
  
      throw new Error("route '" + route + "' did not match")
    }
  }
  