import {
  Apply,
  Functor,
  bimap,
  fromNullable,
  isLeft,
  left,
  mapLeft,
  match,
  right,
  swap,
  toUnion
} from "./chunk-3AL4EMTU.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/EitherT.ts
var EitherT_exports = {};
__export(EitherT_exports, {
  alt: () => alt,
  altValidation: () => altValidation,
  ap: () => ap2,
  bimap: () => bimap2,
  chain: () => chain,
  chainNullableK: () => chainNullableK,
  flatMap: () => flatMap,
  fromNullable: () => fromNullable2,
  fromNullableK: () => fromNullableK,
  getEitherM: () => getEitherM,
  getOrElse: () => getOrElse,
  left: () => left2,
  leftF: () => leftF,
  map: () => map2,
  mapBoth: () => mapBoth,
  mapError: () => mapError,
  mapLeft: () => mapLeft2,
  match: () => match2,
  matchE: () => matchE,
  orElse: () => orElse,
  orElseFirst: () => orElseFirst,
  orLeft: () => orLeft,
  right: () => right2,
  rightF: () => rightF,
  swap: () => swap2,
  tapError: () => tapError,
  toUnion: () => toUnion2
});
function right2(F) {
  return flow(right, F.of);
}
function left2(F) {
  return flow(left, F.of);
}
function rightF(F) {
  return (fa) => F.map(fa, right);
}
function leftF(F) {
  return (fe) => F.map(fe, left);
}
function fromNullable2(F) {
  return (e) => flow(fromNullable(e), F.of);
}
function fromNullableK(F) {
  const fromNullableF = fromNullable2(F);
  return (e) => {
    const fromNullableFE = fromNullableF(e);
    return (f) => flow(f, fromNullableFE);
  };
}
function chainNullableK(M) {
  const chainM = chain(M);
  const fromNullableKM = fromNullableK(M);
  return (e) => {
    const fromNullableKMe = fromNullableKM(e);
    return (f) => chainM(fromNullableKMe(f));
  };
}
function map2(F) {
  return map(F, Functor);
}
function ap2(F) {
  return ap(F, Apply);
}
function chain(M) {
  const flatMapM = flatMap(M);
  return (f) => (ma) => flatMapM(ma, f);
}
function flatMap(M) {
  return (ma, f) => M.chain(ma, (e) => isLeft(e) ? M.of(e) : f(e.right));
}
function alt(M) {
  return (second) => (first) => M.chain(first, (e) => isLeft(e) ? second() : M.of(e));
}
function bimap2(F) {
  const mapBothF = mapBoth(F);
  return (f, g) => (self) => mapBothF(self, f, g);
}
function mapBoth(F) {
  return (self, f, g) => F.map(self, bimap(f, g));
}
function mapLeft2(F) {
  const mapErrorF = mapError(F);
  return (f) => (self) => mapErrorF(self, f);
}
function mapError(F) {
  return (self, f) => F.map(self, mapLeft(f));
}
function altValidation(M, S) {
  return (second) => (first) => M.chain(
    first,
    match(
      (e1) => M.map(
        second(),
        mapLeft((e2) => S.concat(e1, e2))
      ),
      right2(M)
    )
  );
}
function match2(F) {
  return (onLeft, onRight) => (ma) => F.map(ma, match(onLeft, onRight));
}
function matchE(M) {
  return (onLeft, onRight) => (ma) => M.chain(ma, match(onLeft, onRight));
}
function getOrElse(M) {
  return (onLeft) => (ma) => M.chain(ma, match(onLeft, M.of));
}
function orElse(M) {
  return (onLeft) => (ma) => M.chain(ma, (e) => isLeft(e) ? onLeft(e.left) : M.of(e));
}
function orElseFirst(M) {
  const tapErrorM = tapError(M);
  return (onLeft) => (ma) => tapErrorM(ma, onLeft);
}
function tapError(M) {
  const orElseM = orElse(M);
  return (ma, onLeft) => pipe(
    ma,
    orElseM((e) => M.map(onLeft(e), (eb) => isLeft(eb) ? eb : left(e)))
  );
}
function orLeft(M) {
  return (onLeft) => (ma) => M.chain(
    ma,
    match(
      (e) => M.map(onLeft(e), left),
      (a) => M.of(right(a))
    )
  );
}
function swap2(F) {
  return (ma) => F.map(ma, swap);
}
function toUnion2(F) {
  return (fa) => F.map(fa, toUnion);
}
function getEitherM(M) {
  const _ap = ap2(M);
  const _map = map2(M);
  const _chain = chain(M);
  const _alt = alt(M);
  const _bimap = bimap2(M);
  const _mapLeft = mapLeft2(M);
  const _fold = matchE(M);
  const _getOrElse = getOrElse(M);
  const _orElse = orElse(M);
  return {
    map: (fa, f) => pipe(fa, _map(f)),
    ap: (fab, fa) => pipe(fab, _ap(fa)),
    of: right2(M),
    chain: (ma, f) => pipe(ma, _chain(f)),
    alt: (fa, that) => pipe(fa, _alt(that)),
    bimap: (fea, f, g) => pipe(fea, _bimap(f, g)),
    mapLeft: (fea, f) => pipe(fea, _mapLeft(f)),
    fold: (fa, onLeft, onRight) => pipe(fa, _fold(onLeft, onRight)),
    getOrElse: (fa, onLeft) => pipe(fa, _getOrElse(onLeft)),
    orElse: (fa, f) => pipe(fa, _orElse(f)),
    swap: swap2(M),
    rightM: rightF(M),
    leftM: leftF(M),
    left: left2(M)
  };
}

export {
  right2 as right,
  left2 as left,
  rightF,
  leftF,
  fromNullable2 as fromNullable,
  fromNullableK,
  chainNullableK,
  map2 as map,
  ap2 as ap,
  chain,
  flatMap,
  alt,
  bimap2 as bimap,
  mapBoth,
  mapLeft2 as mapLeft,
  mapError,
  altValidation,
  match2 as match,
  matchE,
  getOrElse,
  orElse,
  orElseFirst,
  tapError,
  orLeft,
  swap2 as swap,
  toUnion2 as toUnion,
  getEitherM,
  EitherT_exports
};
