/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Bytes, UnexpectedDataError } from "@matter/general";
import { SecureMessageType } from "@matter/types";
import { DEFAULT_NORMAL_PROCESSING_TIME, SecureChannelMessenger } from "../../securechannel/SecureChannelMessenger.js";
import {
  TlvPasePake1,
  TlvPasePake2,
  TlvPasePake3,
  TlvPbkdfParamRequest,
  TlvPbkdfParamResponse
} from "./PaseMessages.js";
const DEFAULT_PASSCODE_ID = 0;
const SPAKE_CONTEXT = Bytes.fromString("CHIP PAKE V1 Commissioning");
class PaseServerMessenger extends SecureChannelMessenger {
  #closed = false;
  async readPbkdfParamRequest() {
    const { payload } = await this.nextMessage({
      type: SecureMessageType.PbkdfParamRequest,
      expectedProcessingTime: DEFAULT_NORMAL_PROCESSING_TIME
    });
    return { requestPayload: payload, request: TlvPbkdfParamRequest.decode(payload) };
  }
  async sendPbkdfParamResponse(response) {
    return this.send(response, SecureMessageType.PbkdfParamResponse, TlvPbkdfParamResponse, {
      expectedProcessingTime: DEFAULT_NORMAL_PROCESSING_TIME
    });
  }
  readPasePake1() {
    return this.nextMessageDecoded(TlvPasePake1, { type: SecureMessageType.PasePake1 });
  }
  sendPasePake2(pasePake2) {
    return this.send(pasePake2, SecureMessageType.PasePake2, TlvPasePake2);
  }
  readPasePake3() {
    return this.nextMessageDecoded(TlvPasePake3, { type: SecureMessageType.PasePake3 });
  }
  close() {
    this.#closed = true;
    return super.close();
  }
  send(message, type, schema, options) {
    if (this.#closed) {
      throw new UnexpectedDataError("Cannot send message, messenger is closed");
    }
    return super.send(message, type, schema, options);
  }
  async nextMessage(options) {
    if (this.#closed) {
      throw new UnexpectedDataError("Cannot read message, messenger is closed");
    }
    const result = await super.nextMessage(options);
    if (this.#closed) {
      throw new UnexpectedDataError("Cannot read message, messenger is closed");
    }
    return result;
  }
}
class PaseClientMessenger extends SecureChannelMessenger {
  sendPbkdfParamRequest(request, options) {
    return this.send(request, SecureMessageType.PbkdfParamRequest, TlvPbkdfParamRequest, {
      expectedProcessingTime: DEFAULT_NORMAL_PROCESSING_TIME,
      ...options
    });
  }
  async readPbkdfParamResponse(options) {
    const { payload } = await this.nextMessage({
      type: SecureMessageType.PbkdfParamResponse,
      expectedProcessingTime: DEFAULT_NORMAL_PROCESSING_TIME,
      abort: options?.abort
    });
    return { responsePayload: payload, response: TlvPbkdfParamResponse.decode(payload) };
  }
  sendPasePake1(pasePake1, options) {
    return this.send(pasePake1, SecureMessageType.PasePake1, TlvPasePake1, options);
  }
  readPasePake2(options) {
    return this.nextMessageDecoded(TlvPasePake2, { type: SecureMessageType.PasePake2, ...options });
  }
  sendPasePake3(pasePake3, options) {
    return this.send(pasePake3, SecureMessageType.PasePake3, TlvPasePake3, options);
  }
}
export {
  DEFAULT_PASSCODE_ID,
  PaseClientMessenger,
  PaseServerMessenger,
  SPAKE_CONTEXT
};
//# sourceMappingURL=PaseMessenger.js.map
