/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Mark } from "#common/Mark.js";
import { Diagnostic, MatterFlowError } from "#general";
import { NoAssociatedFabricError } from "#protocol/errors.js";
import { NodeId } from "#types";
import { MessageCodec, SessionType } from "../codec/MessageCodec.js";
import { MessageReceptionStateUnencryptedWithRollover } from "../protocol/MessageReceptionState.js";
import { Session } from "./Session.js";
const UNICAST_UNSECURE_SESSION_ID = 0;
class UnsecuredSession extends Session {
  #initiatorNodeId;
  closingAfterExchangeFinished = false;
  supportsMRP = true;
  type = SessionType.Unicast;
  constructor(config) {
    const { crypto, initiatorNodeId, isInitiator } = config;
    super({
      ...config,
      setActiveTimestamp: !isInitiator,
      // When we are the initiator we assume the node is in idle mode
      messageReceptionState: new MessageReceptionStateUnencryptedWithRollover()
    });
    this.#initiatorNodeId = initiatorNodeId ?? NodeId.randomOperationalNodeId(crypto);
  }
  get isSecure() {
    return false;
  }
  get isPase() {
    return false;
  }
  decode(packet) {
    return MessageCodec.decodePayload(packet);
  }
  encode(message) {
    return MessageCodec.encodePayload(message);
  }
  get attestationChallengeKey() {
    throw new MatterFlowError("Not supported on an unsecure session");
  }
  setFabric(_fabric) {
    throw new MatterFlowError("Not supported on an unsecure session");
  }
  get via() {
    return Diagnostic.via(`${Mark.SESSION}unsecured#${this.#initiatorNodeId.toString(16)}`);
  }
  get id() {
    return UNICAST_UNSECURE_SESSION_ID;
  }
  get peerSessionId() {
    return UNICAST_UNSECURE_SESSION_ID;
  }
  get nodeId() {
    return this.#initiatorNodeId;
  }
  get peerNodeId() {
    return void 0;
  }
  get associatedFabric() {
    throw new NoAssociatedFabricError("Session needs to be a secure session");
  }
  detachChannel() {
    this.manager?.unsecuredSessions.delete(this.nodeId);
    return super.detachChannel();
  }
  async close() {
    this.manager?.unsecuredSessions.delete(this.nodeId);
    await super.close();
  }
}
export {
  UNICAST_UNSECURE_SESSION_ID,
  UnsecuredSession
};
//# sourceMappingURL=UnsecuredSession.js.map
