"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 InboundFlow_exports = {};
__export(InboundFlow_exports, {
  InboundFlow: () => InboundFlow
});
module.exports = __toCommonJS(InboundFlow_exports);
var import_general = require("#general");
var import_types = require("#types");
var import_BdxError = require("../BdxError.js");
var import_Flow = require("./Flow.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class InboundFlow extends import_Flow.Flow {
  #closeStreams;
  #writeController;
  #writePromise;
  /**
   * Returns initialized streams for the transfer and initializes the #closeStream class function
   */
  async initTransfer() {
    let writeController;
    const stream = new ReadableStream({
      start: (controller) => {
        writeController = controller;
      }
    });
    const { fileDesignator } = this.transferParameters;
    const writePromise = fileDesignator.writeFromStream(stream);
    this.#closeStreams = async (error) => {
      if (writeController !== void 0) {
        if (error !== void 0) {
          writeController.error(error);
          try {
            await writePromise;
          } catch {
          }
        }
      }
      if (!stream.locked) {
        await stream.cancel();
      }
    };
    this.#writeController = writeController;
    this.#writePromise = writePromise;
  }
  get stream() {
    if (!this.#writeController || !this.#writePromise) {
      throw new import_general.InternalError("Transfer not initialized. Call initTransfer() before starting the transfer.");
    }
    return { writeController: this.#writeController, writePromise: this.#writePromise };
  }
  async finalizeTransfer() {
    const { writePromise } = this.stream;
    const blockCounter = this.finalBlockCounter;
    await writePromise;
    await this.messenger.sendBlockAckEof({ blockCounter });
  }
  writeDataChunk(writeController, data, messageType) {
    writeController.enqueue(data);
    if (this.bytesLeft !== void 0) {
      this.bytesLeft -= data.byteLength;
    }
    let done = false;
    if (messageType === import_types.BdxMessageType.BlockEof) {
      if (this.bytesLeft !== void 0 && this.bytesLeft !== 0) {
        throw new import_BdxError.BdxError(
          `Received BlockEof with ${this.bytesLeft}bytes left, but no more data available`,
          this.bytesLeft > 0 ? import_types.BdxStatusCode.LengthTooShort : import_types.BdxStatusCode.LengthTooLarge
        );
      }
      done = true;
      writeController.close();
    }
    return done;
  }
  async close(error) {
    await super.close(error);
    await this.#closeStreams?.(error);
  }
}
//# sourceMappingURL=InboundFlow.js.map
