/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import"../../../../core/has.js";import t from"../../../../core/PooledArray.js";import{create as e,copy as r,area as s,expand as n,contains as o}from"../../../../geometry/support/aaBoundingRect.js";import{noBudget as i}from"../../../support/Scheduler.js";const h=.05;class a{constructor(){this._extents=new t({allocator:t=>t||e()}),this._tmpExtent=e(),this._dirty=!1}destroy(){this._extents.prune()}get empty(){return 0===this._extents.length}get size(){return this._extents.length}clear(){this._extents.clear()}add(t){this._contains(t)||(this._removeContained(t),r(this._extents.pushNew(),t),this._dirty=!0)}pop(){return this._dirty&&this._mergeTight(),this._extents.pop()}merge(t){return this._mergeTight(t),t.hasProgressed}_mergeTight(t=i){const e=this._extents,o=new Set;let a=0;for(;a!==e.length;){e.sort((t,e)=>t[0]-e[0]),a=e.length,o.clear();for(let i=0;i<e.length;++i){if(t.done)return;const a=e.at(i);if(a){for(let t=i+1;t<e.length;++t){const r=e.at(t);if(null==r||r[0]>=a[2])break;o.add(r)}o.forEach(i=>{if(a===i)return;if(i[2]<=a[0])return void o.delete(i);const l=s(a),_=s(i),d=this._tmpExtent;n(a,i,d);const c=l+_;(s(d)-c)/c<h&&(r(a,d),o.delete(i),e.remove(i),t.madeProgress())}),o.add(a)}}}this._dirty=!1}_contains(t){return this._extents.some(e=>o(e,t))}_removeContained(t){this._extents.filterInPlace(e=>!o(t,e))}get test(){}}export{a as ExtentSet};
