import{__decorate as t}from"tslib";import{B as i,T as s,x as e}from"../lit-html-39a6718c.esm.js";import{property as o,state as h,customElement as n}from"lit/decorators.js";import{s as r}from"./nile-popover.css.esm.js";import{N as l}from"../internal/nile-element.esm.js";import{w as a}from"../internal/watch.esm.js";import{P as c}from"./portal-manager.esm.js";import{V as p}from"../utilities/visibility-manager.esm.js";import"lit";import"../internal/accessibility/a11y.state.enum.esm.js";import"../internal/accessibility/a11y.property.enum.esm.js";import"../internal/accessibility/role.enum.esm.js";import"../floating-ui.dom-cc9126da.esm.js";import"./portal-utils.esm.js";
/**
 * @license
 * Copyright 2019 Google LLC
 * SPDX-License-Identifier: BSD-3-Clause
 */const d=globalThis,u=d.ShadowRoot&&(void 0===d.ShadyCSS||d.ShadyCSS.nativeShadow)&&"adoptedStyleSheets"in Document.prototype&&"replace"in CSSStyleSheet.prototype,m=Symbol(),f=new WeakMap;let b=class{constructor(t,i,s){if(this._$cssResult$=!0,s!==m)throw Error("CSSResult is not constructable. Use `unsafeCSS` or `css` instead.");this.cssText=t,this.t=i}get styleSheet(){let t=this.o;const i=this.t;if(u&&void 0===t){const s=void 0!==i&&1===i.length;s&&(t=f.get(i)),void 0===t&&((this.o=t=new CSSStyleSheet).replaceSync(this.cssText),s&&f.set(i,t))}return t}toString(){return this.cssText}};const y=(t,i)=>{if(u)t.adoptedStyleSheets=i.map((t=>t instanceof CSSStyleSheet?t:t.styleSheet));else for(const s of i){const i=document.createElement("style"),e=d.litNonce;void 0!==e&&i.setAttribute("nonce",e),i.textContent=s.cssText,t.appendChild(i)}},v=u?t=>t:t=>t instanceof CSSStyleSheet?(t=>{let i="";for(const s of t.cssRules)i+=s.cssText;return(t=>new b("string"==typeof t?t:t+"",void 0,m))(i)})(t):t
