"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 BdxInitMessagesSchema_exports = {};
__export(BdxInitMessagesSchema_exports, {
  BDX_VERSION: () => BDX_VERSION,
  BdxInitMessageSchema: () => BdxInitMessageSchema,
  BdxRangeControlBitmap: () => BdxRangeControlBitmap,
  BdxRangeControlSchema: () => BdxRangeControlSchema,
  BdxReceiveInitMessage: () => BdxReceiveInitMessage,
  BdxSendInitMessage: () => BdxSendInitMessage,
  BdxTransferControlBitmap: () => BdxTransferControlBitmap,
  BdxTransferControlSchema: () => BdxTransferControlSchema
});
module.exports = __toCommonJS(BdxInitMessagesSchema_exports);
var import_general = require("@matter/general");
var import_types = require("@matter/types");
var import_BdxError = require("../BdxError.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const BDX_VERSION = 0;
const BdxTransferControlBitmap = {
  /** Protocol Version, Only allowed version is 0.0 for now */
  version: (0, import_types.BitField)(0, 4),
  /** Indicates if Sender drive is supported by Initiator */
  senderDrive: (0, import_types.BitFlag)(4),
  /** Indicates if Receiver drive is supported by Initiator */
  receiverDrive: (0, import_types.BitFlag)(5),
  /**
   * Indicates if Asynchronous Transfer is supported by Initiator
   * Synchronous mode is always implicitly supported.
   */
  asynchronousTransfer: (0, import_types.BitFlag)(6)
};
const BdxTransferControlSchema = (0, import_types.BitmapSchema)(BdxTransferControlBitmap);
const BdxRangeControlBitmap = {
  /** Indicates if the transfer has a definite length */
  definiteLength: (0, import_types.BitFlag)(0),
  /** Indicates if the transfer has a start offset */
  startOffset: (0, import_types.BitFlag)(1),
  /**
   * Indicates if wide (64-bit) range enabled for values
   * Offset (STARTFOFS) and length (DEFLEN) are 4 octets (32-bit) little-endian unsigned quantities.
   */
  wideRange: (0, import_types.BitFlag)(4)
};
const BdxRangeControlSchema = (0, import_types.BitmapSchema)(BdxRangeControlBitmap);
class BdxInitMessageSchema extends import_types.Schema {
  encodeInternal(message) {
    this.validate(message);
    const { transferProtocol, maxBlockSize, startOffset, maxLength, fileDesignator, metaData } = message;
    const rangeControl = {
      startOffset: startOffset !== void 0,
      definiteLength: maxLength !== void 0 && maxLength !== 0,
      wideRange: typeof startOffset === "bigint" || typeof maxLength === "bigint"
    };
    const writer = new import_general.DataWriter(import_general.Endian.Little);
    writer.writeUInt8(BdxTransferControlSchema.encode(transferProtocol));
    writer.writeUInt8(BdxRangeControlSchema.encode(rangeControl));
    writer.writeUInt16(maxBlockSize);
    if (startOffset !== void 0) {
      if (rangeControl.wideRange) {
        writer.writeUInt64(startOffset);
      } else {
        writer.writeUInt32(startOffset);
      }
    }
    if (maxLength !== void 0 && maxLength !== 0) {
      if (rangeControl.wideRange) {
        writer.writeUInt64(maxLength);
      } else {
        writer.writeUInt32(maxLength);
      }
    }
    writer.writeUInt16(fileDesignator.byteLength);
    writer.writeByteArray(fileDesignator);
    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 transferProtocol = BdxTransferControlSchema.decode(reader.readUInt8());
    const rangeControl = BdxRangeControlSchema.decode(reader.readUInt8());
    const maxBlockSize = reader.readUInt16();
    const startOffset = rangeControl.startOffset ? rangeControl.wideRange ? reader.readUInt64() : reader.readUInt32() : void 0;
    let maxLength = rangeControl.definiteLength ? rangeControl.wideRange ? reader.readUInt64() : reader.readUInt32() : void 0;
    if (maxLength === 0) {
      maxLength = void 0;
    }
    const fileDesignatorLength = reader.readUInt16();
    const fileDesignator = reader.readByteArray(fileDesignatorLength);
    const metaData = reader.remainingBytesCount > 0 ? reader.remainingBytes : void 0;
    const result = {
      transferProtocol,
      maxBlockSize,
      startOffset,
      maxLength,
      fileDesignator,
      metaData
    };
    this.validate(result);
    return result;
  }
  validate({ transferProtocol, maxBlockSize, fileDesignator }) {
    if (transferProtocol.version !== BDX_VERSION) {
      throw new import_BdxError.BdxError(
        `Unsupported BDX version: ${transferProtocol.version}`,
        import_types.BdxStatusCode.VersionNotSupported
      );
    }
    if (!transferProtocol.senderDrive && !transferProtocol.receiverDrive) {
      throw new import_BdxError.BdxError(
        "BDX transfer protocol needs to allow either sender or receiver drive",
        import_types.BdxStatusCode.BadMessageContent
      );
    }
    if (fileDesignator.byteLength === 0) {
      throw new import_BdxError.BdxError("File Designator cannot be empty", import_types.BdxStatusCode.BadMessageContent);
    }
    if (fileDesignator.byteLength > 65535) {
      throw new import_BdxError.BdxError(
        "File Designator length exceeds maximum allowed size of 65535bytes",
        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 BdxSendInitMessage = new BdxInitMessageSchema();
const BdxReceiveInitMessage = BdxSendInitMessage;
//# sourceMappingURL=BdxInitMessagesSchema.js.map
