/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import{i as L,j as C,z as h}from"./DVG3NA3H.js";var k="calciteInternalLabelClick",E="calciteInternalLabelConnected",f="calciteInternalLabelDisconnected",v="calcite-label",r=new WeakMap,i=new WeakMap,c=new WeakMap,d=new WeakMap,s=new Set,y=e=>{let{id:t}=e,l=t&&L(e,{selector:`${v}[for="${t}"]`});if(l)return l;let a=C(e,v);return!a||M(a,e)?null:a};function M(e,t){let l,a="custom-element-ancestor-check",o=(n=>{n.stopImmediatePropagation();let u=n.composedPath();l=u.slice(u.indexOf(t),u.indexOf(e))});return e.addEventListener(a,o,{once:!0}),t.dispatchEvent(new CustomEvent(a,{composed:!0,bubbles:!0})),e.removeEventListener(a,o),l.filter(n=>n!==t&&n!==e).filter(n=>n.tagName?.includes("-")).length>0}function m(e){if(!e)return;let t=y(e.el);if(t&&i.has(t)&&t===e.labelEl||!t&&s.has(e))return;let l=O.bind(e);if(t){e.labelEl=t;let a=r.get(t)||[];a.push(e),r.set(t,a.sort(w)),i.has(e.labelEl)||(i.set(e.labelEl,g),e.labelEl.addEventListener(k,g)),s.delete(e),document.removeEventListener(E,c.get(e)),d.set(e,l),document.addEventListener(f,l)}else s.has(e)||(l(),document.removeEventListener(f,d.get(e)))}function T(e){if(!e||(s.delete(e),document.removeEventListener(E,c.get(e)),document.removeEventListener(f,d.get(e)),c.delete(e),d.delete(e),!e.labelEl))return;let t=r.get(e.labelEl);t.length===1&&(e.labelEl.removeEventListener(k,i.get(e.labelEl)),i.delete(e.labelEl)),r.set(e.labelEl,t.filter(l=>l!==e).sort(w)),e.labelEl=void 0}function w(e,t){return h(e.el,t.el)?-1:1}function I(e){return e.label||e.labelEl?.textContent?.trim()||""}function g(e){let t=e.detail.sourceEvent.target,l=r.get(this),a=l.find(n=>n.el===t);if(a&&l.includes(a))return;let b=l[0];b.disabled||b.onLabelClick(e)}function D(){s.has(this)&&m(this)}function O(){s.add(this);let e=c.get(this)||D.bind(this);c.set(this,e),document.addEventListener(E,e)}async function B(e){if(await e.componentOnReady(),r.has(e))return;let l=e.for&&e.ownerDocument?.getElementById(e.for);l&&requestAnimationFrame(()=>{for(let a of s)if(a.el===l){m(a);break}})}export{E as a,f as b,m as c,T as d,I as e,B as f};
