/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.25/esri/copyright.txt for details. */ import{_ as t}from"../chunks/tslib.es6.js";import o from"../core/Accessor.js";import e from"../core/Collection.js";import{HandleOwner as i}from"../core/HandleOwner.js";import{makeHandle as a}from"../core/handleUtils.js";import"../core/has.js";import r from"../core/Logger.js";import{isSome as s,isNone as n,destroyMaybe as l}from"../core/maybe.js";import{watch as h}from"../core/reactiveUtils.js";import{property as c}from"../core/accessorSupport/decorators/property.js";import"../core/arrayUtils.js";import"../core/accessorSupport/ensureType.js";import{subclass as p}from"../core/accessorSupport/decorators/subclass.js";import{TextureCollection as u}from"./3d/support/TextureCollection.js";import{ViewEventPriorities as d}from"./input/InputManager.js";import{eventTypes as v}from"./input/ViewEvents.js";import{getToolCollectionHandles as m,areToolManipulatorsEditable as _}from"./interactive/interactiveToolUtils.js";import{EditableFlag as f}from"./interactive/interfaces.js";import{ToolViewManagerManipulatorState as T}from"./interactive/ToolViewManagerManipulatorState.js";const g="attached",E="tools";let w=class extends i{constructor(t){super(t),this._manipulatorState=new T,this.tools=new e,this.cursor=null,this._forEachTool=t=>{for(const o of this.tools.items)if(t(o))return}}initialize(){this.handles.add([this.view.on(v,(t=>{this._handleInputEvent(t)}),d.TOOL),...m(this.tools),this.tools.on("before-add",(({item:t})=>{this._updateToolEditableFlag(t)})),this.tools.on("before-remove",(({item:t})=>{this._manipulatorState.clearPointers(t,this._manipulatorStateEventArgs),this._updateCursor()})),this.tools.on("change",(()=>{this._refreshToolWatchers()}))])}destroy(){this.detach(),this.handles.removeAll()}get _manipulatorStateEventArgs(){return{forEachTool:this._forEachTool,activeTool:this.activeTool,setActiveTool:t=>{this.activeTool=t},view:this.view}}set activeTool(t){if(s(t)&&!this.view.ready)return void r.getLogger(this.declaredClass).error("Cannot set active tool while view is not ready.");if(t===this.activeTool)return;const o=this.activeTool;this._set("activeTool",t),s(o)&&o.deactivate(),s(t)&&t.activate(),this._removeIncompleteTools(t);for(const e of this.tools){this._updateToolEditableFlag(e);const t=_(e);!n(this.activeTool)&&t||this._manipulatorState.clearPointers(e,this._manipulatorStateEventArgs,!t)}this._updateCursor()}get updating(){return this.updatingHandles.updating||this.tools.some((t=>t.updating))||(this.textures?.updating??!1)}attach(){"3d"===this.view.type?(this._set("textures",new u(this.view._stage,this.view.resourceController.scheduler)),this.handles.add([h((()=>{const{state:t}=this.view;return"camera"in t&&t.camera}),(()=>{this._forEachManipulator((t=>{null!=t.onViewChange&&t.onViewChange()}))})),this.view.elevationProvider.on("elevation-change",(t=>{this._forEachManipulator((o=>{null!=o.onElevationChange&&o.onElevationChange(t)}))})),a((()=>this._set("textures",l(this.textures))))],g)):this.handles.add(h((()=>this.view.extent),(()=>{this._forEachManipulator((t=>{null!=t.onViewChange&&t.onViewChange()}))})))}detach(){s(this.activeTool)&&(this.activeTool=null),this.tools.removeAll(),this.handles.remove(g)}_forEachManipulator(t){this._forEachTool((o=>{o.manipulators&&o.manipulators.forEach((({manipulator:e})=>t(e,o)))}))}_handleInputEvent(t){let o=!1;const e={...t,stopPropagation:()=>{o=!0,t.stopPropagation()}};s(this.activeTool)?this.activeTool.handleInputEvent&&this.activeTool.handleInputEvent(e):this._forEachTool((t=>{!o&&t.visible&&t.handleInputEvent(e)})),!o&&"key-down"===t.type&&"Escape"===t.key&&this.activeTool&&(t.stopPropagation(),this.activeTool=null),this._manipulatorState.handleInputEvent(e,this._manipulatorStateEventArgs),!o&&s(this.activeTool)&&this.activeTool.handleInputEventAfter(e),this._manipulatorState.handleHoverEvent(e,this._forEachTool),this._updateCursor()}_refreshToolWatchers(){this.handles.remove(E),this._forEachTool((t=>{if(t instanceof o){const o=h((()=>[t.cursor,t.visible,t.editable]),(()=>{_(t)||this._manipulatorState.clearPointers(t,this._manipulatorStateEventArgs),this._updateCursor()}));this.handles.add(o,E)}t.manipulators&&this.handles.add([t.manipulators.on("after-remove",(o=>{this._manipulatorState.clearPointers(t,this._manipulatorStateEventArgs,!0,o.item.manipulator)})),t.manipulators.on("change",(()=>{this._manipulatorState.updateHoveredStateFromKnownPointers(this._forEachTool),this._updateCursor()}))],E)})),this._manipulatorState.updateHoveredStateFromKnownPointers(this._forEachTool),this._updateCursor()}_updateToolEditableFlag(t){t.setEditableFlag(f.MANAGER,n(this.activeTool)||t===this.activeTool)}_updateCursor(){let t=this._manipulatorState.cursor;n(t)&&this._forEachTool((o=>!(!s(o.cursor)||!o.visible)&&(t=o.cursor,!0))),this._get("cursor")!==t&&this._set("cursor",t)}_removeIncompleteTools(t){this.tools.filter((o=>(n(t)||o!==t)&&!o.created&&o.removeIncompleteOnCancel)).forEach((t=>{this.tools.remove(t)}))}};t([c({constructOnly:!0,nonNullable:!0})],w.prototype,"view",void 0),t([c({readOnly:!0,nonNullable:!0})],w.prototype,"textures",void 0),t([c({value:null})],w.prototype,"activeTool",null),t([c({readOnly:!0,type:e})],w.prototype,"tools",void 0),t([c({readOnly:!0})],w.prototype,"cursor",void 0),t([c({readOnly:!0})],w.prototype,"updating",null),w=t([p("esri.views.ToolViewManager")],w);const y=w;export{y as default};