"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const l=require("react/jsx-runtime"),x=require("react"),I=require("d3"),g=require("semantic-ui-react"),O=require("react-intl");function _(c){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(c){for(const i in c)if(i!=="default"){const o=Object.getOwnPropertyDescriptor(c,i);Object.defineProperty(t,i,o.get?o:{enumerable:!0,get:()=>c[i]})}}return t.default=c,Object.freeze(t)}const z=_(I);class k extends x.Component{constructor(t){super(t),this.zoomRef=x.createRef(),this.lastInternalZoom=null,this.lastUserTransform=null,this.zoomed=this.zoomed.bind(this),this.zoomEnd=this.zoomEnd.bind(this),this.zoomIn=this.zoomIn.bind(this),this.zoomOut=this.zoomOut.bind(this),this.reset=this.reset.bind(this),this.fullView=this.fullView.bind(this),this.transition2fullView=this.transition2fullView.bind(this),this._fullView=this._fullView.bind(this),this.initialized=!1,this.zoom=z.zoom().scaleExtent([.1,300]).on("zoom",this.zoomed).on("end",this.zoomEnd)}componentDidMount(){const{zoomEnabled:t=!0}=this.props,i=this.getSelection();this._fullView(!1),t&&(i.on("dblclick.zoom",null),i.on("dblclick",o=>{o.preventDefault(),i.transition().duration(250).call(this.zoom.scaleBy,1.5)}),i.on("wheel.zoom",null),i.on("wheel",o=>{o.preventDefault();const e=o.deltaY>0?1/1.5:1.5;i.transition().duration(250).call(this.zoom.scaleBy,e)}),i.call(this.zoom)),this.fullView()}componentDidUpdate(t){const i=this.getSelection(),{zoomEnabled:o,initialPosition:e,readyState:u,height:f,width:h,selectedPoint:s,projection:a,editing:r}=this.props;!this.initialized&&this.props.readyToZoom&&(this.fullView(),this.initialized=!0);const d=JSON.stringify(t.initialPosition)!==JSON.stringify(e);if(console.log("zoomChanged",d),t.zoomEnabled!==o||d&&!r)if(o&&i||r){if(e&&this.lastInternalZoom){const n=(V,j=3)=>Number(V.toFixed(j)),{x:m,y:p,k:b}=e,{x:w,y,k:S}=this.lastInternalZoom;if(Math.abs(n(m)-n(w))<1&&Math.abs(n(p)-n(y))<1&&Math.abs(n(b)-n(S))<.001){this.lastInternalZoom=null;return}}i.call(this.zoom).on(".zoom",this.zoom),this.transition2fullView()}else i&&i.on(".zoom",null);!t.readyState&&u&&this.fullView(),(t.height!==f||t.width!==h)&&this.fullView(),s!=t.selectedPoint&&s&&this.zoomTo(a([s.y,s.x]))}zoomed(t){var o,e;const i=this.getSelection();i&&(i.selectAll("g.zoomable").attr("transform",t.transform),(e=(o=this.props).onZoomed)==null||e.call(o,t.transform),this.props.editing&&t.sourceEvent&&(this.lastUserTransform=t.transform))}zoomEnd(t){var s,a;const{identifier:i,editing:o,width:e,height:u,postMessageOrigin:f="*"}=this.props,h=this.lastUserTransform||t.transform;if(this.lastUserTransform=null,(a=(s=this.props).onZoomed)==null||a.call(s,h),o){const r=(w,y=3)=>Number(w.toFixed(y)),{x:d,y:n,k:m}=h,p={x:r(d),y:r(n),k:r(m)};this.lastInternalZoom=p;const b={type:`d3_map_${i}`,value:{...p,width:e,height:u}};window.parent.postMessage(b,f)}}zoomIn(){const t=this.getSelection();if(t){const i=this.props.editing?0:this.props.transitionDuration||500;t.transition().duration(i).call(this.zoom.scaleBy,2)}}zoomTo(t){const i=z.zoomIdentity.translate(this.props.width/2,this.props.height/2).scale(200).translate(-t[0],-t[1]),o=this.getSelection();this.props.editing||this.props.transitionDuration,o.transition().duration(750).call(this.zoom.transform,i)}zoomOut(){const t=this.getSelection();if(t){const i=this.props.editing?0:this.props.transitionDuration||500;t.transition().duration(i).call(this.zoom.scaleBy,.5)}}reset(){const t=this.getSelection();t&&(this.props.editing?t.call(this.zoom.transform,z.zoomIdentity.translate(0,0).scale(1)):this.transition2fullView())}getSelection(){return z.select(this.zoomRef.current.parentNode.querySelector("svg"))}_fullView(t=!0){const{initialPosition:i,width:o,height:e,transform:u}=this.props;if(!i)return;const{x:f=100,y:h=23,k:s=1,width:a,height:r}=i;if(!a||!r||!s)return;const d=f+(o-a)/2*(1-s),n=h+(e-r)/2*(1-s),m=this.getSelection();m&&m.transition().duration(t?750:0).attr("transform",u).call(this.zoom.transform,z.zoomIdentity.translate(d,n).scale(s))}transition2fullView(){this._fullView(!0)}fullView(){this._fullView(!1)}render(){const{editing:t,zoomEnabled:i=!0}=this.props;return l.jsx("div",{ref:this.zoomRef,className:`zoom ignore ${i?"":"disabled"}`,children:(t||i)&&l.jsxs("div",{children:[l.jsx("div",{className:"button plus",onClick:this.zoomIn,children:l.jsx(g.Icon,{name:"plus",size:"small"})}),l.jsx("div",{className:"button minus",onClick:this.zoomOut,children:l.jsx(g.Icon,{name:"minus",size:"small"})}),l.jsx(g.Popup,{content:l.jsx(O.FormattedMessage,{id:"map.reset.tooltip",defaultMessage:"Reset zoom"}),trigger:l.jsx("div",{className:"button reset",onClick:this.reset,children:l.jsx(g.Icon,{name:"repeat",size:"small"})})})]})})}}exports.default=k;
