/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{createTask as e}from"../../core/asyncUtils.js";import{on as o}from"../../core/events.js";import{makeHandle as t}from"../../core/handleUtils.js";import{ignoreAbortErrors as i,isAborted as n,onAbort as s,waitTick as r}from"../../core/promiseUtils.js";import{whenOnce as a}from"../../core/reactiveUtils.js";import{waitAnimationFrame as c}from"../../core/scheduling.js";import{createScreenPoint as l}from"../../core/screenUtils.js";import m from"../../views/interactive/sketch/SketchTooltipVisibleElements.js";async function p(o,t,m={}){async function p(e){const i=h(o),{tooltipOptions:p}=o;if("coordinates"===t?(t="x",p.xyMode="coordinates"):"direction"!==t&&"distance"!==t||(p.xyMode="direction-distance"),await c(),n(m)||n(e))return;const d=i?.tooltip;if(!d||!i.activeTooltipInfo?.editableFields?.find(e=>e.name===t))return;const{handle:f,promise:u}=v(d,m),w=Promise.withResolvers(),y=s(m.signal,w.resolve),b=s(e,w.resolve),j=p.offset,g=d.position;try{if(p.forceEnabled=!0,await r(),m.position&&(p.offset=0,d.position=l(m.position[0],m.position[1])),await d.enterInputMode(t),n(m)||n(e))return;m.onOpen?.(),await Promise.race([a(()=>i.destroyed||"feedback"===d.content?.mode,m),u,w.promise])}finally{y?.remove(),b?.remove(),f.remove();const e=d.outerContainer.contains(document.activeElement);p.enabled?await d.exitInputMode({focusOnView:e}):e&&o.view?.focus(),m.onClose?.(),d.position=g,p.offset=j,p.forceEnabled=!1}}const f=d.get(o);f&&(f.abort(),await f.promise);const u=e(p);return d.set(o,u),u.promise.catch(i).finally(()=>{d.get(o)===u&&d.delete(o)})}const d=new Map,f={area:!1,distance:!1,direction:!1,elevation:!1,orientation:!1,radius:!1,rotation:!1,scale:!1,size:!1,totalLength:!1,coordinates:!1,helpMessage:!1,header:!1};function u(e){e.tooltipOptions.visibleElements=new m(f)}function v(e,{hideOnBlur:i=!0}){const n=Promise.withResolvers();if(!i)return{promise:n.promise,handle:t()};const{outerContainer:s}=e,r=o(s,"focusout",({relatedTarget:e})=>{e instanceof HTMLElement&&s.contains(e)||n.resolve()});return{promise:n.promise,handle:r}}function h(e){const{activeComponent:o}=e;return o&&"tooltip"in o&&"activeTooltipInfo"in o&&"destroyed"in o?o:null}export{u as hideAllVisibleElements,p as showTooltipAndFocusField};
