/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Diagnostic, InternalError, Lifetime, Transaction } from "#general";
import { AccessLevel } from "#model";
import { AccessControl, Mark } from "#protocol";
import { Contextual } from "../Contextual.js";
let nextInternalId = 1;
let ReadOnly;
const LocalActorContext = {
  /**
   * Operate on behalf of a local actor.  This is the context for operations on nodes initiated locally, without
   * authentication.
   *
   * {@link act} provides an {@link ActionContext} you can use to access agents for a {@link Endpoint}.
   * State changes and change events occur once {@link actor} returns.
   * It can return a promise even if the actor method does not return a promise, so manual checks are needed.
   *
   * The {@link Transaction} is destroyed with {@link act} exits so you should not keep a reference to any agents
   * beyond the lifespan of {@link actor}.
   *
   * Offline context is very permissive.  You should use carefully.
   */
  act(purpose, actor, options) {
    const context = this.open(purpose, options);
    let result;
    try {
      result = actor(context);
    } catch (e) {
      return context.reject(e);
    }
    return context.resolve(result);
  },
  /**
   * Create an offline context.
   *
   * This context operates with a {@link Transaction} created via {@link Transaction.open} and the same rules
   * apply for lifecycle management using {@link Transaction.Finalization}.
   */
  open(purpose, options) {
    const id = nextInternalId;
    nextInternalId = (nextInternalId + 1) % 65535;
    const via = Diagnostic.via(`${Mark.LOCAL_SESSION}${purpose}#${id.toString(16)}`);
    let frame;
    let transaction;
    try {
      frame = options?.activity?.begin(via);
      transaction = Transaction.open(via, options?.lifetime ?? Lifetime.process, options?.isolation);
      if (frame) {
        transaction.onClose(frame.close.bind(frame));
      }
      const context = Object.freeze({
        ...options,
        transaction,
        activity: frame,
        authorityAt(desiredAccessLevel) {
          return desiredAccessLevel === AccessLevel.View ? AccessControl.Authority.Granted : AccessControl.Authority.Unauthorized;
        },
        get [Contextual.context]() {
          return this;
        },
        [Symbol.toStringTag]: "OfflineContext",
        resolve: transaction.resolve.bind(transaction),
        reject: transaction.reject.bind(transaction),
        offline: true
      });
      return context;
    } catch (e) {
      if (transaction) {
        transaction.reject(e);
      } else {
        frame?.close();
        throw e;
      }
    }
    throw new InternalError("Unexpected end of open");
  },
  /**
   * Normally you need to use {@link LocalActorContext.act} to work with behaviors, and you can only interact with the
   * behaviors in the actor function.  This {@link ActionContext} allows you to create offline agents that remain
   * functional for the lifespan of the node.
   *
   * Write operations will throw an error with this context.
   */
  get ReadOnly() {
    if (ReadOnly === void 0) {
      ReadOnly = LocalActorContext.open("read-only", { isolation: "ro" });
    }
    return ReadOnly;
  },
  [Symbol.toStringTag]: "LocalActorContext"
};
export {
  LocalActorContext,
  nextInternalId
};
//# sourceMappingURL=LocalActorContext.js.map
