/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Bytes, DataWriter, Endian, HashFipsAlgorithmId } from "@matter/general";
import { TlvOtaImageHeader } from "./OtaImageHeader.js";
class OtaImageWriter {
  /**
   * Create a complete OTA image file.
   *
   * @param crypto - Crypto implementation for computing hash digests
   * @param options - Image configuration options
   * @param options.imageDigestType - Hash algorithm for payload digest (defaults to SHA-256)
   * @param options.fullFileChecksumType - Hash algorithm for full file checksum (defaults to SHA-256)
   * @returns OTA image result containing the image data and full file checksum
   */
  static async create(crypto, options) {
    const {
      vendorId,
      productId,
      softwareVersion,
      softwareVersionString,
      minApplicableSoftwareVersion,
      maxApplicableSoftwareVersion,
      releaseNotesUrl,
      payload,
      imageDigestType = "SHA-256",
      fullFileChecksumType = "SHA-256"
    } = options;
    const header = {
      vendorId,
      productId,
      softwareVersion,
      softwareVersionString,
      minApplicableSoftwareVersion,
      maxApplicableSoftwareVersion,
      releaseNotesUrl,
      payloadSize: BigInt(payload.byteLength),
      imageDigestType: HashFipsAlgorithmId[imageDigestType],
      imageDigest: Bytes.of(await crypto.computeHash(payload, imageDigestType))
    };
    const headerTlv = Bytes.of(TlvOtaImageHeader.encode(header));
    const writer = new DataWriter(Endian.Little);
    writer.writeUInt32(468644126);
    writer.writeUInt64(BigInt(16 + headerTlv.length + payload.byteLength));
    writer.writeUInt32(headerTlv.length);
    writer.writeByteArray(headerTlv);
    writer.writeByteArray(payload);
    const image = writer.toByteArray();
    const fullChecksum = await crypto.computeHash(image, fullFileChecksumType);
    const fullFileChecksum = Bytes.toBase64(Bytes.of(fullChecksum));
    return {
      image,
      fullFileChecksum,
      fullFileChecksumType
    };
  }
}
export {
  OtaImageWriter
};
//# sourceMappingURL=OtaImageWriter.js.map