/**
 * @license
 * Copyright 2017 Google LLC
 * SPDX-License-Identifier: BSD-3-Clause
 */,{is:w,defineProperty:S,getOwnPropertyDescriptor:g,getOwnPropertyNames:C,getOwnPropertySymbols:P,getPrototypeOf:$}=Object,k=globalThis,_=k.trustedTypes,j=_?_.emptyScript:"",E=k.reactiveElementPolyfillSupport,O=(t,i)=>t,A={toAttribute(t,i){switch(i){case Boolean:t=t?j:null;break;case Object:case Array:t=null==t?t:JSON.stringify(t)}return t},fromAttribute(t,i){let s=t;switch(i){case Boolean:s=null!==t;break;case Number:s=null===t?null:Number(t);break;case Object:case Array:try{s=JSON.parse(t)}catch(t){s=null}}return s}},M=(t,i)=>!w(t,i),U={attribute:!0,type:String,converter:A,reflect:!1,hasChanged:M};Symbol.metadata??=Symbol("metadata"),k.litPropertyMetadata??=new WeakMap;class B extends HTMLElement{static addInitializer(t){this._$Ei(),(this.l??=[]).push(t)}static get observedAttributes(){return this.finalize(),this._$Eh&&[...this._$Eh.keys()]}static createProperty(t,i=U){if(i.state&&(i.attribute=!1),this._$Ei(),this.elementProperties.set(t,i),!i.noAccessor){const s=Symbol(),e=this.getPropertyDescriptor(t,s,i);void 0!==e&&S(this.prototype,t,e)}}static getPropertyDescriptor(t,i,s){const{get:e,set:o}=g(this.prototype,t)??{get(){return this[i]},set(t){this[i]=t}};return{get(){return e?.call(this)},set(i){const h=e?.call(this);o.call(this,i),this.requestUpdate(t,h,s)},configurable:!0,enumerable:!0}}static getPropertyOptions(t){return this.elementProperties.get(t)??U}static _$Ei(){if(this.hasOwnProperty(O("elementProperties")))return;const t=$(this);t.finalize(),void 0!==t.l&&(this.l=[...t.l]),this.elementProperties=new Map(t.elementProperties)}static finalize(){if(this.hasOwnProperty(O("finalized")))return;if(this.finalized=!0,this._$Ei(),this.hasOwnProperty(O("properties"))){const t=this.properties,i=[...C(t),...P(t)];for(const s of i)this.createProperty(s,t[s])}const t=this[Symbol.metadata];if(null!==t){const i=litPropertyMetadata.get(t);if(void 0!==i)for(const[t,s]of i)this.elementProperties.set(t,s)}this._$Eh=new Map;for(const[t,i]of this.elementProperties){const s=this._$Eu(t,i);void 0!==s&&this._$Eh.set(s,t)}this.elementStyles=this.finalizeStyles(this.styles)}static finalizeStyles(t){const i=[];if(Array.isArray(t)){const s=new Set(t.flat(1/0).reverse());for(const t of s)i.unshift(v(t))}else void 0!==t&&i.push(v(t));return i}static _$Eu(t,i){const s=i.attribute;return!1===s?void 0:"string"==typeof s?s:"string"==typeof t?t.toLowerCase():void 0}constructor(){super(),this._$Ep=void 0,this.isUpdatePending=!1,this.hasUpdated=!1,this._$Em=null,this._$Ev()}_$Ev(){this._$ES=new Promise((t=>this.enableUpdating=t)),this._$AL=new Map,this._$E_(),this.requestUpdate(),this.constructor.l?.forEach((t=>t(this)))}addController(t){(this._$EO??=new Set).add(t),void 0!==this.renderRoot&&this.isConnected&&t.hostConnected?.()}removeController(t){this._$EO?.delete(t)}_$E_(){const t=new Map,i=this.constructor.elementProperties;for(const s of i.keys())this.hasOwnProperty(s)&&(t.set(s,this[s]),delete this[s]);t.size>0&&(this._$Ep=t)}createRenderRoot(){const t=this.shadowRoot??this.attachShadow(this.constructor.shadowRootOptions);return y(t,this.constructor.elementStyles),t}connectedCallback(){this.renderRoot??=this.createRenderRoot(),this.enableUpdating(!0),this._$EO?.forEach((t=>t.hostConnected?.()))}enableUpdating(t){}disconnectedCallback(){this._$EO?.forEach((t=>t.hostDisconnected?.()))}attributeChangedCallback(t,i,s){this._$AK(t,s)}_$EC(t,i){const s=this.constructor.elementProperties.get(t),e=this.constructor._$Eu(t,s);if(void 0!==e&&!0===s.reflect){const o=(void 0!==s.converter?.toAttribute?s.converter:A).toAttribute(i,s.type);this._$Em=t,null==o?this.removeAttribute(e):this.setAttribute(e,o),this._$Em=null}}_$AK(t,i){const s=this.constructor,e=s._$Eh.get(t);if(void 0!==e&&this._$Em!==e){const t=s.getPropertyOptions(e),o="function"==typeof t.converter?{fromAttribute:t.converter}:void 0!==t.converter?.fromAttribute?t.converter:A;this._$Em=e,this[e]=o.fromAttribute(i,t.type),this._$Em=null}}requestUpdate(t,i,s){if(void 0!==t){if(s??=this.constructor.getPropertyOptions(t),!(s.hasChanged??M)(this[t],i))return;this.P(t,i,s)}!1===this.isUpdatePending&&(this._$ES=this._$ET())}P(t,i,s){this._$AL.has(t)||this._$AL.set(t,i),!0===s.reflect&&this._$Em!==t&&(this._$Ej??=new Set).add(t)}async _$ET(){this.isUpdatePending=!0;try{await this._$ES}catch(t){Promise.reject(t)}const t=this.scheduleUpdate();return null!=t&&await t,!this.isUpdatePending}scheduleUpdate(){return this.performUpdate()}performUpdate(){if(!this.isUpdatePending)return;if(!this.hasUpdated){if(this.renderRoot??=this.createRenderRoot(),this._$Ep){for(const[t,i]of this._$Ep)this[t]=i;this._$Ep=void 0}const t=this.constructor.elementProperties;if(t.size>0)for(const[i,s]of t)!0!==s.wrapped||this._$AL.has(i)||void 0===this[i]||this.P(i,this[i],s)}let t=!1;const i=this._$AL;try{t=this.shouldUpdate(i),t?(this.willUpdate(i),this._$EO?.forEach((t=>t.hostUpdate?.())),this.update(i)):this._$EU()}catch(i){throw t=!1,this._$EU(),i}t&&this._$AE(i)}willUpdate(t){}_$AE(t){this._$EO?.forEach((t=>t.hostUpdated?.())),this.hasUpdated||(this.hasUpdated=!0,this.firstUpdated(t)),this.updated(t)}_$EU(){this._$AL=new Map,this.isUpdatePending=!1}get updateComplete(){return this.getUpdateComplete()}getUpdateComplete(){return this._$ES}shouldUpdate(t){return!0}update(t){this._$Ej&&=this._$Ej.forEach((t=>this._$EC(t,this[t]))),this._$EU()}updated(t){}firstUpdated(t){}}B.elementStyles=[],B.shadowRootOptions={mode:"open"},B[O("elementProperties")]=new Map,B[O("finalized")]=new Map,E?.({ReactiveElement:B}),(k.reactiveElementVersions??=[]).push("2.0.4");
