"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.tapSink = void 0;
exports.tapSink_ = tapSink_;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/fromQueueWithShutdown"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/flattenTake"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Take/operations/chunk"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/fromEffect"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/write"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/zipRight"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Take/operations/failCause"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/readWithCause"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/run"));

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/execute"));

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/TerminationStrategy"));

var tsplus_module_12 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/pipeTo"));

var tsplus_module_13 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/merge"));

var tsplus_module_14 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Queue/external/bounded"));

var tsplus_module_15 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/fromEffect"));

var tsplus_module_16 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/flatMap"));

var _StreamInternal = /*#__PURE__*/require("@effect/core/stream/Stream/operations/_internal/StreamInternal");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/**
 * Sends all elements emitted by this stream to the specified sink in addition
 * to emitting them.
 *
 * @tsplus fluent ets/Stream tapSink
 */
function tapSink_(self, sink, __tsplusTrace) {
  return tsplus_module_16.flatMap_(tsplus_module_15.fromEffect(() => tsplus_module_14.bounded(1, __tsplusTrace), __tsplusTrace), queue => {
    const right = tsplus_module_2.flattenTake(tsplus_module_1.fromQueueWithShutdown(() => queue, 1, __tsplusTrace), __tsplusTrace);
    const loop = tsplus_module_8.readWithCause(chunk => tsplus_module_6.zipRight_(tsplus_module_6.zipRight_(tsplus_module_4.fromEffect(() => queue.offer(tsplus_module_3.chunk(chunk), __tsplusTrace)), () => tsplus_module_5.write(() => chunk)), () => loop), cause => tsplus_module_4.fromEffect(() => queue.offer(tsplus_module_7.failCause(cause), __tsplusTrace)), () => tsplus_module_4.fromEffect(() => queue.shutdown));
    void 0;
    return tsplus_module_13.merge_(new _StreamInternal.StreamInternal(tsplus_module_12.pipeTo_(self.channel, () => loop)), () => tsplus_module_10.execute(() => tsplus_module_9.run_(right, sink, __tsplusTrace), __tsplusTrace), () => tsplus_module_11.both, __tsplusTrace);
  }, __tsplusTrace);
}
/**
 * Sends all elements emitted by this stream to the specified sink in addition
 * to emitting them.
 *
 * @tsplus static ets/Stream/Aspects tapSink
 */


const tapSink = (sink, __tsplusTrace) => self => tapSink_(self, sink, __tsplusTrace);

exports.tapSink = tapSink;
//# sourceMappingURL=tapSink.js.map