/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{createTask as o}from"../../core/asyncUtils.js";import{handlesGroup as t}from"../../core/handleUtils.js";import{removeMaybe as e}from"../../core/maybe.js";import{throwIfAborted as n,isAbortError as i,onAbort as a}from"../../core/promiseUtils.js";import{watch as r,syncAndInitial as s,on as l,whenOnce as c}from"../../core/reactiveUtils.js";function u(t,n){t.interactive=!0;const{tool:i,view:r}=t;r.activeTool=i;let s=a(n,()=>{r.activeTool===i&&(r.activeTool=null)});return o(async o=>{await c(()=>!t.tool?.active,o),s=e(s)},n)}function m(o,t){return r(()=>o.interactive,()=>p(o,t),s)}function p(o,t){o.interactive?v(o,t):f(o)}function v(o,t){f(o);const{view:e,analysis:n}=o,i=new t({view:e,analysis:n,analysisViewData:o});return o.tool=i,e.tools.add(i),i}function f(o){const{view:t,tool:e}=o;null!=e&&(t.tools.remove(e),o.tool=null)}function y(e,a){const r=e.userOperation,s=o(async o=>{if(r){const t=r.promise.catch(()=>{});r?.abort(),await t,n(o)}const s=u(e,{signal:o}),c=a?.shouldRejectOnCancel??(()=>!0),m=t([l(()=>e.tool,"cancel",()=>{c()&&s.abort()})]),{tool:p}=e;try{p&&(p.resetCreated(),a?.onToolActivated?.(p)),await s.promise,n(o)}catch(v){if(o.aborted||!i(v)||c())throw v}finally{m?.remove()}},{signal:a?.abortOptions?.signal});return e.userOperation=s,s.promise.finally(()=>{e.userOperation===s&&(e.userOperation=null)})}async function d(o,t){const e=o.analysis.clone();return await y(o,{abortOptions:t?.placementOptions,onToolActivated:t?.onToolActivated,shouldRejectOnCancel:()=>{const{analysis:t}=o;return!t.valid||t.equals(e)}}),{}}export{u as activateAnalysisViewTool,m as connectAnalysisViewToTool,f as removeAnalysisViewTool,y as startExclusiveInteractiveOperation,d as startPlaceOperation};
