"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 OtaImageWriter_exports = {};
__export(OtaImageWriter_exports, {
  OtaImageWriter: () => OtaImageWriter
});
module.exports = __toCommonJS(OtaImageWriter_exports);
var import_general = require("@matter/general");
var import_OtaImageHeader = require("./OtaImageHeader.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
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: import_general.HashFipsAlgorithmId[imageDigestType],
      imageDigest: import_general.Bytes.of(await crypto.computeHash(payload, imageDigestType))
    };
    const headerTlv = import_general.Bytes.of(import_OtaImageHeader.TlvOtaImageHeader.encode(header));
    const writer = new import_general.DataWriter(import_general.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 = import_general.Bytes.toBase64(import_general.Bytes.of(fullChecksum));
    return {
      image,
      fullFileChecksum,
      fullFileChecksumType
    };
  }
}
//# sourceMappingURL=OtaImageWriter.js.map
