import{Texture as e,SpriteMaterial as t,Sprite as i,Vector2 as n,Object3D as o,Vector3 as s}from"three";function r(e,t,i,n){return new(i||(i=Promise))((function(o,s){function r(e){try{l(n.next(e))}catch(t){s(t)}}function a(e){try{l(n.throw(e))}catch(t){s(t)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(r,a)}l((n=n.apply(e,t||[])).next())}))}class a{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 l extends a{constructor(){super(),Object.defineProperty(this,"map",{enumerable:!0,configurable:!0,writable:!0,value:void 0})}__inject(e){return this.map=e,this}}class c extends l{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 r(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(((o,s)=>{const r=new Image;r.onload=()=>{const e=r.height*t;i.width=r.width*t*this._scale,i.height=e*this._scale,n.drawImage(r,0,0,i.width,i.height),o(i)},r.src=e}))}makeCanvasSprite(o,{name:s,renderOrder:r,position:a}){const l=new e(o);l.needsUpdate=!0;const c=new t({map:l,transparent:!0,depthTest:!1,depthWrite:!1}),h=new i(c);return h.name=s,h.renderOrder=r,h.center=new n(.5,0),h.position.copy(a),h}}Object.defineProperty(c,"NAME",{enumerable:!0,configurable:!0,writable:!0,value:"PoiMarker"});class h extends l{constructor({name:e,collision:t}){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:t},this._group=new o,this._group.name=e}complete(){}__init(){this.map.scene.add(this._group),this.map.updateSizeMap.set(this,(()=>this._updateSize()))}add(...e){return r(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,o,s,r;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===(r=null===(s=null===(o=null==a?void 0:a.sprite)||void 0===o?void 0:o.material)||void 0===s?void 0:s.dispose)||void 0===r||r.call(s),(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 o=this.getPoiScale(i.position,{w:n.width,h:n.height});if(i.scale.set(o[0]/this._scale,o[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),o=e.material.map.image,s=t.material.map.image,r=o.width/this._scale,a=o.height/this._scale,l=i.x-r/2,c=i.y-a/2,h=s?s.width/this._scale:0,p=s?s.height/this._scale:0,u=n.x-h/2,d=n.y-p/2;return!(l>=u&&l>=u+h)&&(!(l<=u&&l+r<=u)&&(!(c>=d&&c>=d+p)&&!(c<=d&&c+a<=d)))}toScreenPos(e,t){const i=new s(e[0],e[1],e[2]).project(t),n=this.map.$map.clientWidth/2,o=this.map.$map.clientHeight/2;return{x:Math.round(i.x*n+n),y:Math.round(-i.y*o+o)}}}Object.defineProperty(h,"NAME",{enumerable:!0,configurable:!0,writable:!0,value:"PoisLayer"});export{c as PoiMarker,h as PoisLayer};
