"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 Streams_exports = {};
__export(Streams_exports, {
  EndOfStreamError: () => EndOfStreamError,
  NoResponseTimeoutError: () => NoResponseTimeoutError,
  Stream: () => Stream
});
module.exports = __toCommonJS(Streams_exports);
var import_MatterError = require("#MatterError.js");
var import_Promises = require("./Promises.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class EndOfStreamError extends import_MatterError.MatterError {
  constructor(message = "Unexpected end of stream") {
    super(message);
  }
}
class NoResponseTimeoutError extends import_MatterError.TimeoutError {
}
var Stream;
((Stream2) => {
  function from(iterable2) {
    if ("from" in ReadableStream) {
      return ReadableStream.from(iterable2);
    }
    return AnyIterableReadableStream(iterable2);
  }
  Stream2.from = from;
  async function* iterable(stream) {
    if (Symbol.asyncIterator in stream) {
      yield* stream;
      return;
    }
    const reader = stream.getReader();
    try {
      while (true) {
        const next = await reader.read();
        if (next.done) {
          return;
        }
        yield next.value;
      }
    } finally {
      reader.releaseLock();
    }
  }
  Stream2.iterable = iterable;
})(Stream || (Stream = {}));
function AnyIterableReadableStream(iterable) {
  let iterator;
  return new ReadableStream({
    start() {
      if (Symbol.asyncIterator in iterable) {
        iterator = iterable[Symbol.asyncIterator]();
      } else {
        iterator = iterable[Symbol.iterator]();
      }
    },
    async pull(controller) {
      if (!iterator) {
        controller.close();
        return;
      }
      try {
        let next = iterator.next();
        if (import_Promises.MaybePromise.is(next)) {
          next = await next;
        }
        if (next.done) {
          controller.close();
          return;
        }
        controller.enqueue(next.value);
      } catch (e) {
        controller.error(e);
        return;
      }
    },
    async cancel(reason) {
      if (reason) {
        if (iterator?.throw) {
          await iterator.throw(reason);
          return;
        }
        if (iterator?.return) {
          await iterator.return();
        }
        throw reason;
      }
      await iterator?.return?.();
    }
  });
}
//# sourceMappingURL=Streams.js.map
