import * as tracing_1 from "../../../Tracing";
const fileName_1 = "(@effect-ts/system): _src/Experimental/Stream/GroupBy/GroupBy.ts";
import * as Mp from "../../../Collections/Immutable/Map";
import * as Tp from "../../../Collections/Immutable/Tuple";
import * as T from "../../../Effect";
import * as Ex from "../../../Exit";
import { pipe } from "../../../Function";
import * as M from "../../../Managed";
import * as O from "../../../Option";
import * as P from "../../../Promise";
import * as Q from "../../../Queue";
import * as Ref from "../../../Ref";
import * as ChainPar from "../_internal/api/chainPar";
import * as DistributedWithDynamic from "../_internal/api/distributedWithDynamic";
import * as FilterEffect from "../_internal/api/filterEffect";
import * as FlattenExitOption from "../_internal/api/flattenExitOption";
import * as FromQueueWithShutdown from "../_internal/api/fromQueueWithShutdown";
import * as Map from "../_internal/api/map";
import * as MapEffect from "../_internal/api/mapEffect";
import * as UnwrapManaged from "../_internal/api/unwrapManaged";
import * as ZipWithIndex from "../_internal/api/zipWithIndex";
export class GroupBy {}

class GroupByInternal extends GroupBy {
  constructor(stream, key, buffer) {
    super();
    this.stream = stream;
    this.key = key;
    this.buffer = buffer;
    this.grouped = UnwrapManaged.unwrapManaged(M.map_(M.tap_(M.bind_(M.bind_(M.bind_(M.bind_(M.do, "decider", () => T.toManaged(P.make()), fileName_1 + ":45:13"), "out", () => T.toManagedRelease_(Q.makeBounded(this.buffer, fileName_1 + ":50:24"), Q.shutdown), fileName_1 + ":48:13"), "ref", () => T.toManaged(Ref.makeRef(Mp.empty)), fileName_1 + ":56:13"), "add", ({
      decider,
      out
    }) => DistributedWithDynamic.distributedWithDynamic_(MapEffect.mapEffect_(this.stream, this.key), this.buffer, ({
      tuple: [k, v]
    }) => T.chain_(P.await(decider), _ => _(k, v), fileName_1 + ":64:23"), _ => Q.offer_(out, _)), fileName_1 + ":57:13"), ({
      add,
      decider,
      out,
      ref
    }) => T.toManaged(P.succeed_(decider, (k, _) => T.chain_(T.map_(ref.get, Mp.lookup(k), fileName_1 + ":74:20"), O.fold(() => T.chain_(add, ({
      tuple: [idx, q]
    }) => T.as_(T.zipRight_(Ref.update_(ref, Mp.insert(k, idx)), Q.offer_(out, Ex.succeed(Tp.tuple(k, Q.map_(q, Ex.map(({
      tuple: [_, a]
    }) => a))))), fileName_1 + ":80:36"), _ => _ === idx, fileName_1 + ":79:28"), fileName_1 + ":78:29"), idx => T.succeed(_ => _ === idx, fileName_1 + ":98:37")), fileName_1 + ":75:22"))), fileName_1 + ":69:12"), ({
      out
    }) => FlattenExitOption.flattenExitOption(FromQueueWithShutdown.fromQueueWithShutdown_(out)), fileName_1 + ":105:12"));
  }
  /**
   * Only consider the first n groups found in the stream.
   */


  first(n) {
    return new FirstInternal(this.stream, this.key, this.buffer, n);
  }
  /**
   * Only consider the first n groups found in the stream.
   */


  filter(f) {
    return new FilterInternal(this.stream, this.key, this.buffer, f);
  }

  apply(f) {
    return ChainPar.chainPar_(this.grouped, Number.MAX_SAFE_INTEGER, ({
      tuple: [k, q]
    }) => f(k, FlattenExitOption.flattenExitOption(FromQueueWithShutdown.fromQueueWithShutdown_(q))));
  }

}

class FirstInternal extends GroupByInternal {
  constructor(stream, key, buffer, n) {
    super(stream, key, buffer);
    this.n = n;
    this.grouped = Map.map_(FilterEffect.filterEffect_(ZipWithIndex.zipWithIndex(super.grouped), elem => {
      const {
        tuple: [{
          tuple: [_, q]
        }, i]
      } = elem;
      return i < this.n ? T.as_(T.succeed(elem, fileName_1 + ":167:42"), true, fileName_1 + ":167:32") : T.as_(Q.shutdown(q), false, fileName_1 + ":167:63");
    }), Tp.get(0));
  }

}

class FilterInternal extends GroupByInternal {
  constructor(stream, key, buffer, f) {
    super(stream, key, buffer);
    this.f = f;
    this.grouped = FilterEffect.filterEffect_(super.grouped, elem => {
      const {
        tuple: [k, q]
      } = elem;
      return this.f(k) ? T.as_(T.succeed(elem, fileName_1 + ":190:41"), true, fileName_1 + ":190:31") : T.as_(Q.shutdown(q), false, fileName_1 + ":190:62");
    });
  }

}

function concrete(_groupBy) {//
}

export function make_(stream, key, buffer) {
  return new GroupByInternal(stream, key, buffer);
}
/**
 * @ets_data_first make_
 */

export function make(key, buffer) {
  return stream => make_(stream, key, buffer);
}
/**
 * Only consider the first n groups found in the stream.
 */

export function filter_(self, f) {
  concrete(self);
  return self.filter(f);
}
/**
 * Only consider the first n groups found in the stream.
 *
 * @ets_data_first filter_
 */

export function filter(f) {
  return self => filter_(self, f);
}
/**
 * Only consider the first n groups found in the stream.
 */

export function first_(self, n) {
  concrete(self);
  return self.first(n);
}
/**
 * Only consider the first n groups found in the stream.
 *
 * @ets_data_first first_
 */

export function first(n) {
  return self => first_(self, n);
}
export function mergeGroupBy_(self, f) {
  concrete(self);
  return self.apply(f);
}
/**
 * @ets_data_first mergeGroupBy_
 */

export function mergeGroupBy(f) {
  return self => mergeGroupBy_(self, f);
}
//# sourceMappingURL=GroupBy.js.map