var KTMapPluginPoisLayer=function(e,t){"use strict";function i(e,t,i,n){return new(i||(i=Promise))((function(r,o){function s(e){try{l(n.next(e))}catch(t){o(t)}}function a(e){try{l(n.throw(e))}catch(t){o(t)}}function l(e){var t;e.done?r(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(s,a)}l((n=n.apply(e,t||[])).next())}))}class n{constructor(){var e;Object.defineProperty(this,"emitter",{enumerable:!0,configurable:!0,writable:!0,value:{all:e=e||new Map,on:function(t,i){var n=e.get(t);n?n.push(i):e.set(t,[i])},off:function(t,i){var n=e.get(t);n&&(i?n.splice(n.indexOf(i)>>>0,1):e.set(t,[]))},emit:function(t,i){var n=e.get(t);n&&n.slice().map((function(e){e(i)})),(n=e.get("*"))&&n.slice().map((function(e){e(t,i)}))}}})}on(e,t){return this.emitter.on(e,t),this}off(e,t){return this.emitter.off(e,t),this}}class r extends n{constructor(){super(),Object.defineProperty(this,"map",{enumerable:!0,configurable:!0,writable:!0,value:void 0})}__inject(e){return this.map=e,this}}class o extends r{constructor(e){super(),Object.defineProperty(this,"options",{enumerable:!0,configurable:!0,writable:!0,value:e}),Object.defineProperty(this,"_scale",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"sprite",{enumerable:!0,configurable:!0,writable:!0,value:void 0})}complete(){}__inject(e,t){return super.__inject(e),this._scale=t,this}__init(){}_create(){return i(this,void 0,void 0,(function*(){const e=yield this.generateCanvas(this.options);return this.sprite=this.makeCanvasSprite(e,this.options),this.sprite}))}setPosition(e){this.sprite&&this.sprite.position.copy(e),this.map.reDraw()}generateCanvas({icon:e,scale:t=1}){const i=document.createElement("canvas"),n=i.getContext("2d");return new Promise(((r,o)=>{const s=new Image;s.onload=()=>{const e=s.height*t;i.width=s.width*t*this._scale,i.height=e*this._scale,n.drawImage(s,0,0,i.width,i.height),r(i)},s.src=e}))}makeCanvasSprite(e,{name:i,renderOrder:n,position:r}){const o=new t.Texture(e);o.needsUpdate=!0;const s=new t.SpriteMaterial({map:o,transparent:!0,depthTest:!1,depthWrite:!1}),a=new t.Sprite(s);return a.name=i,a.renderOrder=n,a.center=new t.Vector2(.5,0),a.position.copy(r),a}}Object.defineProperty(o,"NAME",{enumerable:!0,configurable:!0,writable:!0,value:"PoiMarker"});class s extends r{constructor({name:e,collision:i}){super(),Object.defineProperty(this,"_scale",{enumerable:!0,configurable:!0,writable:!0,value:4}),Object.defineProperty(this,"options",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_group",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.options={name:e,collision:i},this._group=new t.Object3D,this._group.name=e}complete(){}__init(){this.map.scene.add(this._group),this.map.updateSizeMap.set(this,(()=>this._updateSize()))}add(...e){return i(this,void 0,void 0,(function*(){for(const t of e){t.__inject(this.map,this._scale);const e=yield t._create();this._group.add(e)}return this.map.reDraw(),this}))}remove(...e){var t,i,n,r,o,s;for(const a of e)null===(n=null===(i=null===(t=null==a?void 0:a.sprite)||void 0===t?void 0:t.geometry)||void 0===i?void 0:i.dispose)||void 0===n||n.call(i),null===(s=null===(o=null===(r=null==a?void 0:a.sprite)||void 0===r?void 0:r.material)||void 0===o?void 0:o.dispose)||void 0===s||s.call(o),(null==a?void 0:a.sprite)&&this._group.remove(null==a?void 0:a.sprite);return this.map.reDraw(),this}_updateSize(){const e=this._group?this._group.children:[];if(0!==e.length){if(this.options.collision)for(let t=0;t<e.length;t++){e[t].visible=!0}for(let t=0;t<e.length;t++){const i=e[t],n=i.material.map.image;if(n){const r=this.getPoiScale(i.position,{w:n.width,h:n.height});if(i.scale.set(r[0]/this._scale,r[1]/this._scale,1),this.options.collision&&i.visible)for(let n=t+1;n<e.length;n++){const t=e[n];this.isMarkerCollision(i,t)&&(t.visible=!1)}}}}}getPoiScale(e,t){const i=this.map.camera.position.distanceTo(e),n=2*(Math.tan(this.map.camera.fov/2*Math.PI/180)*i)/this.map.$map.clientHeight;return[t.w*n,t.h*n,1]}isMarkerCollision(e,t){const i=this.toScreenPos([e.position.x,e.position.y,e.position.z],this.map.camera),n=this.toScreenPos([t.position.x,t.position.y,t.position.z],this.map.camera),r=e.material.map.image,o=t.material.map.image,s=r.width/this._scale,a=r.height/this._scale,l=i.x-s/2,c=i.y-a/2,h=o?o.width/this._scale:0,u=o?o.height/this._scale:0,p=n.x-h/2,d=n.y-u/2;return!(l>=p&&l>=p+h)&&(!(l<=p&&l+s<=p)&&(!(c>=d&&c>=d+u)&&!(c<=d&&c+a<=d)))}toScreenPos(e,i){const n=new t.Vector3(e[0],e[1],e[2]).project(i),r=this.map.$map.clientWidth/2,o=this.map.$map.clientHeight/2;return{x:Math.round(n.x*r+r),y:Math.round(-n.y*o+o)}}}return Object.defineProperty(s,"NAME",{enumerable:!0,configurable:!0,writable:!0,value:"PoisLayer"}),e.PoiMarker=o,e.PoisLayer=s,Object.defineProperty(e,"__esModule",{value:!0}),e}({},THREE);