/**
 * @license
 * Copyright 2017 Google LLC
 * SPDX-License-Identifier: BSD-3-Clause
 */
class T extends B{constructor(){super(...arguments),this.renderOptions={host:this},this._$Do=void 0}createRenderRoot(){const t=super.createRenderRoot();return this.renderOptions.renderBefore??=t.firstChild,t}update(t){const s=this.render();this.hasUpdated||(this.renderOptions.isConnected=this.isConnected),super.update(t),this._$Do=i(s,this.renderRoot,this.renderOptions)}connectedCallback(){super.connectedCallback(),this._$Do?.setConnected(!0)}disconnectedCallback(){super.disconnectedCallback(),this._$Do?.setConnected(!1)}render(){return s}}T._$litElement$=!0,T.finalized=!0,globalThis.litElementHydrateSupport?.({LitElement:T});const N=globalThis.litElementPolyfillSupport;N?.({LitElement:T}),(globalThis.litElementVersions??=[]).push("4.1.1");let R=class extends l{constructor(){super(...arguments),this.placement="top",this.distance=18,this.preventOverlayClose=!1,this.arrow=!0,this.title="",this.open=!1,this.isShow=!1,this.arrowPlacement="anchor",this.portalManager=null,this.enableVisibilityEffect=!1,this.enableTabClose=!1,this.hoist=!1,this.portal=!1,this.flip=!1,this.handleClick=()=>{this.isShow=!this.isShow;document.querySelectorAll("nile-popover").forEach((t=>{t!==this&&(t.isShow=!1)}))},this.handleDocumentClick=()=>{this.isShow&&!this.preventOverlayClose&&(this.isShow=!1)}}static get styles(){return[r]}connectedCallback(){super.connectedCallback(),this.emit("nile-init"),document.addEventListener("click",this.handleDocumentClick),requestAnimationFrame((()=>{this.portal&&!this.portalManager&&(this.portalManager=new c(this))}))}disconnectedCallback(){super.disconnectedCallback(),this.emit("nile-destroy"),this.visibilityManager?.cleanup(),document.removeEventListener("click",this.handleDocumentClick),this.portalManager&&(this.portalManager.cleanupPortalAppend(),this.portalManager=null)}async firstUpdated(t){await this.updateComplete;const i=this.renderRoot.querySelector('slot[name="anchor"]'),s=i?.assignedElements({flatten:!0})[0];this.visibilityManager=new p({host:this,target:s||null,enableVisibilityEffect:this.enableVisibilityEffect,enableTabClose:this.enableTabClose,isOpen:()=>this.isShow,onAnchorOutOfView:()=>{this.isShow=!1,this.emit("nile-visibility-change",{visible:!1,reason:"anchor-out-of-view"})},onDocumentHidden:()=>{this.isShow=!1,this.emit("nile-visibility-change",{visible:!1,reason:"document-hidden"})},emit:(t,i)=>this.emit(`nile-${t}`,i)})}updated(t){t.has("open")&&(this.isShow=this.open),t.has("portal")&&(this.portal&&!this.portalManager?this.portalManager=new c(this):!this.portal&&this.portalManager&&(this.portalManager.cleanupPortalAppend(),this.portalManager=null))}handleShowHide(){this.isShow?(this.emit("nile-show"),this.visibilityManager?.setup(),this.portal&&this.portalManager?this.portalManager.setupPortalAppend():this.portal&&!this.portalManager&&(this.portalManager=new c(this),this.portalManager.setupPortalAppend())):(this.emit("nile-hide"),this.visibilityManager?.cleanup(),this.portal&&this.portalManager&&this.portalManager.cleanupPortalAppend())}render(){const t=this.isShow&&!(this.portal&&this.portalManager);return e`
      <nile-popup
        active
        .arrow="${this.arrow&&this.isShow&&!this.portal}"
        distance="${this.distance}"
        placement="${this.placement}"
        @click=${t=>t.stopPropagation()}
        arrowPlacement="${this.arrowPlacement}"
        .flip="${this.flip}"
        shift
        strategy=${this.hoist?"fixed":"absolute"}
      >
        <slot
          slot="anchor"
          name="anchor"
          aria-describedby="tooltip"
          @click=${this.handleClick}
        ></slot>
        ${t?e` <div part="popover" class="popover__box">
              <slot name="title" class="popover__title">${this.title}</slot>
              <slot part="base"></slot>
              <slot name="action" class="popover__action"> </slot>
            </div>`:e``}
      </nile-popup>
    `}updatePortalContent(){this.portalManager&&this.portalManager.updatePortalAppendPosition()}forceReposition(){this.portalManager&&this.portalManager.forceReposition()}getCurrentPlacement(){return this.portalManager?this.portalManager.getCurrentPlacement():this.placement}isPositioningOptimal(){return!this.portalManager||this.portalManager.isPositioningOptimal()}};t([o()],R.prototype,"placement",void 0),t([o({type:Number})],R.prototype,"distance",void 0),t([o({type:Boolean,reflect:!0})],R.prototype,"preventOverlayClose",void 0),t([o({type:Boolean,attribute:"arrow"})],R.prototype,"arrow",void 0),t([o({type:String})],R.prototype,"title",void 0),t([o({type:Boolean})],R.prototype,"open",void 0),t([h()],R.prototype,"isShow",void 0),t([o({attribute:"arrow-placement"})],R.prototype,"arrowPlacement",void 0),t([o({type:Boolean,reflect:!0})],R.prototype,"enableVisibilityEffect",void 0),t([o({type:Boolean,reflect:!0})],R.prototype,"enableTabClose",void 0),t([o({type:Boolean})],R.prototype,"hoist",void 0),t([o({type:Boolean})],R.prototype,"portal",void 0),t([o({type:Boolean})],R.prototype,"flip",void 0),t([a("isShow")],R.prototype,"handleShowHide",null),R=t([n("nile-popover")],R);export{R as N};
