!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("rbush")):"function"==typeof define&&define.amd?define(["rbush"],t):"object"==typeof exports?exports.labelgun=t(require("rbush")):e.labelgun=t(e.rbush)}(this,function(e){return function(e){function t(l){if(a[l])return a[l].exports;var n=a[l]={i:l,l:!1,exports:{}};return e[l].call(n.exports,n,n.exports,t),n.l=!0,n.exports}var a={};return t.m=e,t.c=a,t.d=function(e,a,l){t.o(e,a)||Object.defineProperty(e,a,{configurable:!1,enumerable:!0,get:l})},t.n=function(e){var a=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(a,"a",a),a},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,a){"use strict";function l(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var n=function(){function e(e,t){for(var a=0;a<t.length;a++){var l=t[a];l.enumerable=l.enumerable||!1,l.configurable=!0,"value"in l&&(l.writable=!0),Object.defineProperty(e,l.key,l)}}return function(t,a,l){return a&&e(t.prototype,a),l&&e(t,l),t}}(),i=a(1),s=function(e){return e&&e.__esModule?e:{default:e}}(i),r=function(){function e(t,a,n){l(this,e);var i=n||10;this.tree=(0,s.default)(i),this.allLabels={},this.hasChanged=[],this.allChanged=!1,this.hideLabel=t,this.showLabel=a}return n(e,[{key:"_total",value:function(e){for(var t=0,a=0,l=Object.keys(this.allLabels);a<l.length;a++)this.allLabels[l[a]].state==e&&(t+=1);return t}},{key:"totalShown",value:function(){return this._total("show")}},{key:"totalHidden",value:function(){return this._total("hide")}},{key:"_getLabelsByState",value:function(e){for(var t=[],a=0,l=Object.keys(this.allLabels);a<l.length;a++)this.allLabels[l[a]].state==e&&t.push(this.allLabels[l[a]]);return t}},{key:"getHidden",value:function(){return this._getLabelsByState("hide")}},{key:"getShown",value:function(){return this._getLabelsByState("show")}},{key:"getCollisions",value:function(e){var t=this.allLabels[e];if(void 0===t)throw Error("Label doesn't exist :"+JSON.stringify(e));var a=this.tree.search(t),l=a.indexOf(t);return void 0!==l&&a.splice(l,1),a}},{key:"getLabel",value:function(e){return this.allLabels[e]}},{key:"reset",value:function(){this.tree.clear(),this.allLabels={},this.hasChanged=[],this.allChanged=!1}},{key:"_callLabelCallbacks",value:function(e){var t=this;Object.keys(this.allLabels).forEach(function(a){t._callLabelStateCallback(t.allLabels[a],e)})}},{key:"_callLabelStateCallback",value:function(e,t){var a=t||e.state;"show"===a&&this.showLabel(e),"hide"===a&&this.hideLabel(e)}},{key:"_compareLabels",value:function(){var e=this;this.orderedLabels=Object.keys(this.allLabels).map(function(t){return e.allLabels[t]}).sort(this._compare),this.orderedLabels.forEach(function(t){var a=e.tree.search(t);(0===a.length||e._allLower(a,t)||t.isDragged)&&(e.allLabels[t.id].state="show")})}},{key:"_allLower",value:function(e,t){for(var a=void 0,l=0;l<e.length;l++)if(a=e[l],"show"===a.state||a.weight>t.weight||a.isDragged)return!1;return!0}},{key:"_compare",value:function(e,t){return e.weight>t.weight?-1:e.weight<t.weight?1:0}},{key:"_setupLabels",value:function(){var e=this;this.allChanged?(this.allChanged=!1,this.hasChanged=[],this.tree.clear(),Object.keys(this.allLabels).forEach(function(t){e._handleLabelIngestion(t)})):this.hasChanged.length>0&&(this.hasChanged.forEach(function(t){e._handleLabelIngestion(t)}),this.hasChanged=[])}},{key:"_handleLabelIngestion",value:function(e){var t=this.allLabels[e];this.ingestLabel({bottomLeft:[t.minX,t.minY],topRight:[t.maxX,t.maxY]},t.id,t.weight,t.labelObject,t.name,t.isDragged)}},{key:"update",value:function(e){this.allChanged=!e,this._setupLabels(),this._compareLabels(),this._callLabelCallbacks()}},{key:"removeLabel",value:function(e){var t=this.allLabels[e];this.tree.remove(t),delete this.allLabels[e]}},{key:"_addToTree",value:function(e){this.allLabels[e.id]=e,this.tree.insert(e)}},{key:"ingestLabel",value:function(e,t,a,l,n,i){if(void 0!==a&&null!==a||(a=0),!e||!e.bottomLeft||!e.topRight)throw Error("Bounding box must be defined with bottomLeft and topRight properties");if("string"!=typeof t&&"number"!=typeof t)throw Error("Label IDs must be a string or a number");var s=this.allLabels[t];s&&this.removeLabel(s.id);var r={minX:e.bottomLeft[0],minY:e.bottomLeft[1],maxX:e.topRight[0],maxY:e.topRight[1],state:"hide",id:t,weight:a,labelObject:l,name:n,isDragged:i};this._addToTree(r)}},{key:"labelHasChanged",value:function(e){-1===this.hasChanged.indexOf(e)&&this.hasChanged.push(e)}}]),e}();t.default=r},function(t,a){t.exports=e}])});