/**
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at
http://polymer.github.io/LICENSE.txt The complete set of authors may be found at
http://polymer.github.io/AUTHORS.txt The complete set of contributors may be
found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as
part of the polymer project is also subject to an additional IP rights grant
found at http://polymer.github.io/PATENTS.txt
*/
import"../polymer/polymer-legacy.js";import"./iron-overlay-backdrop.js";import{IronA11yKeysBehavior as e}from"../iron-a11y-keys-behavior/iron-a11y-keys-behavior.js";import{dom as r}from"../polymer/lib/legacy/polymer.dom.js";import*as t from"../polymer/lib/utils/gestures.js";export class IronOverlayManagerClass{constructor(){this._overlays=[],this._minimumZ=101,this._backdropElement=null,t.addListener(document.documentElement,"tap",(function(){})),document.addEventListener("tap",this._onCaptureClick.bind(this),!0),document.addEventListener("focus",this._onCaptureFocus.bind(this),!0),document.addEventListener("keydown",this._onCaptureKeyDown.bind(this),!0)}get backdropElement(){return this._backdropElement||(this._backdropElement=document.createElement("iron-overlay-backdrop")),this._backdropElement}get deepActiveElement(){var e=document.activeElement;for(e&&e instanceof Element!=!1||(e=document.body);e.root&&r(e.root).activeElement;)e=r(e.root).activeElement;return e}_bringOverlayAtIndexToFront(e){var r=this._overlays[e];if(r){var t=this._overlays.length-1,a=this._overlays[t];if(a&&this._shouldBeBehindOverlay(r,a)&&t--,!(e>=t)){var s=Math.max(this.currentOverlayZ(),this._minimumZ);for(this._getZ(r)<=s&&this._applyOverlayZ(r,s);e<t;)this._overlays[e]=this._overlays[e+1],e++;this._overlays[t]=r}}}addOrRemoveOverlay(e){e.opened?this.addOverlay(e):this.removeOverlay(e)}addOverlay(e){var r=this._overlays.indexOf(e);if(r>=0)return this._bringOverlayAtIndexToFront(r),void this.trackBackdrop();var t=this._overlays.length,a=this._overlays[t-1],s=Math.max(this._getZ(a),this._minimumZ),i=this._getZ(e);if(a&&this._shouldBeBehindOverlay(e,a)){this._applyOverlayZ(a,s),t--;var o=this._overlays[t-1];s=Math.max(this._getZ(o),this._minimumZ)}i<=s&&this._applyOverlayZ(e,s),this._overlays.splice(t,0,e),this.trackBackdrop()}removeOverlay(e){var r=this._overlays.indexOf(e);-1!==r&&(this._overlays.splice(r,1),this.trackBackdrop())}currentOverlay(){var e=this._overlays.length-1;return this._overlays[e]}currentOverlayZ(){return this._getZ(this.currentOverlay())}ensureMinimumZ(e){this._minimumZ=Math.max(this._minimumZ,e)}focusOverlay(){var e=this.currentOverlay();e&&e._applyFocus()}trackBackdrop(){var e=this._overlayWithBackdrop();(e||this._backdropElement)&&(this.backdropElement.style.zIndex=this._getZ(e)-1,this.backdropElement.opened=!!e,this.backdropElement.prepare())}getBackdrops(){for(var e=[],r=0;r<this._overlays.length;r++)this._overlays[r].withBackdrop&&e.push(this._overlays[r]);return e}backdropZ(){return this._getZ(this._overlayWithBackdrop())-1}_overlayWithBackdrop(){for(var e=this._overlays.length-1;e>=0;e--)if(this._overlays[e].withBackdrop)return this._overlays[e]}_getZ(e){var r=this._minimumZ;if(e){var t=Number(e.style.zIndex||window.getComputedStyle(e).zIndex);t==t&&(r=t)}return r}_setZ(e,r){e.style.zIndex=r}_applyOverlayZ(e,r){this._setZ(e,r+2)}_overlayInPath(e){e=e||[];for(var r=0;r<e.length;r++)if(e[r]._manager===this)return e[r]}_onCaptureClick(e){var t=this._overlays.length-1;if(-1!==t)for(var a,s=r(e).path;(a=this._overlays[t])&&this._overlayInPath(s)!==a&&(a._onCaptureClick(e),a.allowClickThrough);)t--}_onCaptureFocus(e){var r=this.currentOverlay();r&&r._onCaptureFocus(e)}_onCaptureKeyDown(r){var t=this.currentOverlay();t&&(e.keyboardEventMatchesKeys(r,"esc")?t._onCaptureEsc(r):e.keyboardEventMatchesKeys(r,"tab")&&t._onCaptureTab(r))}_shouldBeBehindOverlay(e,r){return!e.alwaysOnTop&&r.alwaysOnTop}}export const IronOverlayManager=new IronOverlayManagerClass;