import * as tsplus_module_1 from "@effect/core/stm/STM/operations/succeedNow";
import * as tsplus_module_2 from "@tsplus/stdlib/collections/Chunk/concat";
import * as tsplus_module_3 from "@tsplus/stdlib/collections/Chunk/append";
import * as tsplus_module_4 from "@effect/core/stm/THub/operations/TDequeue/take";
import * as tsplus_module_5 from "@effect/core/stm/STM/operations/map";
import * as tsplus_module_6 from "@effect/core/stm/STM/operations/flatMap";
import * as tsplus_module_7 from "@effect/core/stm/STM/operations/succeed";
import * as tsplus_module_8 from "@effect/core/stm/THub/operations/TDequeue/takeUpTo";
export const takeRemainder_ = takeRemainder_1;
/**
 * @tsplus fluent ets/THub/TDequeue takeRemainder
 */

function takeRemainder_1(self, min, max, acc) {
  if (max < min) {
    return tsplus_module_1.succeedNow(acc);
  }

  return tsplus_module_6.flatMap_(tsplus_module_8.takeUpTo_(self, max), bs => {
    const remaining = min - bs.length;

    if (remaining === 1) {
      return tsplus_module_5.map_(tsplus_module_4.take(self), b => tsplus_module_3.append_(tsplus_module_2.concat_(acc, bs), b));
    }

    if (remaining > 1) {
      return tsplus_module_6.flatMap_(tsplus_module_4.take(self), b => takeRemainder_1(self, remaining - 1, max - bs.length - 1, tsplus_module_3.append_(tsplus_module_2.concat_(acc, bs), b)));
    }

    return tsplus_module_7.succeed(() => tsplus_module_2.concat_(acc, bs));
  });
}
/**
 * @internal
 * @tsplus static ets/THub/TDequeue/Aspects takeRemainder
 */


export const takeRemainder = (min, max, acc) => self => takeRemainder_(self, min, max, acc);
//# sourceMappingURL=takeRemainder.mjs.map