/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import t from"../../core/Collection.js";class a{constructor(){this._isToolEditable=!0,this._manipulators=new t,this._resourceContexts={manipulator3D:{}},this._attached=!1}set isToolEditable(t){this._isToolEditable=t}get length(){return this._manipulators.length}add(t,a=0){this.addMany([t],a)}addMany(t,a=0){for(const i of t){const t={manipulator:i,visibilityPredicate:a,attached:!1};this._manipulators.add(t),this._attached&&this._updateManipulatorAttachment(t)}}remove(t){for(let a=0;a<this._manipulators.length;a++)if(this._manipulators.at(a).manipulator===t){const t=this._manipulators.splice(a,1)[0];this._detachManipulator(t);break}}removeAll(){this._manipulators.forEach(t=>{this._detachManipulator(t)}),this._manipulators.removeAll()}attach(){this._manipulators.forEach(t=>{this._updateManipulatorAttachment(t)}),this._attached=!0}detach(){this._manipulators.forEach(t=>{this._detachManipulator(t)}),this._attached=!1}destroy(){this.detach(),this._manipulators.forEach(({manipulator:t})=>t.destroy()),this._manipulators.destroy(),this._resourceContexts=null}on(t,a){return this._manipulators.on(t,t=>{a(t)})}forEach(t){for(const a of this._manipulators.items)t(a)}some(t){return this._manipulators.items.some(t)}toArray(){const t=[];return this.forEach(a=>t.push(a.manipulator)),t}intersect(t,a){let i=null,e=Number.MAX_VALUE;return this._manipulators.forEach(({manipulator:s,attached:r})=>{if(!r||!s.interactive)return;const o=s.intersectionDistance(t,a);null!=o&&o<e&&(e=o,i=s)}),i}_updateManipulatorAttachment(t){this._isManipulatorItemVisible(t)?this._attachManipulator(t):this._detachManipulator(t)}_attachManipulator(t){t.attached||(t.manipulator.attach&&t.manipulator.attach(this._resourceContexts),t.attached=!0)}_detachManipulator(t){if(!t.attached)return;const a=t.manipulator;a.grabbing=!1,a.dragging=!1,a.hovering=!1,a.selected=!1,a.detach&&a.detach(this._resourceContexts),t.attached=!1}_isManipulatorItemVisible(t){return 2===t.visibilityPredicate||(this._isToolEditable?0===t.visibilityPredicate:1===t.visibilityPredicate)}}export{a as ManipulatorCollection};
