import {
  Functor,
  bimap,
  both,
  getApply,
  left,
  mapLeft,
  match,
  right,
  swap,
  toTuple2
} from "./chunk-DTQCPQTO.mjs";
import {
  map
} from "./chunk-WKK6AYQI.mjs";
import {
  ap
} from "./chunk-ZMCLCMOU.mjs";
import {
  flow,
  pipe
} from "./chunk-ZI34HWXZ.mjs";
import {
  __export
} from "./chunk-QK4PKNB3.mjs";

// src/TheseT.ts
var TheseT_exports = {};
__export(TheseT_exports, {
  ap: () => ap2,
  bimap: () => bimap2,
  both: () => both2,
  chain: () => chain,
  getTheseM: () => getTheseM,
  left: () => left2,
  leftF: () => leftF,
  map: () => map2,
  mapLeft: () => mapLeft2,
  match: () => match2,
  matchE: () => matchE,
  right: () => right2,
  rightF: () => rightF,
  swap: () => swap2,
  toTuple2: () => toTuple22
});
function right2(F) {
  return flow(right, F.of);
}
function left2(F) {
  return flow(left, F.of);
}
function both2(F) {
  return flow(both, F.of);
}
function rightF(F) {
  return (fa) => F.map(fa, right);
}
function leftF(F) {
  return (fe) => F.map(fe, left);
}
function map2(F) {
  return map(F, Functor);
}
function ap2(F, S) {
  return ap(F, getApply(S));
}
function chain(M, S) {
  const _left = left2(M);
  return (f) => (ma) => M.chain(
    ma,
    match(
      _left,
      f,
      (e1, a) => M.map(
        f(a),
        match(
          (e2) => left(S.concat(e1, e2)),
          (b) => both(e1, b),
          (e2, b) => both(S.concat(e1, e2), b)
        )
      )
    )
  );
}
function bimap2(F) {
  return (f, g) => (fea) => F.map(fea, bimap(f, g));
}
function mapLeft2(F) {
  return (f) => (fea) => F.map(fea, mapLeft(f));
}
function match2(F) {
  return (onLeft, onRight, onBoth) => (ma) => F.map(ma, match(onLeft, onRight, onBoth));
}
function matchE(M) {
  return (onLeft, onRight, onBoth) => (ma) => M.chain(ma, match(onLeft, onRight, onBoth));
}
function swap2(F) {
  return (ma) => F.map(ma, swap);
}
function toTuple22(F) {
  return (e, a) => (fa) => F.map(fa, toTuple2(e, a));
}
function getTheseM(M) {
  const _map = map2(M);
  const _bimap = bimap2(M);
  const _mapLeft = mapLeft2(M);
  const _fold = matchE(M);
  const _toTuple2 = toTuple22(
    M
  );
  const of = right2(M);
  const mapT = (fa, f) => pipe(fa, _map(f));
  return {
    map: mapT,
    bimap: (fea, f, g) => pipe(fea, _bimap(f, g)),
    mapLeft: (fea, f) => pipe(fea, _mapLeft(f)),
    fold: (fa, onLeft, onRight, onBoth) => pipe(fa, _fold(onLeft, onRight, onBoth)),
    swap: swap2(M),
    rightM: rightF(M),
    leftM: leftF(M),
    left: left2(M),
    right: right2(M),
    both: both2(M),
    toTuple: (fa, e, a) => pipe(
      fa,
      _toTuple2(
        () => e,
        () => a
      )
    ),
    getMonad: (S) => {
      const _ap = ap2(M, S);
      const _chain = chain(M, S);
      return {
        _E: void 0,
        map: mapT,
        of,
        ap: (fab, fa) => pipe(fab, _ap(fa)),
        chain: (ma, f) => pipe(ma, _chain(f))
      };
    }
  };
}

export {
  right2 as right,
  left2 as left,
  both2 as both,
  rightF,
  leftF,
  map2 as map,
  ap2 as ap,
  chain,
  bimap2 as bimap,
  mapLeft2 as mapLeft,
  match2 as match,
  matchE,
  swap2 as swap,
  toTuple22 as toTuple2,
  getTheseM,
  TheseT_exports
};
