noflo = require 'noflo'

exports.getComponent = ->
  c = new noflo.Component
  c.icon = 'facebook-square'
  c.description = 'Check the type of Facebook posts and route based on that'

  route = (post) ->
    return unless post.link
    switch post.type
      when 'link'
        c.outPorts.link.send post.link
      when 'photo'
        return unless post.object_id
        c.outPorts.photo.send post.object_id

  c.inPorts.add 'in',
    datatype: 'object'
    process: (event, payload) ->
      switch event
        when 'begingroup'
          c.outPorts.photo.beginGroup payload
          c.outPorts.link.beginGroup payload
        when 'data'
          route payload
        when 'endgroup'
          c.outPorts.photo.endGroup()
          c.outPorts.link.endGroup()
        when 'disconnect'
          c.outPorts.photo.disconnect()
          c.outPorts.link.disconnect()
  c.outPorts.add 'link',
    datatype: 'string'
  c.outPorts.add 'photo',
    datatype: 'object'
  c
