"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 FollowingReceivingFlow_exports = {};
__export(FollowingReceivingFlow_exports, {
  FollowingReceivingFlow: () => FollowingReceivingFlow
});
module.exports = __toCommonJS(FollowingReceivingFlow_exports);
var import_general = require("#general");
var import_types = require("#types");
var import_BdxMessenger = require("../BdxMessenger.js");
var import_BdxMessage = require("../schema/BdxMessage.js");
var import_OutboundFlow = require("./OutboundFlow.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const logger = import_general.Logger.get("BdxFollowingReceivingFlow");
class FollowingReceivingFlow extends import_OutboundFlow.OutboundFlow {
  async transferNextChunk() {
    const { iterator, streamReader } = this.stream;
    const blockQuery = await this.messenger.readBlockQuery();
    const bytesToSkip = import_BdxMessage.BdxMessage.is(blockQuery, import_types.BdxMessageType.BlockQueryWithSkip) ? import_BdxMessenger.BdxMessenger.asSafeNumber(
      blockQuery.message.bytesToSkip,
      "BytesToSkip",
      import_types.BdxStatusCode.TransferFailedUnknownError
    ) : 0;
    const { blockCounter } = blockQuery.message;
    this.validateCounter(blockCounter);
    if (bytesToSkip > 0) {
      const skipped = await streamReader.skip(bytesToSkip);
      logger.debug(`Skipped ${skipped}bytes of data (requested ${bytesToSkip}bytes)`);
      this.transferredBytes += bytesToSkip;
    }
    const { data, done } = await this.readDataChunk(iterator);
    if (done) {
      await this.messenger.sendBlockEof({ data, blockCounter });
      this.transferredBytes += data.byteLength;
      this.finalBlockCounter = blockCounter;
      return true;
    }
    await this.messenger.sendBlock({ data, blockCounter });
    this.transferredBytes += data.byteLength;
    return false;
  }
}
//# sourceMappingURL=FollowingReceivingFlow.js.map
