'use strict'

var sorted = require('sorted-array-functions')
var match = require('./match')
var onlyRegex = require('./onlyRegex')

function Bucket (parent) {
  this.data = []
  this._algo = parent._algo
  this._isDeep = parent._isDeep
  this.magic = this._isDeep ? 0 : Number.MAX_SAFE_INTEGER
}

Bucket.prototype.add = function (set) {
  sorted.add(this.data, set, this._algo)
  if (this._isDeep) {
    if (this.magic < set.magic) {
      this.magic = set.magic
    }
  } else {
    if (this.magic > set.magic) {
      this.magic = set.magic
    }
  }
  return this
}

Bucket.prototype.remove = function (pattern, payload) {
  var foundPattern = false
  var data = this.data
  for (var i = 0; i < data.length; i++) {
    if (match(pattern, data[i].pattern)) {
      if (payload === null || payload === data[i].payload || onlyRegex(pattern)) {
        data.splice(i, 1)
        foundPattern = true

        // to remove all occurences
        this.remove(pattern, payload)
        break
      }
    }
  }

  return foundPattern
}

Bucket.prototype.forEach = function (func, that) {
  this.data.forEach(func, that)
  return this
}

module.exports = Bucket

@@ -669,16 +669,53 @@
 is.data%0A
+  var justRegex = onlyRegex(pattern)%0A
   for (v
@@ -852,34 +852,25 @@
 load %7C%7C 
-only
+just
 Regex
-(pattern)
 ) %7B%0A    

Call onlyRegex just once when removing