import * as tracing_1 from "../../../Tracing";
const fileName_1 = "(@effect-ts/system): _src/Experimental/Stream/_internal/zip.ts";
import * as Cause from "../../../Cause";
import * as Chunk from "../../../Collections/Immutable/Chunk";
import * as Tp from "../../../Collections/Immutable/Tuple";
import * as T from "../../../Effect";
import * as E from "../../../Either";
import * as Exit from "../../../Exit";
import * as O from "../../../Option";
import * as S from "./core";
import { zipChunks_ } from "./utils";

class Running {
  constructor(excess) {
    this.excess = excess;
    this._tag = "Running";
  }

}

class LeftDone {
  constructor(excessL) {
    this.excessL = excessL;
    this._tag = "LeftDone";
  }

}

class RightDone {
  constructor(excessR) {
    this.excessR = excessR;
    this._tag = "RightDone";
  }

}

class End {
  constructor() {
    this._tag = "End";
  }

}

function handleSuccess(f, leftUpd, rightUpd, excess) {
  const [leftExcess, rightExcess] = E.fold_(excess, l => [l, Chunk.empty()], r => [Chunk.empty(), r]);
  const left = O.fold_(leftUpd, () => leftExcess, upd => Chunk.concat_(leftExcess, upd));
  const right = O.fold_(rightUpd, () => rightExcess, upd => Chunk.concat_(rightExcess, upd));
  const {
    tuple: [emit, newExcess]
  } = zipChunks_(left, right, f);

  if (leftUpd._tag === "Some" && rightUpd._tag === "Some") {
    return Exit.succeed(Tp.tuple(emit, new Running(newExcess)));
  }

  if (leftUpd._tag === "None" && rightUpd._tag === "None") {
    return Exit.fail(O.none);
  }

  const newState = newExcess._tag === "Left" ? Chunk.isEmpty(newExcess.left) ? new End() : new LeftDone(newExcess.left) : Chunk.isEmpty(newExcess.right) ? new End() : new RightDone(newExcess.right);
  return Exit.succeed(Tp.tuple(emit, newState));
}
/**
 * Zips this stream with another point-wise and applies the function to the paired elements.
 *
 * The new stream will end when one of the sides ends.
 */


export function zipWith_(self, that, f) {
  return S.combineChunks_(self, that, new Running(E.left(Chunk.empty())), (st, p1, p2) => {
    switch (st._tag) {
      case "End":
        {
          return T.succeed(Exit.fail(O.none), fileName_1 + ":87:27");
        }

      case "Running":
        {
          return T.catchAllCause_(T.zipWithPar_(T.optional(p1, fileName_1 + ":91:37"), T.optional(p2, fileName_1 + ":91:53"), (l, r) => handleSuccess(f, l, r, st.excess), fileName_1 + ":91:26"), e => T.succeed(Exit.halt(Cause.map_(e, O.some)), fileName_1 + ":94:29"), fileName_1 + ":90:34");
        }

      case "LeftDone":
        {
          return T.catchAllCause_(T.map_(T.optional(p2, fileName_1 + ":99:30"), l => handleSuccess(f, O.none, l, E.left(st.excessL)), fileName_1 + ":99:19"), e => T.succeed(Exit.halt(Cause.map_(e, O.some)), fileName_1 + ":102:29"), fileName_1 + ":98:34");
        }

      case "RightDone":
        {
          return T.catchAllCause_(T.map_(T.optional(p1, fileName_1 + ":107:30"), r => handleSuccess(f, r, O.none, E.right(st.excessR)), fileName_1 + ":107:19"), e => T.succeed(Exit.halt(Cause.map_(e, O.some)), fileName_1 + ":110:29"), fileName_1 + ":106:34");
        }
    }
  });
}
/**
 * Zips this stream with another point-wise and applies the function to the paired elements.
 *
 * The new stream will end when one of the sides ends.
 *
 * @ets_data_first zipWith_
 */

export function zipWith(that, f) {
  return self => zipWith_(self, that, f);
}
/**
 * Zips this stream with another point-wise and emits tuples of elements from both streams.
 *
 * The new stream will end when one of the sides ends.
 */

export function zip_(self, that) {
  return zipWith_(self, that, Tp.tuple);
}
/**
 * Zips this stream with another point-wise and emits tuples of elements from both streams.
 *
 * The new stream will end when one of the sides ends.
 *
 * @ets_data_first zip_
 */

export function zip(that) {
  return self => zip_(self, that);
}
/**
 * Zips this stream with another point-wise, but keeps only the outputs of the other stream.
 *
 * The new stream will end when one of the sides ends.
 */

export function zipRight_(self, that) {
  return zipWith_(self, that, (_, o) => o);
}
/**
 * Zips this stream with another point-wise, but keeps only the outputs of the other stream.
 *
 * The new stream will end when one of the sides ends.
 *
 * @ets_data_first zipRight_
 */

export function zipRight(that) {
  return self => zipRight_(self, that);
}
/**
 * Zips this stream with another point-wise, but keeps only the outputs of this stream.
 *
 * The new stream will end when one of the sides ends.
 */

export function zipLeft_(self, that) {
  return zipWith_(self, that, (o, _) => o);
}
/**
 * Zips this stream with another point-wise, but keeps only the outputs of this stream.
 *
 * The new stream will end when one of the sides ends.
 *
 * @ets_data_first zipLeft_
 */

export function zipLeft(that) {
  return self => zipLeft_(self, that);
}
//# sourceMappingURL=zip.js.map