/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { EndpointLifecycle } from "#endpoint/properties/EndpointLifecycle.js";
import { EventEmitter, Observable } from "#general";
import { Behavior } from "../../Behavior.js";
class IndexBehavior extends Behavior {
  static id = "index";
  static early = true;
  initialize() {
    this.reactTo(this.endpoint.lifecycle.changed, this.#handleChange);
  }
  [Symbol.asyncDispose]() {
    this.internal.changeBroadcastPending = false;
  }
  get partsById() {
    return this.internal.partsById;
  }
  get partsByNumber() {
    return this.internal.partsByNumber;
  }
  /**
   * Retrieve a {@link Endpoint} by number.
   *
   * Note that {@link internal.partsByNumber} does not include {@link endpoint} but this method will return it if the
   * number matches.
   */
  forNumber(number) {
    if (this.endpoint.lifecycle.hasNumber && number === this.endpoint.number) {
      return this.endpoint;
    }
    return this.internal.partsByNumber[number];
  }
  #handleChange(type, endpoint) {
    switch (type) {
      case EndpointLifecycle.Change.IdAssigned:
      case EndpointLifecycle.Change.NumberAssigned:
      case EndpointLifecycle.Change.Installed:
        this.#add(endpoint);
        this.#change();
        break;
      case EndpointLifecycle.Change.Destroyed:
        this.#remove(endpoint);
        this.#change();
        break;
    }
  }
  #add(endpoint) {
    if (endpoint.lifecycle.hasNumber) {
      this.internal.partsByNumber[endpoint.number] = endpoint;
    }
    if (endpoint.lifecycle.hasId) {
      this.internal.partsById[endpoint.id] = endpoint;
    }
    for (const child of endpoint.parts) {
      this.#add(child);
    }
  }
  #remove(endpoint) {
    if (endpoint.id && this.internal.partsById[endpoint.id] === endpoint) {
      delete this.internal.partsById[endpoint.id];
    }
    if (endpoint.number !== void 0 && this.internal.partsByNumber[endpoint.number] === endpoint) {
      delete this.internal.partsByNumber[endpoint.number];
    }
    for (const child of endpoint.parts) {
      this.#remove(child);
    }
    this.#change();
  }
  /**
   * Trigger change event lazily so transactions complete and we can coalesce into fewer events.
   */
  #change() {
    const { internal } = this;
    if (internal.changeBroadcastPending) {
      return;
    }
    internal.changeBroadcastPending = true;
    void Promise.resolve().then(() => {
      if (!internal.changeBroadcastPending) {
        return;
      }
      internal.changeBroadcastPending = false;
      this.events.change.emit();
    });
  }
}
((IndexBehavior2) => {
  class Internal {
    changeBroadcastPending;
    /**
     * Map of ID to {@link Endpoint}.
     */
    partsById = {};
    /**
     * Map of number to {@link Endpoint}.
     */
    partsByNumber = {};
  }
  IndexBehavior2.Internal = Internal;
  class Events extends EventEmitter {
    /**
     * Emitted when the index changes.
     */
    change = Observable();
  }
  IndexBehavior2.Events = Events;
})(IndexBehavior || (IndexBehavior = {}));
export {
  IndexBehavior
};
//# sourceMappingURL=IndexBehavior.js.map
