"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 ServerEndpointInitializer_exports = {};
__export(ServerEndpointInitializer_exports, {
  ServerEndpointInitializer: () => ServerEndpointInitializer
});
module.exports = __toCommonJS(ServerEndpointInitializer_exports);
var import_Endpoint = require("#endpoint/Endpoint.js");
var import_EndpointServer = require("#endpoint/EndpointServer.js");
var import_EndpointInitializer = require("#endpoint/properties/EndpointInitializer.js");
var import_general = require("#general");
var import_DescriptorServer = require("../../behaviors/descriptor/DescriptorServer.js");
var import_NodeStore = require("../storage/NodeStore.js");
/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const logger = import_general.Logger.get("BehaviorInit");
class ServerEndpointInitializer extends import_EndpointInitializer.EndpointInitializer {
  #store;
  constructor(environment) {
    super();
    this.#store = environment.get(import_NodeStore.NodeStore);
  }
  initializeDescendent(endpoint) {
    if (!endpoint.lifecycle.hasId) {
      endpoint.id = this.#identifyPart(endpoint);
    }
    this.#store.endpointStores.assignNumber(endpoint);
    endpoint.behaviors.require(import_DescriptorServer.DescriptorServer);
  }
  /**
   * If a {@link Endpoint} does not yet have a {@link EndpointServer}, create one now, then create a
   * {@link BehaviorBacking} for a specific {@link Behavior}.
   *
   * This is where we adapt endpoints and behaviors for a server role.
   */
  createBacking(endpoint, behavior) {
    return import_EndpointServer.EndpointServer.forEndpoint(endpoint).createBacking(behavior);
  }
  /**
   * Select an ID for an endpoint automatically based on available metadata.
   */
  #identifyPart(endpoint) {
    const basicInfo = endpoint.behaviors.supported.basicInformation ?? endpoint.behaviors.supported.bridgedDeviceBasicInformation;
    if (basicInfo) {
      const defaults = {
        ...new basicInfo.State(),
        ...endpoint.behaviors.defaultsFor(basicInfo)
      };
      let id2 = defaults.uniqueId;
      if (id2) {
        return id2;
      }
      id2 = defaults.serialNumber;
      if (id2) {
        return id2;
      }
    }
    if (!(endpoint.owner instanceof import_Endpoint.Endpoint)) {
      throw new import_general.InternalError("Cannot determine ID for endpoint with unknown parent type");
    }
    if (!endpoint.owner.lifecycle.hasId) {
      throw new import_general.InternalError("Cannot determine ID for endpoint because parent has no ID");
    }
    const index = endpoint.owner.parts.indexOf(endpoint);
    if (index === -1) {
      throw new import_general.InternalError("Cannot determine ID for endpoint because parent does not list as child");
    }
    const id = `part${index}`;
    logger.warn(`Using fallback ID of ${id} for child of ${endpoint.owner}; assign ID to remove this warning`);
    return id;
  }
}
//# sourceMappingURL=ServerEndpointInitializer.js.map
