"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 EndpointVariableService_exports = {};
__export(EndpointVariableService_exports, {
  EndpointVariableService: () => EndpointVariableService
});
module.exports = __toCommonJS(EndpointVariableService_exports);
var import_general = require("@matter/general");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const NODE_SUBKEY = "nodes";
const BEHAVIOR_SUBKEY = "behaviors";
const PART_SUBKEY = "parts";
class EndpointVariableService {
  #env;
  #varsForEndpoint = /* @__PURE__ */ new WeakMap();
  #varsForBehavior = /* @__PURE__ */ new WeakMap();
  constructor(env) {
    this.#env = env;
  }
  static [import_general.Environmental.create](env) {
    const service = new EndpointVariableService(env);
    env.set(EndpointVariableService, service);
    return service;
  }
  /**
   * Access the variable map for an instance of a behavior.
   */
  forBehaviorInstance(endpoint, type) {
    const forAll = this.forBehaviorType(type);
    const forThisEndpoint = this.forEndpoint(endpoint);
    const forThis = forThisEndpoint[type.id] ?? forThisEndpoint[type.id.toLowerCase()];
    if ((0, import_general.isObject)(forThis)) {
      return { ...forAll, ...forThis };
    }
    return forAll;
  }
  /**
   * Access the variable map for an endpoint.
   */
  forEndpoint(endpoint) {
    if (!endpoint.lifecycle.hasId) {
      return {};
    }
    let vars = this.#varsForEndpoint.get(endpoint);
    if (vars !== void 0) {
      return vars;
    }
    let envVars;
    if (endpoint.owner === void 0) {
      envVars = this.#env.vars.get(`${NODE_SUBKEY}.${endpoint.id}`);
    } else if (!endpoint.lifecycle.isInstalled) {
      return {};
    } else {
      const partVars = this.forEndpoint(endpoint.owner)[PART_SUBKEY];
      if ((0, import_general.isObject)(partVars)) {
        envVars = partVars[endpoint.id];
      }
    }
    if ((0, import_general.isObject)(envVars)) {
      vars = envVars;
    } else {
      vars = {};
    }
    this.#varsForEndpoint.set(endpoint, vars);
    return vars;
  }
  /**
   * Access the variable map for a type of behavior.
   */
  forBehaviorType(type) {
    let vars = this.#varsForBehavior.get(type);
    if (vars !== void 0) {
      return vars;
    }
    let envVars = this.#env.vars.get(`${BEHAVIOR_SUBKEY}.${type.id}`);
    if (!(0, import_general.isObject)(envVars)) {
      envVars = this.#env.vars.get(type.id);
    }
    if ((0, import_general.isObject)(envVars)) {
      vars = envVars;
    } else {
      vars = {};
    }
    this.#varsForBehavior.set(type, vars);
    return vars;
  }
}
//# sourceMappingURL=EndpointVariableService.js.map
