text: function adjustRanges(e,n,t){for(var s=[],i=0;i<t.length;i++){var l=t[i];if(e<=l.beginIndex)i>0&&l.beginIndex+n===t[i-1].endIndex+1?(t[i-1].endIndex=l.endIndex+n,t.splice(i--,1),s.push(t[i])):(l.beginIndex+=n,l.endIndex+=n,s.push(l));else if(e>l.beginIndex&&e<=l.endIndex){var d={beginIndex:e+n,endIndex:l.endIndex+n};t.splice(++i,0,d),s.push(d),l.endIndex=e-1,s.push(l)}}return s}import*as tslib_1 from"tslib";import{ObservableValue}from"../Core/Observable";var Selection=function(e){function n(n){var t=e.call(this,[])||this;return t.selectedRanges=[],t.lockCount=0,t.unselectableRangesValue=[],t.selectedCount=0,t.unselectableCount=0,t.onItemsChanged=function(e,n){var s,i,l=e.index;if("change"!==n){if(e.removedItems&&e.removedItems.length&&(s=t.removeUnselectableInternal(l,e.removedItems.length),i=t.unselectInternal(l,e.removedItems.length)),e.addedItems||e.removedItems){var d=(e.addedItems?e.addedItems.length:0)-(e.removedItems?e.removedItems.length:0),a=adjustRanges(l,d,t.selectedRanges),c=adjustRanges(l,d,t.unselectableRanges);a.length&&t.notify(a,"set"),c.length&&t.notify(c,"setUnselectable")}s&&t.notify([s],"removeUnselectable"),i&&t.notify([i],"unselect")}},"boolean"==typeof n||void 0===n?t.multiSelect=!!n||!1:(t.alwaysMerge=!!n.alwaysMerge,t.multiSelect=!!n.multiSelect,t.unselectableRanges=n.unselectableRanges||[],t.value=n.selectedRanges||[]),t}return tslib_1.__extends(n,e),Object.defineProperty(n.prototype,"value",{get:function(){return this.selectedRanges},set:function(e){var n=this;this.selectedCount=0,this.selectedRanges=e.map(function(e){return n.selectedCount+=e.endIndex-e.beginIndex+1,{beginIndex:e.beginIndex,endIndex:e.endIndex}}),this.notify(e,"set")},enumerable:!0,configurable:!0}),Object.defineProperty(n.prototype,"unselectableRanges",{get:function(){return this.unselectableRangesValue},set:function(e){var n=this;this.unselectableCount=0,this.unselectableRangesValue=e.map(function(e){return n.unselectableCount+=e.endIndex-e.beginIndex+1,{beginIndex:e.beginIndex,endIndex:e.endIndex}}),this.notify(e,"setUnselectable")},enumerable:!0,configurable:!0}),n.prototype.clear=function(){var e=this.clearSelectedRanges();e&&this.notify(e,"unselect")},n.prototype.clearUnselectable=function(){var e=this.unselectableRangesValue.slice();this.unselectableRanges=[],this.unselectableCount=0,this.notify(e,"removeUnselectable")},n.prototype.selectable=function(e){return!indexWithinRanges(e,this.unselectableRanges)},n.prototype.selected=function(e){return indexWithinRanges(e,this.selectedRanges)},n.prototype.addUnselectable=function(e,n){var t=!1,s=e,i=e+(n||1)-1;for(n=n||1;n>0;n--)if(this.selectable(e)){for(var l=0,d=void 0;l<this.unselectableRanges.length;l++){var a=this.unselectableRanges[l];if(e<a.beginIndex){e===a.beginIndex-1&&(d=a).beginIndex--;break}if(e===a.endIndex+1){l<this.unselectableRanges.length-1&&e===this.unselectableRanges[l+1].beginIndex-1?((d=a).endIndex=this.unselectableRanges[l+1].endIndex,this.unselectableRanges.splice(l+1,1)):(d=a).endIndex++;break}}d||(d={beginIndex:e,endIndex:e},this.unselectableRanges.splice(l,0,d)),t=!0,this.unselectableCount++,e++}else e++;t&&this.notify([{beginIndex:s,endIndex:i}],"addUnselectable")},n.prototype.removeUnselectable=function(e,n){var t=this.removeUnselectableInternal(e,n);t&&this.notify([t],"removeUnselectable")},n.prototype.select=function(e,n,t){if(void 0===t&&(t=this.alwaysMerge),!this.lockCount){var s=e,i=s+(n||1)-1,l=!1,d=void 0;if(this.multiSelect)for(t||(d=this.clearSelectedRanges()),n=n||1;n>0;n--)if(!this.selected(e)&&this.selectable(e)){for(var a=0,c=void 0;a<this.selectedRanges.length;a++){var o=this.selectedRanges[a];if(e<o.beginIndex){e===o.beginIndex-1&&(c=o).beginIndex--;break}if(e===o.endIndex+1){a<this.selectedRanges.length-1&&e===this.selectedRanges[a+1].beginIndex-1?((c=o).endIndex=this.selectedRanges[a+1].endIndex,this.selectedRanges.splice(a+1,1)):(c=o).endIndex++;break}}c||(c={beginIndex:e,endIndex:e},this.selectedRanges.splice(a,0,c)),this.selectedCount++,e++,l=!0}else e++;else if(!this.selected(e)&&this.selectable(e)){d=this.clearSelectedRanges();c={beginIndex:e,endIndex:e};this.selectedRanges.push(c),this.selectedCount++,l=!0}d&&this.notify(d,"unselect"),l&&this.notify([{beginIndex:s,endIndex:i}],"select")}},n.prototype.toggle=function(e,n){void 0===n&&(n=this.alwaysMerge),this.selected(e)?this.unselect(e):this.select(e,1,n)},n.prototype.unselect=function(e,n){var t=this.unselectInternal(e,n);t&&this.notify([t],"unselect")},n.prototype.lock=function(){this.lockCount++},n.prototype.unlock=function(){this.lockCount--},n.prototype.removeUnselectableInternal=function(e,n){var t=e,s=t+(n||1)-1,i=!1;for(n=n||1;n>0;n--)if(this.selectable(e))e++;else{for(var l=0;l<this.unselectableRanges.length;l++){var d=this.unselectableRanges[l];if(!(e<d.beginIndex)&&(e>=d.beginIndex&&e<=d.endIndex)){e===d.beginIndex?d.beginIndex++:e===d.endIndex?d.endIndex--:(this.unselectableRanges.splice(l+1,0,{beginIndex:e+1,endIndex:d.endIndex}),d.endIndex=e-1),d.beginIndex>d.endIndex&&this.unselectableRanges.splice(l,1),this.unselectableCount--,i=!0;break}}e++}if(i)return{beginIndex:t,endIndex:s}},n.prototype.unselectInternal=function(e,n){var t=!1,s=e,i=s+(n||1)-1;if(!this.lockCount)for(n=n||1;n>0;n--)if(this.selected(e)){for(var l=0;l<this.selectedRanges.length;l++){var d=this.selectedRanges[l];if(!(e<d.beginIndex)&&(e>=d.beginIndex&&e<=d.endIndex)){e===d.beginIndex?d.beginIndex++:e===d.endIndex?d.endIndex--:(this.selectedRanges.splice(l+1,0,{beginIndex:e+1,endIndex:d.endIndex}),d.endIndex=e-1),d.beginIndex>d.endIndex&&this.selectedRanges.splice(l,1),this.selectedCount--,t=!0;break}}e++}else e++;if(t)return{beginIndex:s,endIndex:i}},n.prototype.clearSelectedRanges=function(){if(!this.lockCount&&this.selectedRanges.length>0){var e=this.selectedRanges.slice();return this.selectedRanges=[],this.selectedCount=0,e}},n}(ObservableValue);export{Selection};export function indexWithinRanges(e,n){if(n)for(var t=0,s=n;t<s.length;t++){var i=s[t];if(e>=i.beginIndex&&e<=i.endIndex)return!0}return!1};export function compareSelectionRanges(e,n){for(var t=[],s=0;s<e.length;s++)for(l=(i=e[s]).beginIndex;l<=i.endIndex;l++)indexWithinRanges(l,n)||t.push(-1*l);for(s=0;s<n.length;s++)for(var i=n[s],l=i.beginIndex;l<=i.endIndex;l++)indexWithinRanges(l,e)||t.push(l);return t};