/**
 * Copyright 2018 The Pennsylvania State University
 * @license Apache-2.0, see License.md for full text.
 */
import{LitElement as t}from"../../../lit/index.js";window.AbsolutePositionStateManager=window.AbsolutePositionStateManager||{},window.AbsolutePositionStateManager.requestAvailability=()=>{if(!window.AbsolutePositionStateManager.instance){window.AbsolutePositionStateManager.instance=document.createElement("absolute-position-state-manager");let t=window.AbsolutePositionStateManager.instance;document.body.appendChild(t)}return window.AbsolutePositionStateManager.instance};class AbsolutePositionStateManager extends t{static get tag(){return"absolute-position-state-manager"}static get properties(){return{elements:{type:Array},__observer:{type:Object},__timeout:{type:Object},__timeout2:{type:Object}}}constructor(){super(),this.elements=[],this.__timeout=!1,this.__observer=new MutationObserver((t=>this.checkMutations(t)))}loadElement(t){this.elements.length<1&&(this.__observer.observe(document,{attributes:!0,childList:!0,subtree:!0,characterData:!0}),this.updateElements(),document.addEventListener("load",this.updateElements),window.addEventListener("resize",this._handleResize)),this.elements.push(t),t.style.top=0,t.style.left=0,this.positionElement(t)}unloadElement(t){this.elements.filter((e=>e===t)),this.elements.length<1&&this.removeEventListeners()}_handleScroll(){this.__timeout2&&clearTimeout(this.__timeout2),this.__timeout2=setTimeout(window.AbsolutePositionStateManager.instance.updateStickyElements(),1e3)}_handleResize(){this.__timeout&&clearTimeout(this.__timeout),this.__timeout=setTimeout(window.AbsolutePositionStateManager.instance.updateElements(),250)}checkMutations(t){let e=!1;t.forEach((t=>{e||(e=e||!("attributes"===t.type&&"style"===t.attributeName&&this.elements.includes(t.target)))})),e&&this.updateElements()}findTarget(t){let e="#"+t.for,i=t.target,o=t;for(;t.for&&!i&&o&&o.parentNode&&o!==document;)o=o.parentNode,i=o?o.querySelector(e):void 0,11===o.nodeType&&(o=o.host),i=!i&&o?o.querySelector(e):i;return i}removeEventListeners(){this.__observer&&this.__observer.disconnect&&this.__observer.disconnect(),document.removeEventListener("load",this.updateElements),window.removeEventListener("resize",this._handleResize),this.__watchSticky&&window.removeEventListener("scroll",this._handleScroll)}updateElements(){this.elements.forEach((t=>this.positionElement(t))),this.loadSticky()}updateStickyElements(){this.elements.forEach((t=>{t.sticky&&this.positionElement(t)}))}loadSticky(){!this.__watchSticky&&this.elements.filter((t=>t.sticky)).length>0?(this.__watchSticky=!0,window.addEventListener("scroll",this._handleScroll)):this.__watchSticky&&this.elements.filter((t=>t.sticky)).length<1&&(window.removeEventListener("scroll",this._handleScroll),this.__watchSticky=!1)}_getParentNode(t){let e=t.parentNode;return null!=e&&e.nodeType===Node.DOCUMENT_FRAGMENT_NODE&&(e=e.host),e}positionElement(t){t.position||(t.position="bottom"),t.style.top||(t.style.top="0px"),t.style.left||(t.style.left="0px");let e=this.findTarget(t),i=t.offsetParent,o=!e||e.getBoundingClientRect();if(!e||!i)return;t.justify&&(t.style.width=`${o.width}px`);let s=parseFloat(t.offset),n=document.body.getBoundingClientRect(),l=i.getBoundingClientRect(),a=t.getBoundingClientRect(),vertical=(e=t.position)=>"left"!==e&&"right"!==e,setAlign=(e=vertical(t.position))=>{let i,pxToNum=t=>parseFloat(t.replace("px","")),s=e?pxToNum(t.style.left)-a.left:pxToNum(t.style.top)-a.top,l=e?"left":"top",distance=t=>e?t.width:t.height,r=s+distance(n)-distance(a),h=s;return"end"===t.positionAlign?h+=o[l]-distance(a)+distance(o):"start"===t.positionAlign?h+=o[l]:h+=o[l]-distance(a)/2+distance(o)/2,i=t.fitToVisibleBounds?Math.max(s,Math.min(r,h)):h,i},getCoord=(e=t.position)=>{let i,pxToNum=t=>parseFloat(t.replace("px","")),n=vertical(e)?pxToNum(t.style.top)-a.top:pxToNum(t.style.left)-a.left,l="visible"==window.getComputedStyle(t,null).overflowY?Math.max(a.height,t.scrollHeight):a.height,r="visible"==window.getComputedStyle(t,null).overflowX?Math.max(a.width,t.scrollWidth):a.width;return i="top"===e?o.top+n-l-s:"left"===e?o.left+n-r-s:o[e]+n+s,i},isFit=(e=t.position)=>{let distance=t=>vertical(e)?a.height+s:a.width+s;return((e=t.position)=>"left"===e||"top"===e)(e)?o[e]-n[e]>distance:n[e]-o[e]>distance},r=!1!==t.fitToVisibleBounds&&!isFit(t.position),h={top:["bottom","left","right"],left:["right","top","bottom"],bottom:["top","right","left"],right:["left","bottom","top"]};t.style.position="absolute",r&&isFit(h[t.position][0])?t.position=h[t.position][0]:r&&isFit(h[t.position][1])?t.position=h[t.position][1]:r&&isFit(h[t.position][2])&&(t.position=h[t.position][2]);let d=vertical(t.position)?getCoord():setAlign(),p=vertical(t.position)?setAlign():getCoord();if(t.sticky){let e=window.pageYOffset||(document.documentElement||document.body.parentNode||document.body).scrollTop,s=window.innerHeight,n=0===a.height&&t.children&&t.children[0]?t.children[0].offsetHeight:a.height,l=o.top-a.height<0&&o.top+o.height>20+n,r=o.top+o.height+a.height>s&&o.top<s-n;"top"===t.position&&l&&(d=e-i.offsetTop+(n-a.height)),"bottom"===t.position&&r&&(d=e+s-i.offsetTop-n)}t.style.top=d+"px",t.style.left=p+"px",t.__positions={self:a,parent:l,target:o}}disconnectedCallback(){this.removeEventListeners(),super.disconnectedCallback()}}window.customElements.define(AbsolutePositionStateManager.tag,AbsolutePositionStateManager);export{AbsolutePositionStateManager};