"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var IndexBehavior_exports = {};
__export(IndexBehavior_exports, {
  IndexBehavior: () => IndexBehavior
});
module.exports = __toCommonJS(IndexBehavior_exports);
var import_EndpointLifecycle = require("#endpoint/properties/EndpointLifecycle.js");
var import_general = require("#general");
var import_Behavior = require("../../Behavior.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class IndexBehavior extends import_Behavior.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 import_EndpointLifecycle.EndpointLifecycle.Change.IdAssigned:
      case import_EndpointLifecycle.EndpointLifecycle.Change.NumberAssigned:
      case import_EndpointLifecycle.EndpointLifecycle.Change.Installed:
        this.#add(endpoint);
        this.#change();
        break;
      case import_EndpointLifecycle.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 import_general.EventEmitter {
    /**
     * Emitted when the index changes.
     */
    change = (0, import_general.Observable)();
  }
  IndexBehavior2.Events = Events;
})(IndexBehavior || (IndexBehavior = {}));
//# sourceMappingURL=IndexBehavior.js.map
