(function(t,e){typeof exports==="object"&&typeof module!=="undefined"?module.exports=e():typeof define==="function"&&define.amd?define(e):(t=typeof globalThis!=="undefined"?globalThis:t||self,t.Popup=e())})(this,(function(){"use strict";var t=25,e=5,i=10,n=10;var o={};o.bottom=function t(e,o){return{shape:[-i,-n,i,-n],pos:[e/2,o+n]}};o.top=function t(e,o){return{shape:[-i,n,i,n],pos:[e/2,-n]}};o.left=function t(e,o){return{shape:[n,i,n,-i],pos:[-n,o/2]}};o.right=function t(e,o){return{shape:[-n,i,-n,-i],pos:[e+n,o/2]}};o.topLeft=function t(i,o){return{shape:[n+e,n,n,n+e],pos:[-n,-n]}};o.bottomLeft=function t(i,o){return{shape:[n+e,-n,n,-n-e],pos:[-n,o+n]}};o.topRight=function t(i,o){return{shape:[-n-e,n,-n,n+e],pos:[i+n,-n]}};o.bottomRight=function t(i,o){return{shape:[-n-e,-n,-n,-n-e],pos:[i+n,o+n]}};function r(t,o,r,l,p,s,a){var h=r-t/2-n,u=r+t/2+n,f=t/2+Math.min(0,h-p.left)+Math.max(0,u-p.right),d;if(f-i<e){d=[-f,(-n-e)*s,Math.max(i,e-f),-n*s]}else if(f+i>t-e){d=[Math.min(-i,t-f-e),-n*s,Math.min(i,t-f),(-n-e)*s]}else{d=[-i,-n*s,i,-n*s]}return{pos:[f,a],shape:d}}function l(t,o,r,l,p,s,a){var h=l-o/2-n,u=l+o/2+n,f=o/2+Math.min(0,h-p.top)+Math.max(0,u-p.bottom),d;if(f-i<e){d=[(-n-e)*s,-f,-n*s,Math.max(i,e-f)]}else if(f+i>o-e){d=[-n*s,Math.min(-i,o-f-e),(-n-e)*s,Math.min(i,o-f)]}else{d=[-n*s,-i,-n*s,i]}return{pos:[a,f],shape:d}}o.bottomFlexible=function t(e,i,o,l,p){return r(e,i,o,l,p,+1,i+n)};o.topFlexible=function t(e,i,o,l,p){return r(e,i,o,l,p,-1,-n)};o.rightFlexible=function t(e,i,o,r,p){return l(e,i,o,r,p,+1,e+n)};o.leftFlexible=function t(e,i,o,r,p){return l(e,i,o,r,p,-1,-n)};function p(e,i,n,r,l,p){var s=o[e](i,n,r,l,p),a=r-t-s.pos[0],h=l-t-s.pos[1];return{left:a,top:h,right:a+i+2*t,bottom:h+n+2*t}}function s(e,i,n,r,l,p,s,a,h,u){var f=o[e](r,l,a,h,u);i.left=p-t-f.pos[0]+"px";i.top=s-t-f.pos[1]+"px";n.setAttribute("d","M0,0L"+f.shape.join(",")+"Z");n.setAttribute("transform","translate("+(f.pos[0]+t)+","+(f.pos[1]+t)+")")}function a(){var e=this;e.is_visible=true;function i(t){if(e._maxWidth.match(/^\d+(?:\.\d+)?%$/)){return t.width*parseFloat(e._maxWidth)/100}if(e._maxWidth.match(/^\d+(?:\.\d+)?(?:px)?$/)){return parseFloat(e._maxWidth)}if(e._maxWidth!=null){console.error("Popup: Unknown value for maxWidth: "+e._maxWidth)}return t.width}if(!e._point){console.error("Popup: cannot draw popup till point() has been specified");return}var o=document.documentElement.getBoundingClientRect(),r=e._point[0],l=e._point[1],a=e._container.getBoundingClientRect();if(r<a.left)r=a.left;else if(r>a.right)r=a.right;if(l<a.top)l=a.top;else if(l>a.bottom)l=a.bottom;var h=r-o.left,u=l-o.top;var f=e._getElement(),d=f.style,c=f.querySelector(".flourish-popup-svg"),m=c.querySelector("g"),g=m.querySelector("rect"),_=m.querySelector("path"),v=f.querySelector(".flourish-popup-content");d.display="block";v.style.maxWidth=i(a)+"px";if(e._inner_html!=e._html){v.innerHTML=e._inner_html=e._html}var b=v.getBoundingClientRect(),x,y;do{x=Math.ceil(b.width);y=Math.ceil(b.height);d.width=x+2*t+"px";d.height=y+2*t+"px";b=v.getBoundingClientRect()}while(x!=Math.ceil(b.width)||y!=Math.ceil(b.height));g.setAttribute("width",x);g.setAttribute("height",y);c.setAttribute("width",x+2*t);c.setAttribute("height",y+2*t);var w=t-n;var M=null,C=null,E=null,F=Infinity,A=Infinity,q,k;for(var S=0;S<e._directions.length;S++){var W=e._directions[S],R=p(W,x,y,r,l,a),L=Math.max(0,Math.floor(a.left)-R.left-w)+Math.max(0,R.right-Math.ceil(a.right)-w),z=Math.max(0,Math.floor(a.top)-R.top-w)+Math.max(0,R.bottom-Math.ceil(a.bottom)-w);if(L==0&&z==0){M=W;break}if(L<F||L==F&&z<q){F=L;q=z;C=W}if(z<A||z==A&&L<k){A=z;k=L;E=W}}if(M){W=M}else if(e._fallbackFit=="horizontal"){W=C}else if(e._fallbackFit=="vertical"){W=E}else{console.warn("Popup: failed to point box of size ("+x+", "+y+")"+" at ("+r+", "+l+") within ("+a.left+", "+a.top+", "+a.right+", "+a.bottom+")");W=e._directions[0]}s(W,d,_,x,y,h,u,r,l,a);return e}function h(t,e,i){var n=document.createElementNS("http://www.w3.org/2000/svg",t);var o;if(e)for(o in e)n.setAttribute(o,e[o]);var r=n.style;if(i)for(o in i)r[o]=i[o];return n}function u(){var i=this;var n=i.element;if(!n){n=i.element=document.createElement("div");var o="flourish-popup-"+i.unique_id;n.className="flourish-popup";n.id=o;var r=n.style;r.display="none";r.margin=r.padding=0;r.position="absolute";r.width="80px";r.height="40px";r.boxSizing="border-box";n.addEventListener("click",(function(t){i.fire("click",t)}),false);var l=h("svg",{class:"flourish-popup-svg"},{position:"absolute",top:0,left:0,bottom:0,right:0});var p=h("filter",{id:"dropshadow-"+i.unique_id,height:"130%"});p.appendChild(h("feGaussianBlur",{in:"SourceAlpha",stdDeviation:5}));p.appendChild(h("feOffset",{dx:0,dy:2,result:"offsetblur"}));var s=h("feComponentTransfer");s.appendChild(h("feFuncA",{type:"linear",slope:.2}));p.appendChild(s);var a=h("feMerge");p.appendChild(a);a.appendChild(h("feMergeNode"));a.appendChild(h("feMergeNode",{in:"SourceGraphic"}));l.appendChild(p);var u=h("g",{filter:"url(#dropshadow-"+i.unique_id+")",fill:"white",stroke:"none"});u.appendChild(h("rect",{x:t,y:t,rx:e}));u.appendChild(h("path"));l.appendChild(u);n.appendChild(l);var f=document.createElement("div");f.className="flourish-popup-content";r=f.style;r.position="absolute";r.top=r.left=t+"px";r.padding="10px";n.appendChild(f);i._getConstrainer().appendChild(n)}i._resizeConstrainer();return n}var f,d;function c(){if(f)return f;f=document.createElement("div");f.id="flourish-popup-constrainer";f.setAttribute("aria-hidden",true);d=f.style;d.overflow="hidden";d.pointerEvents="none";d.position="absolute";d.left="0";d.top="0";d.margin="0";d.padding="0";document.body.appendChild(f);this._resizeConstrainer();return f}function m(){if(!f)c.call(this);var t=d.display;d.display="none";d.width=document.documentElement.scrollWidth+"px";d.height=document.documentElement.scrollHeight+"px";d.display=t}var g="2.1.5";var _={container:document.body,maxWidth:"70%",point:null,html:null,directions:["bottom","top","left","right","topLeft","bottomLeft","topRight","bottomRight","bottomFlexible","topFlexible","leftFlexible","rightFlexible"],fallbackFit:"horizontal"};function v(){let t;let e;if("Flourish"in window){t=window.Flourish;e="__popup_next_unique_id"}else{t=window;e="__flourish_popup_next_unique_id"}if(!t[e]){return t[e]=1e3}t[e]+=1;return t[e]}function b(){this.unique_id=v();this.is_visible=true;this.element=null;for(var t in _)this["_"+t]=_[t];this.handlers={click:[]}}function x(t){b.prototype[t]=function(e){if(typeof e=="undefined")return this["_"+t];this["_"+t]=e;return this}}for(var y in _)x(y);b.prototype.point=function(t,e){if(typeof t=="undefined")return this._point;if(Array.isArray(t))this._point=[t[0],t[1]];else if(typeof e!="undefined")this._point=[t,e];else if(t instanceof HTMLElement||t instanceof SVGElement){var i=t.getBoundingClientRect();this._point=[Math.floor(i.left+i.width/2),Math.floor(i.top+i.height/2)]}else{console.error("Popup: could not understand argument")}return this};b.prototype.directions=function(t){if(typeof t==="undefined")return this._directions;if(typeof t==="string")t=[t];this._directions=t.slice();return this};function w(t){return t.replace(/[&<>]/g,(function(t){return{"&":"&amp;","<":"&lt;",">":"&gt;"}[t]}))}b.prototype.text=function t(e){this._html=w(e);return this};b.prototype.on=function t(e,i){if(!(e in this.handlers))throw new Error("Popup.on: No such event: "+e);this.handlers[e].push(i);return this};b.prototype.fire=function t(e,i){if(!(e in this.handlers))throw new Error("Popup.fire: No such event: "+e);var n=this.handlers[e];for(var o=0;o<n.length;o++){n[o].call(this,i)}return this};b.prototype._getElement=u;b.prototype._getConstrainer=c;b.prototype._resizeConstrainer=m;b.prototype.draw=a;b.prototype.hide=function t(){if(!this.is_visible)return this;this.is_visible=false;this._getElement().style.display="none";return this};function M(){return new b}M.version=g;return M}));