noflo = require 'noflo'

getOutPort = (item) ->
  if item.type in ['text/x-markdown', 'text/markdown', 'text/plain']
    return 'markdown'
  'html'

exports.getComponent = ->
  c = new noflo.Component
  c.icon = 'code-fork'
  c.description = 'Route items by their mime type'
  c.inPorts.add 'in',
    datatype: 'object'
  c.outPorts.add 'html',
    datatype: 'object'
  c.outPorts.add 'markdown',
    datatype: 'object'

  noflo.helpers.WirePattern c,
    in: 'in'
    out: [
      'html'
      'markdown'
    ]
    forwardGroups: true
    async: true
  , (data, groups, out, callback) ->
    port = getOutPort data
    out[port].send data
    do callback

  c
