var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};

// packages/plugins/pois-layer/src/poiMarker.ts
import { Sprite, SpriteMaterial, Texture, Vector2 } from "three";
import { Element } from "@ktmap/map";
var PoiMarker = class extends Element {
  constructor(options) {
    super();
    this.options = options;
    __publicField(this, "_scale");
    __publicField(this, "sprite");
  }
  complete() {
  }
  __inject(map, _scale) {
    super.__inject(map);
    this._scale = _scale;
    return this;
  }
  __init() {
  }
  _create() {
    return __async(this, null, function* () {
      const canvas = yield this.generateCanvas(this.options);
      this.sprite = this.makeCanvasSprite(canvas, this.options);
      return this.sprite;
    });
  }
  setPosition(position) {
    this.sprite && this.sprite.position.copy(position);
    this.map.reDraw();
  }
  generateCanvas({
    icon,
    scale = 1
  }) {
    const canvas = document.createElement("canvas");
    const context = canvas.getContext("2d");
    return new Promise((resolve, reject) => {
      const img = new Image();
      img.onload = () => {
        const w = img.width * scale;
        const h = img.height * scale;
        canvas.width = w * this._scale;
        canvas.height = h * this._scale;
        context.drawImage(img, 0, 0, canvas.width, canvas.height);
        resolve(canvas);
      };
      img.src = icon;
    });
  }
  makeCanvasSprite(canvas, { name, renderOrder, position }) {
    const texture = new Texture(canvas);
    texture.needsUpdate = true;
    const spriteMaterial = new SpriteMaterial({
      map: texture,
      transparent: true,
      depthTest: false,
      depthWrite: false
    });
    const sprite = new Sprite(spriteMaterial);
    sprite.name = name;
    sprite.renderOrder = renderOrder;
    sprite.center = new Vector2(0.5, 0);
    sprite.position.copy(position);
    return sprite;
  }
};
__publicField(PoiMarker, "NAME", "PoiMarker");

// packages/plugins/pois-layer/src/poisLayer.ts
import { Object3D, Vector3 as Vector32 } from "three";
import { Element as Element2 } from "@ktmap/map";
var PoisLayer = class extends Element2 {
  constructor({ name, collision }) {
    super();
    __publicField(this, "_scale", 4);
    __publicField(this, "options");
    __publicField(this, "_group");
    this.options = { name, collision };
    this._group = new Object3D();
    this._group.name = name;
  }
  complete() {
  }
  __init() {
    this.map.scene.add(this._group);
    this.map.updateSizeMap.set(this, () => this._updateSize());
  }
  add(...poiMarker) {
    return __async(this, null, function* () {
      for (const item of poiMarker) {
        item.__inject(this.map, this._scale);
        const marker = yield item._create();
        this._group.add(marker);
      }
      this.map.reDraw();
      return this;
    });
  }
  remove(...poiMarker) {
    var _a, _b, _c, _d, _e, _f;
    for (const item of poiMarker) {
      (_c = (_b = (_a = item == null ? void 0 : item.sprite) == null ? void 0 : _a.geometry) == null ? void 0 : _b.dispose) == null ? void 0 : _c.call(_b);
      (_f = (_e = (_d = item == null ? void 0 : item.sprite) == null ? void 0 : _d.material) == null ? void 0 : _e.dispose) == null ? void 0 : _f.call(_e);
      (item == null ? void 0 : item.sprite) && this._group.remove(item == null ? void 0 : item.sprite);
    }
    this.map.reDraw();
    return this;
  }
  _updateSize() {
    const sprites = this._group ? this._group.children : [];
    if (sprites.length === 0)
      return;
    if (this.options.collision) {
      for (let i = 0; i < sprites.length; i++) {
        const sprite = sprites[i];
        sprite.visible = true;
      }
    }
    for (let i = 0; i < sprites.length; i++) {
      const compareSprite = sprites[i];
      const canvas = compareSprite.material.map.image;
      if (canvas) {
        const position = compareSprite.position;
        const scale = this.getPoiScale(position, {
          w: canvas.width,
          h: canvas.height
        });
        compareSprite.scale.set(
          scale[0] / this._scale,
          scale[1] / this._scale,
          1
        );
        if (this.options.collision && compareSprite.visible) {
          for (let j = i + 1; j < sprites.length; j++) {
            const sprite = sprites[j];
            if (this.isMarkerCollision(compareSprite, sprite)) {
              sprite.visible = false;
            }
          }
        }
      }
    }
  }
  getPoiScale(position, poiRect) {
    const distance = this.map.camera.position.distanceTo(position);
    const top = Math.tan(this.map.camera.fov / 2 * Math.PI / 180) * distance;
    const meterPerPixel = 2 * top / this.map.$map.clientHeight;
    const scaleX = poiRect.w * meterPerPixel;
    const scaleY = poiRect.h * meterPerPixel;
    return [scaleX, scaleY, 1];
  }
  isMarkerCollision(compareSprite, sprite) {
    const comparePosition = this.toScreenPos(
      [
        compareSprite.position.x,
        compareSprite.position.y,
        compareSprite.position.z
      ],
      this.map.camera
    );
    const spritePosition = this.toScreenPos(
      [sprite.position.x, sprite.position.y, sprite.position.z],
      this.map.camera
    );
    const image1 = compareSprite.material.map.image;
    const image2 = sprite.material.map.image;
    const w1 = image1.width / this._scale;
    const h1 = image1.height / this._scale;
    const x1 = comparePosition.x - w1 / 2;
    const y1 = comparePosition.y - h1 / 2;
    const w2 = image2 ? image2.width / this._scale : 0;
    const h2 = image2 ? image2.height / this._scale : 0;
    const x2 = spritePosition.x - w2 / 2;
    const y2 = spritePosition.y - h2 / 2;
    if (x1 >= x2 && x1 >= x2 + w2) {
      return false;
    } else if (x1 <= x2 && x1 + w1 <= x2) {
      return false;
    } else if (y1 >= y2 && y1 >= y2 + h2) {
      return false;
    } else if (y1 <= y2 && y1 + h1 <= y2) {
      return false;
    } else {
      return true;
    }
  }
  toScreenPos(position, camera) {
    const worldVector = new Vector32(position[0], position[1], position[2]);
    const standardVector = worldVector.project(camera);
    const a = this.map.$map.clientWidth / 2;
    const b = this.map.$map.clientHeight / 2;
    const x = Math.round(standardVector.x * a + a);
    const y = Math.round(-standardVector.y * b + b);
    return {
      x,
      y
    };
  }
};
__publicField(PoisLayer, "NAME", "PoisLayer");
export {
  PoiMarker,
  PoisLayer
};
//# sourceMappingURL=pois-layer.esm-bundler.js.map
