"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 BdxAcceptMessagesSchema_exports = {};
__export(BdxAcceptMessagesSchema_exports, {
  BdxReceiveAcceptMessage: () => BdxReceiveAcceptMessage,
  BdxReceiveAcceptMessageSchema: () => BdxReceiveAcceptMessageSchema,
  BdxSendAcceptMessage: () => BdxSendAcceptMessage,
  BdxSendAcceptMessageSchema: () => BdxSendAcceptMessageSchema
});
module.exports = __toCommonJS(BdxAcceptMessagesSchema_exports);
var import_general = require("#general");
var import_types = require("#types");
var import_BdxError = require("../BdxError.js");
var import_BdxInitMessagesSchema = require("./BdxInitMessagesSchema.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class BdxSendAcceptMessageSchema extends import_types.Schema {
  encodeInternal(message) {
    this.validate(message);
    const { transferControl, maxBlockSize, metaData } = message;
    const writer = new import_general.DataWriter(import_general.Endian.Little);
    writer.writeUInt8(import_BdxInitMessagesSchema.BdxTransferControlSchema.encode(transferControl));
    writer.writeUInt16(maxBlockSize);
    if (metaData && metaData.byteLength > 0) {
      writer.writeByteArray(metaData);
    }
    return writer.toByteArray();
  }
  decodeInternal(bytes) {
    const reader = new import_general.DataReader(bytes, import_general.Endian.Little);
    const transferControl = import_BdxInitMessagesSchema.BdxTransferControlSchema.decode(reader.readUInt8());
    const maxBlockSize = reader.readUInt16();
    const metaData = reader.remainingBytesCount > 0 ? reader.remainingBytes : void 0;
    const result = {
      transferControl,
      maxBlockSize,
      metaData
    };
    this.validate(result);
    return result;
  }
  validate({ transferControl, maxBlockSize }) {
    if (transferControl.version !== import_BdxInitMessagesSchema.BDX_VERSION) {
      throw new import_BdxError.BdxError(
        `Unsupported BDX version: ${transferControl.version}`,
        import_types.BdxStatusCode.VersionNotSupported
      );
    }
    if (!transferControl.senderDrive && !transferControl.receiverDrive && !transferControl.asynchronousTransfer) {
      throw new import_BdxError.BdxError(
        "BDX transfer control need to specify at least one of sender drive, receiver drive or asynchronousTransfer",
        import_types.BdxStatusCode.BadMessageContent
      );
    }
    if (maxBlockSize < 1 || maxBlockSize > 65535) {
      throw new import_BdxError.BdxError("Max Block Size must be between 1 and 65535bytes", import_types.BdxStatusCode.BadMessageContent);
    }
  }
}
const BdxSendAcceptMessage = new BdxSendAcceptMessageSchema();
class BdxReceiveAcceptMessageSchema extends BdxSendAcceptMessageSchema {
  encodeInternal(message) {
    this.validate(message);
    const { transferControl, maxBlockSize, metaData, length } = message;
    const rangeControl = {
      definiteLength: length !== void 0 && length !== 0,
      wideRange: typeof length === "bigint"
    };
    const writer = new import_general.DataWriter(import_general.Endian.Little);
    writer.writeUInt8(import_BdxInitMessagesSchema.BdxTransferControlSchema.encode(transferControl));
    writer.writeUInt8(import_BdxInitMessagesSchema.BdxRangeControlSchema.encode(rangeControl));
    writer.writeUInt16(maxBlockSize);
    if (length !== void 0 && length !== 0) {
      if (rangeControl.wideRange) {
        writer.writeUInt64(length);
      } else {
        writer.writeUInt32(length);
      }
    }
    if (metaData && metaData.byteLength > 0) {
      writer.writeByteArray(metaData);
    }
    return writer.toByteArray();
  }
  decodeInternal(bytes) {
    const reader = new import_general.DataReader(bytes, import_general.Endian.Little);
    const transferControl = import_BdxInitMessagesSchema.BdxTransferControlSchema.decode(reader.readUInt8());
    const rangeControl = import_BdxInitMessagesSchema.BdxRangeControlSchema.decode(reader.readUInt8());
    const maxBlockSize = reader.readUInt16();
    let length = rangeControl.definiteLength ? rangeControl.wideRange ? reader.readUInt64() : reader.readUInt32() : void 0;
    if (length === 0) {
      length = void 0;
    }
    const metaData = reader.remainingBytesCount > 0 ? reader.remainingBytes : void 0;
    const result = {
      transferControl,
      maxBlockSize,
      metaData,
      length
    };
    this.validate(result);
    return result;
  }
}
const BdxReceiveAcceptMessage = new BdxReceiveAcceptMessageSchema();
//# sourceMappingURL=BdxAcceptMessagesSchema.js.map
