//@flow

import { Observable } from 'rxjs/Observable'
import 'rxjs/add/observable/from'
import 'rxjs/add/operator/do'
import 'rxjs/add/operator/filter'
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/distinctUntilChanged'

import ObservableHistory from './observable.history'
import createHistory from 'history/lib/createBrowserHistory'

import Command from './Command'
import Index from './Index'
import Location from './Location'
import Page from './Page'

const history = ObservableHistory(createHistory())

const log = console.log.bind(console)
const next = log.bind("NEXT")
const error = log.bind("ERROR")
const done = log.bind("DONE")

import { decode } from 'base-64'
import 'rxjs/add/observable/dom/ajax'

//Observable
//  .ajax({
//    method: 'GET',
//    withCredentials: false,
//    url: `https://api.github.com/repos/tldr-pages/tldr-pages.github.io/contents/assets/index.json?ref=master`
//  })
//  .timeout(5000, new Error("Timeout :("))
//  .filter( res => res.status === 200 )
//  .pluck('response')
//  .pluck('content')
//  .map(decode)
//  .map(JSON.parse)
//  .mergeMap( index => index.commands )
//  .filter( cmd => cmd.name === "vim" )
//  .defaultIfEmpty(false)
//  .subscribe(next, error, done)

let State = Observable
  .from(history)
  .distinctUntilChanged()
  .map( (location) => ({
    index: Location.toIndex(location),
    command: Location.toCommand(location)
  }))
  .mergeMap( state => Index(state.index).search(state.command.name)
          , (state, found) => ({state, found}))

// Subscribe to commands found and fetch them
Observable.from(State)
  .filter( ({state, found}) => found )
  .mergeMap( ({state, found}) =>
    Page({
      branch: state.index.branch,
      repository: 'tldr-pages/tldr',
      timeout: 5000
    }).get(state.command)
 )
  .subscribe(next, error, done)

// Subscribe to commands not being found
Observable.from(State)
  .filter( ({state, found}) => !found )
  .subscribe(next, error, done)

@@ -2,16 +2,17 @@
 /@flow%0A%0A
+%0A
 import %7B
@@ -345,40 +345,8 @@
 y'%0A%0A
-import Command from './Command'%0A
 impo
@@ -430,16 +430,51 @@
 /Page'%0A%0A
+import render from './components'%0A%0A
 const hi
@@ -523,19 +523,21 @@
 %0A%0Aconst 
-log
+next 
  = conso
@@ -559,679 +559,110 @@
 sole
-)%0Aconst next = log.bind(%22NEXT%22)%0Aconst error = log.bind(%22ERROR%22)%0Aconst done = log.bind(%22DONE%22)%0A%0Aimport %7B decode %7D from 'base-64'%0Aimport 'rxjs/add/observable/dom/ajax'%0A%0A//Observable%0A//  .ajax(%7B%0A//    method: 'GET',%0A//    withCredentials: false,%0A//    url: %60https://api.github.com/repos/tldr-pages/tldr-pages.github.io/contents/assets/index.json?ref=master%60%0A//  %7D)%0A//  .timeout(5000, new Error(%22Timeout :(%22))%0A//  .filter( res =%3E res.status === 200 )%0A//  .pluck('response')%0A//  .pluck('content')%0A//  .map(decode)%0A//  .map(JSON.parse)%0A//  .mergeMap( index =%3E index.commands )%0A//  .filter( cmd =%3E cmd.name === %22vim%22 )%0A//  .defaultIfEmpty(false)%0A//  .subscribe(next, error, done
+,%22NEXT%22)%0Aconst error = console.log.bind(console,%22ERROR%22)%0Aconst done  = console.log.bind(console,%22DONE%22
 )%0A%0Al
@@ -1224,16 +1224,70 @@
 ommand)%0A
+  , (%7Bstate, found%7D, page) =%3E (%7Bstate, found, page%7D)%0A 
  )%0A  .su
@@ -1286,36 +1286,38 @@
  )%0A  .subscribe(
-next
+render
 , error, done)%0A%0A
@@ -1433,20 +1433,22 @@
 bscribe(
-next
+render
 , error,

Extend state with page if found