/* COPYRIGHT Esri - https://js.arcgis.com/5.2/LICENSE.txt */
import { c as a } from "../../chunks/runtime.js";
import n from "@arcgis/core/Graphic.js";
import { u as l } from "../../chunks/useT9n.js";
import { html as h } from "lit";
import { createRef as o, ref as c } from "lit/directives/ref.js";
import { LitElement as u } from "@arcgis/lumina";
import * as s from "@arcgis/toolkit/log";
class _ extends u {
  constructor() {
    super(...arguments), this._lastRun = 0, this._attempted = !1, this._isCurrentlyInView = !1, this._io = null, this._titleContainerSpan = o(), this._messages = l(), this._generatedTitle = null, this.isNewFeature = !1;
  }
  static {
    this.properties = { _generatedTitle: 16, graphic: 0, layer: 0, noTitleLabel: 1, isNewFeature: 5 };
  }
  loaded() {
    this.manager.onLifecycle(() => (this._addIntersectionObserver(), () => {
      this._removeIntersectionObserver();
    }));
  }
  willUpdate(e) {
    e.has("graphic") && (this.hasUpdated || this.graphic !== null) && this._changeGraphic();
  }
  updated() {
    !this._attempted && this._isCurrentlyInView && (this._attempted = !0, this._handleTitle().catch((e) => {
      s.log("warn", this, "Failed processing title");
    }));
  }
  get rootLayer() {
    return this.layer?.type === "subtype-sublayer" ? this.layer.parent : this.layer;
  }
  _changeGraphic() {
    this._changeData();
  }
  _changeData() {
    this._generatedTitle = null, this._attempted = !1;
  }
  _createNoTitleLabel() {
    return this.noTitleLabel === null || this.noTitleLabel === void 0 ? this._messages.noTitleLabel ?? "" : this.noTitleLabel;
  }
  async _generateTitle() {
    if (!(this.layer && this.graphic))
      return this._createNoTitleLabel();
    if (this.layer.getFeatureTitle) {
      let e = this.graphic;
      if (this.isNewFeature && this.layer?.objectIdField && e) {
        const { [this.layer.objectIdField]: i, ...r } = e.attributes ?? {};
        e = new n({
          geometry: e.geometry,
          attributes: { ...r }
        });
      }
      const t = await this.layer.getFeatureTitle(e);
      return t || this._createNoTitleLabel();
    } else
      return this._createNoTitleLabel();
  }
  async _handleTitle() {
    const e = ++this._lastRun;
    if (!this._generatedTitle || this._generatedTitle === "")
      try {
        const t = await this._generateTitle();
        if (this._lastRun !== e || this._generatedTitle === t)
          return;
        this._generatedTitle = t;
      } finally {
        this._attempted = !0;
      }
  }
  _addIntersectionObserver() {
    const e = {
      // If the title gets within 50px in the Y axis, start the download.
      rootMargin: "50px 0px",
      threshold: 0.01
    };
    this._titleContainerSpan.value && requestAnimationFrame(() => {
      this.isConnected && (this._io = new IntersectionObserver((t) => {
        let i = !1;
        t.forEach((r) => {
          r.intersectionRatio > 0 && !i ? (i = !0, this._isCurrentlyInView = !0, this._handleTitle().catch((p) => {
            s.log("warn", this, "Failed processing title");
          })) : this._isCurrentlyInView = !1;
        });
      }, e), this._io.observe(this._titleContainerSpan.value));
    });
  }
  _removeIntersectionObserver() {
    this._io?.disconnect(), this._io = null;
  }
  render() {
    return h`<span ${c(this._titleContainerSpan)}>${this._generatedTitle ?? ""}</span>`;
  }
}
a("arcgis-feature-title", _);
export {
  _ as ArcgisFeatureTitle
};
