/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { c as h } from "../../chunks/runtime.js";
import { keyed as n } from "lit/directives/keyed.js";
import { LitElement as d, createEvent as l, nothing as p } from "@arcgis/lumina";
import { css as m, html as a } from "lit";
const u = m`:host{overflow-y:auto;overflow-x:hidden;scroll-behavior:smooth;height:100%;width:100%}`;
class v extends d {
  constructor() {
    super(...arguments), this.treeItemsOpen = /* @__PURE__ */ new Set(), this.handleMutations = (e) => {
      e.forEach((s) => {
        if (s.type === "attributes" && s.attributeName === "expanded") {
          const t = s.target;
          "key" in t.dataset && t.dataset.key !== void 0 && this.treeItemsOpen.has(t.dataset.key) !== t.expanded && this.toggleTreeItem(t.dataset.key);
        }
      });
    }, this.renderedTree = [], this.data = {}, this.arcgisChange = l();
  }
  static {
    this.properties = { renderedTree: 16, data: 0, value: 1 };
  }
  static {
    this.styles = u;
  }
  load() {
    this.setupComponent();
  }
  willUpdate(e) {
    e.has("data") && (this.hasUpdated || Object.keys(this.data ?? {}).length > 0) && this.setupComponent();
  }
  loaded() {
    this.manager.onLifecycle(() => {
      this.observer = new MutationObserver(this.handleMutations);
      const e = this.el.shadowRoot ?? this.el;
      return this.observer.observe(e, {
        attributes: !0,
        subtree: !0,
        attributeFilter: ["expanded"]
      }), () => {
        this.observer?.disconnect(), this.observer = void 0;
      };
    });
  }
  setupComponent() {
    this.treeItemsOpen.clear(), this.buildJSXTree();
  }
  buildJSXTree() {
    const e = (s) => {
      const t = [];
      for (const r of Object.keys(s)) {
        const i = s[r];
        let o;
        typeof i == "object" && (o = this.treeItemsOpen.has(r) ? e(i) : []);
        const c = typeof i == "object" ? r : i;
        t.push(this.createTreeItem(r, c, o));
      }
      return t;
    };
    this.renderedTree = e(this.data);
  }
  toggleTreeItem(e) {
    this.treeItemsOpen.has(e) ? this.treeItemsOpen.delete(e) : this.treeItemsOpen.add(e), this.buildJSXTree();
  }
  createTreeItem(e, s, t) {
    return a`<calcite-tree-item data-key=${e ?? p} .selected=${this.value === e} .expanded=${this.treeItemsOpen.has(e)}>${s}${t !== void 0 && n(`${e}-child-tree`, a`<calcite-tree slot=children>${t}</calcite-tree>`) || ""}</calcite-tree-item>`;
  }
  handleTreeSelect(e) {
    const t = e.currentTarget?.selectedItems[0]?.dataset.key;
    t !== void 0 && (this.value = t, this.arcgisChange.emit());
  }
  render() {
    return n("base-tree", a`<calcite-tree scale=s @calciteTreeSelect=${this.handleTreeSelect}>${this.renderedTree}</calcite-tree>`);
  }
}
h("arcgis-spatial-reference-picker-tree", v);
export {
  v as ArcgisSpatialReferencePickerTree
};
