_ = require('lodash')
Burger = require('./burger')

byName = (conditions, item) ->
  item.name == conditions.name

byData = (conditions, item) ->
  for own key, value of conditions
    return false if item.data.get(key) != value

  true

class Collection
  constructor: (collection) ->
    @__collection = collection

  size: ->
    @__collection.length

  first: ->
    @__collection[0]

  where: (conditions) ->
    filter = if _.isFunction(conditions) && Burger.isRegistered(conditions)
                byName
              else if _.isObject(conditions)
                byData
              else
                (->)

    results =
      @__collection.filter (item) ->
        filter(conditions, item)

    new Collection(results)

  find: (conditions) ->
    where(conditions).first()

module.exports = Collection
