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;
};

// packages/plugins/info-window/src/infoWindow.ts
import { InfoWindow as InfoWindowParent } from "@ktmap/map";
import { alignPoint } from "dom-align";
import { getScreenPostion, warn } from "@ktmap/shared";
var InfoWindow = class extends InfoWindowParent {
  constructor(map, options) {
    var _a, _b, _c;
    super();
    this.map = map;
    this.options = options;
    __publicField(this, "id", "");
    __publicField(this, "clsPrefix", "ktmap-info-window");
    __publicField(this, "$window");
    __publicField(this, "lastWorldPosition");
    __publicField(this, "cacheInnerHTML", "");
    options.align = (_a = options.align) != null ? _a : { points: ["cl"], useCssTransform: true };
    options.align.points = (_b = options.align.points) != null ? _b : ["cl"];
    options.align.useCssTransform = (_c = options.align.useCssTransform) != null ? _c : true;
    this.__init();
  }
  __init() {
    console.log("InfoWindow init success");
    if (true) {
      warn("\u5F00\u53D1\u73AF\u5883\u8B66\u544A\uFF0C\u6D4B\u8BD5");
    }
    this.create();
    const {
      map,
      options,
      options: { align },
      $window
    } = this;
    map.updateSizeMap.set(this, () => {
      if (this.lastWorldPosition && $window.style.display === "block") {
        const { $map, scene, camera } = map;
        const { x, y } = getScreenPostion({
          worldPosition: this.lastWorldPosition,
          $map,
          scene,
          camera
        });
        alignPoint(
          $window,
          { clientX: x, clientY: y },
          {
            points: align.points,
            offset: align.offset,
            overflow: { adjustX: true, adjustY: true },
            useCssTransform: align.useCssTransform
          }
        );
      }
    });
    if (options.trigger === "hover") {
      const _find = (item) => {
        let _item = item.object;
        while (_item && _item.name !== options.targetName) {
          _item = _item == null ? void 0 : _item.parent;
        }
        return _item;
      };
      map.on("mousemove", (ev) => {
        const { intersects, worldPosition, screenPosition } = ev;
        if (intersects && screenPosition) {
          this.lastWorldPosition = worldPosition;
          let target = null;
          for (const item of intersects) {
            target = _find(item);
            if (target)
              break;
          }
          if (target) {
            const { x, y } = screenPosition;
            alignPoint(
              $window,
              { clientX: x, clientY: y },
              {
                points: align.points,
                offset: align.offset,
                overflow: { adjustX: true, adjustY: true },
                useCssTransform: align.useCssTransform
              }
            );
            $window.style.display = "block";
            map.domElement.style.cursor = "pointer";
            this.emitter.emit("hover", target);
          } else {
            $window.style.display = "none";
            map.domElement.style.cursor = "default";
          }
        } else {
          $window.style.display = "none";
        }
      });
    } else if (options.trigger === "click") {
      const _find = (item) => {
        let _item = item.object;
        while (_item && _item.name !== options.targetName) {
          _item = _item == null ? void 0 : _item.parent;
        }
        return _item;
      };
      map.on("mousemove", (ev) => {
        const { intersects, screenPosition } = ev;
        if (intersects && screenPosition) {
          let target = null;
          for (const item of intersects) {
            target = _find(item);
            if (target)
              break;
          }
          map.domElement.style.cursor = target ? "pointer" : "default";
        }
      });
      map.on("click", (ev) => {
        const { intersects, worldPosition, screenPosition } = ev;
        if (intersects && screenPosition) {
          this.lastWorldPosition = worldPosition;
          let target = null;
          for (const item of intersects) {
            target = _find(item);
            if (target)
              break;
          }
          if (target) {
            const { x, y } = screenPosition;
            alignPoint(
              $window,
              { clientX: x, clientY: y },
              {
                points: align.points,
                offset: align.offset,
                overflow: { adjustX: true, adjustY: true },
                useCssTransform: align.useCssTransform
              }
            );
            $window.style.display = "block";
            this.emitter.emit("click", target);
          } else {
            $window.style.display = "none";
          }
        } else {
          $window.style.display = "none";
        }
      });
    }
  }
  setContent(content) {
    const { clsPrefix, $window } = this;
    const $content = $window == null ? void 0 : $window.querySelector(`.${clsPrefix}__content`);
    if ($content) {
      let innerHTML = this.cacheInnerHTML;
      if (content instanceof HTMLElement) {
        innerHTML = content.outerHTML;
      } else {
        innerHTML = content;
      }
      if (this.cacheInnerHTML !== innerHTML)
        $content.innerHTML = innerHTML;
      this.cacheInnerHTML = innerHTML;
    }
  }
  create() {
    const { map, options, clsPrefix } = this;
    const html = `
      ${options.title ? `<div class="${clsPrefix}__title">${options.title}</div>` : ""}
      <div class="${clsPrefix}__content"></div>
      ${options.close ? `<div class="text-right">
              <button>\u6211\u77E5\u9053\u4E86</button>
            </div>` : ""}
    `;
    const wrapper = document.createElement("div");
    wrapper.id = this.id = `${clsPrefix}_${+new Date()}`;
    wrapper.className = `${clsPrefix} ${clsPrefix}--${options.type}`;
    wrapper.innerHTML = html;
    wrapper.style.display = "none";
    wrapper.addEventListener("click", (ev) => {
      if (ev.target.tagName === "BUTTON") {
        this.hide();
      }
    });
    map.$map.appendChild(wrapper);
    this.$window = wrapper;
    return wrapper;
  }
  show(worldPosition) {
    if (worldPosition) {
      const {
        map: { $map, scene, camera },
        options: { align }
      } = this;
      this.lastWorldPosition = worldPosition;
      const { x, y } = getScreenPostion({
        worldPosition,
        $map,
        scene,
        camera
      });
      alignPoint(
        this.$window,
        { clientX: x, clientY: y },
        {
          points: align.points,
          offset: align.offset,
          overflow: { adjustX: true, adjustY: true },
          useCssTransform: align.useCssTransform
        }
      );
    }
    this.$window.style.display = "block";
  }
  hide() {
    this.$window.style.display = "none";
  }
  destroy() {
  }
};
__publicField(InfoWindow, "NAME", "InfoWindow");
export {
  InfoWindow
};
//# sourceMappingURL=info-window.esm-bundler.js.map
