"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 Agent_exports = {};
__export(Agent_exports, {
  Agent: () => Agent,
  INSTALL_BEHAVIOR: () => INSTALL_BEHAVIOR
});
module.exports = __toCommonJS(Agent_exports);
var import_general = require("#general");
var import_protocol = require("#protocol");
/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const INSTALL_BEHAVIOR = Symbol("install-behavior");
class Agent {
  #endpoint;
  #context;
  #behaviors = {};
  constructor(endpoint, context) {
    this.#endpoint = endpoint;
    this.#context = context;
  }
  /**
   * Access the {@link Endpoint} this agent acts on behalf of.
   */
  get endpoint() {
    return this.#endpoint;
  }
  /**
   * Access an {@link Agent} for this agent's owner.
   */
  get owner() {
    if (this.#endpoint.owner === void 0) {
      return void 0;
    }
    return this.#endpoint.owner.agentFor(this.context);
  }
  /**
   * Access the agent's {@link ActionContext}.
   */
  get context() {
    return this.#context;
  }
  /**
   * Test to see if a {@link Behavior.Type} is supported by this agent.
   */
  has(type) {
    return this.#endpoint.behaviors.has(type);
  }
  /**
   * Obtain a {@link Behavior} supported by this agent.  Throws an error if the {@link Behavior.Type} isn't supported
   * or is still initializing.
   *
   * You may also access behaviors using normal property access, e.g. `agent.descriptor` is the same as
   * `agent.get(DescriptorBehavior)`.
   *
   * Property access is available in TypeScript when the set of behaviors is defined statically.
   */
  get(type) {
    let behavior = this.#behaviors[type.id];
    if (!behavior) {
      behavior = this.#endpoint.behaviors.createSync(type, this);
    }
    return behavior;
  }
  /**
   * Optionally obtain a behavior supported by this agent.  Returns `undefined` if the {@link Behavior.Type} isn't
   * supported or yet installed
   */
  maybeGet(type) {
    if (this.has(type)) {
      return this.get(type);
    }
  }
  /**
   * Obtain a behavior supported by this agent.  Throws an error if the {@link Behavior.Type} isn't supported.  Waits
   * if the behavior is not yet initialized.
   */
  load(type) {
    const behavior = this.#behaviors[type.id];
    if (behavior) {
      return behavior;
    }
    return this.#endpoint.behaviors.createMaybeAsync(type, this);
  }
  /**
   * Trigger initialization of a supported {@link Behavior.Type}.
   *
   * Functionally identical to {@link load} but has no return value and errors are logged rather than thrown.
   */
  activate(type) {
    this.#endpoint.behaviors.activate(type, this);
  }
  /**
   * Add support for a {@link Behavior.Type}.
   */
  require(type, options) {
    this.#endpoint.behaviors.require(type, options);
  }
  /**
   * Determine whether a behavior is loaded (does not validate class, only by ID).
   */
  isLoaded(type) {
    return this.#behaviors[type.id] !== void 0;
  }
  get env() {
    return this.#endpoint.env;
  }
  toString() {
    return this.#endpoint.toString();
  }
  /**
   * Create a new {@link Agent} that supports the specified behaviors.
   */
  static for(type, behaviors) {
    const props = {};
    Object.values(behaviors).forEach((behavior) => {
      props[behavior.id] = {
        get() {
          return this.get(behavior);
        },
        enumerable: true
      };
    });
    return (0, import_general.GeneratedClass)({
      name: `${type.name}Agent`,
      base: Agent,
      instanceDescriptors: props
    });
  }
  /**
   * Execute logic with elevated privileges.
   *
   * Temporarily modifies {@link context} to be a {@link LocalActorContext}.  This bypasses checks associated with the
   * remote subject.
   *
   * Elevated logic effectively ignores ACLs so should be used with care.
   *
   * Note that interactions with the agent will remain elevated until the synchronous completion of this call. You
   * should only elevate privileges for synchronous logic.
   *
   * @param fn the elevated logic
   */
  asLocalActor(fn) {
    const { context } = this;
    let restoreContext;
    try {
      if ((0, import_protocol.hasRemoteActor)(context)) {
        const { fabric, subject } = context;
        restoreContext = () => {
          Object.assign(context, { fabric, subject });
        };
        delete context.fabric;
        delete context.subject;
        Object.assign(context, { fabric: void 0, subject: void 0 });
      }
      fn();
    } finally {
      restoreContext?.();
    }
  }
  [INSTALL_BEHAVIOR](behavior) {
    this.#behaviors[behavior.type.id] = behavior;
  }
}
//# sourceMappingURL=Agent.js.map
