"use strict";

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

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Maybe/definition"));

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

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

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

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

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

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/single"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Maybe/fold"));

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

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/empty"));

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Tuple/make"));

var tsplus_module_12 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/append"));

var tsplus_module_13 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/reduce"));

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; }

/**
 * Creates a stream that groups on adjacent keys, calculated by function f.
 *
 * @tsplus fluent ets/Stream groupAdjacentBy
 */
function groupAdjacentBy_(self, f, __tsplusTrace) {
  void 0;
  return new _StreamInternal.StreamInternal(tsplus_module_2.pipeTo_(self.channel, () => chunkAdjacent(tsplus_module_1.none, f, __tsplusTrace)));
}
/**
 * Creates a stream that groups on adjacent keys, calculated by function f.
 *
 * @tsplus static ets/Stream/Aspects groupAdjacentBy
 */


const groupAdjacentBy = (f, __tsplusTrace) => self => groupAdjacentBy_(self, f, __tsplusTrace);

exports.groupAdjacentBy = groupAdjacentBy;

function chunkAdjacent(buffer, f, __tsplusTrace) {
  return tsplus_module_9.readWithCause(chunk => {
    const {
      tuple: [outputs, newBuffer]
    } = go(chunk, buffer, f, __tsplusTrace);
    return tsplus_module_4.zipRight_(tsplus_module_3.write(() => outputs), () => chunkAdjacent(newBuffer, f, __tsplusTrace));
  }, cause => tsplus_module_5.failCause(() => cause), () => tsplus_module_8.fold_(buffer, () => tsplus_module_6.unit, o => tsplus_module_3.write(() => tsplus_module_7.single(o))));
}

function go(input, state, f, __tsplusTrace) {
  return tsplus_module_13.reduce_(input, tsplus_module_11.make(tsplus_module_10.empty(), state), ({
    tuple: [os, o]
  }, a) => tsplus_module_8.fold_(o, () => tsplus_module_11.make(os, tsplus_module_1.some(tsplus_module_11.make(f(a), tsplus_module_7.single(a)))), agg => {
    const k2 = f(a);
    const {
      tuple: [k, aggregated]
    } = agg;

    if (k === k2) {
      return tsplus_module_11.make(os, tsplus_module_1.some(tsplus_module_11.make(k, tsplus_module_12.append_(aggregated, a))));
    } else {
      return tsplus_module_11.make(tsplus_module_12.append_(os, agg), tsplus_module_1.some(tsplus_module_11.make(k2, tsplus_module_7.single(a))));
    }
  }));
}
//# sourceMappingURL=groupAdjacentBy.js.map