import {
  UpdateMessage,
  UpdateInlineQuery,
  getEntityFirst
} from './telegram-driver'
import { find, curryN, prop } from 'ramda'

import t from 'tcomb'

let matchPluginPath = (plugins, str) => find(
  ({path}) => str.match(path),
  plugins
)

let matchPluginArgs = (str, plugin) => str.match(plugin.path)

let makeResolverWith = curryN(3, (plugins, queryWith, u) => {
  let query = queryWith(u)
  let plugin = matchPluginPath(plugins, query)
  let props = matchPluginArgs(query, plugin)

  return { plugin, props }
})

export let matchPlugin = function (plugins) {
  let resolve = makeResolverWith(plugins)
  let type = (u) => t.match(u,
    UpdateMessage, resolve(
      u => u.message.text.substr(getEntityFirst('bot_command', u).offset)
    ),
    UpdateInlineQuery, resolve(
      u => u.inline_query.query
    )
  )

  return this.map(u => {
    let match = type(u)
    let component = {}
    if (match.plugin.type.is(u)) {
      component = match.plugin.component({props: match.props}, u) || {}
    }
    return component
  }).filter(prop('bot'))
}

@@ -115,46 +115,249 @@
 prop
- %7D from 'ramda'%0A%0Aimport t from 'tcomb'
+, compose, isNil, not, merge %7D from 'ramda'%0A%0Aimport isolate from '@cycle/isolate'%0Aimport t from 'tcomb'%0A%0Aconst UpdateMessageCommand = t.refinement(UpdateMessage,%0A  compose(not, isNil, getEntityFirst('bot_command')),%0A  'UpdateMessageCommand')
 %0A%0Ale
@@ -762,16 +762,25 @@
 (plugins
+, sources
 ) %7B%0A  le
@@ -865,16 +865,23 @@
 eMessage
+Command
 , resolv
@@ -1033,16 +1033,65 @@
 ry%0A    )
+,%0A    UpdateMessage, resolve(u =%3E u.message.text)
 %0A  )%0A%0A  
@@ -1156,24 +1156,76 @@
 ponent = %7B%7D%0A
+    let args = merge(%7Bprops: match.props%7D, sources)%0A
     if (matc
@@ -1265,16 +1265,24 @@
 onent = 
+isolate(
 match.pl
@@ -1295,37 +1295,22 @@
 omponent
-(%7Bprops: match.props%7D
+)(args
 , u) %7C%7C 

correct behaviour for `UpdateMessage`