/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","./dom"],(function(e,t){"use strict";
/*!
   * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
   * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
   * v1.0.0-beta.82
   */const n="calciteInternalLabelClick",l="calciteInternalLabelConnected",o="calciteInternaLabelDisconnected",i="calcite-label",s=new WeakMap,c=new WeakMap,a=new WeakMap,r=new Set,d=e=>{const{id:n}=e,l=n&&t.queryElementRoots(e,{selector:`${i}[for="${n}"]`});if(l)return l;const o=t.closestElementCrossShadowBoundary(e,i);return!o||u(o,e)?null:o};function u(e,t){let n;const l="custom-element-ancestor-check",o=l=>{l.stopImmediatePropagation();const o=l.composedPath();n=o.slice(o.indexOf(t),o.indexOf(e))};e.addEventListener(l,o,{once:!0}),t.dispatchEvent(new CustomEvent(l,{composed:!0,bubbles:!0})),e.removeEventListener(l,o);return n.filter((n=>n!==t&&n!==e)).filter((e=>{var t;return null===(t=e.tagName)||void 0===t?void 0:t.includes("-")})).length>0}function v(e){const t=d(e.el);if(s.has(t)||!t&&r.has(e))return;const i=h.bind(e);if(t){e.labelEl=t;const d=m.bind(e);s.set(e.labelEl,d),e.labelEl.addEventListener(n,d),r.delete(e),document.removeEventListener(l,c.get(e)),a.set(e,i),document.addEventListener(o,i)}else r.has(e)||(i(),document.removeEventListener(o,a.get(e)))}function b(e){if(r.delete(e),document.removeEventListener(l,c.get(e)),document.removeEventListener(o,a.get(e)),c.delete(e),a.delete(e),!e.labelEl)return;const t=s.get(e.labelEl);e.labelEl.removeEventListener(n,t),s.delete(e.labelEl)}function E(e){var t,n;return e.label||(null===(n=null===(t=e.labelEl)||void 0===t?void 0:t.textContent)||void 0===n?void 0:n.trim())||""}function m(e){if(this.disabled)return;this.el.contains(e.detail.sourceEvent.target)||this.onLabelClick(e)}function f(){r.has(this)&&v(this)}function h(){r.add(this);const e=c.get(this)||f.bind(this);c.set(this,e),document.addEventListener(l,e)}e.connectLabel=v,e.disconnectLabel=b,e.getLabelText=E,e.labelConnectedEvent=l,e.labelDisconnectedEvent=o}));
