import {
  Apply,
  Functor as Functor2,
  Monad,
  Pointed,
  flatMap as flatMap2,
  map as map2,
  of,
  traverseReadonlyNonEmptyArrayWithIndex as traverseReadonlyNonEmptyArrayWithIndex2
} from "./chunk-DHOW4GBY.mjs";
import {
  alt,
  altValidation,
  ap as ap2,
  flatMap,
  getOrElse,
  left,
  leftF,
  map,
  mapBoth,
  mapError,
  match,
  matchE,
  orElse,
  orLeft,
  right as right3,
  rightF,
  swap,
  tapError,
  toUnion
} from "./chunk-HZOOJZWX.mjs";
import {
  filter,
  filterMap,
  partition,
  partitionMap
} from "./chunk-NNZUJMKX.mjs";
import {
  compact,
  separate
} from "./chunk-47WJXJHK.mjs";
import {
  fromIOK,
  tapIO
} from "./chunk-TUMMPMRG.mjs";
import {
  Functor,
  do_3 as do_,
  getApplicativeValidation,
  getCompactable,
  getFilterable,
  getSemigroup,
  isLeft as isLeft2,
  isRight,
  right as right2,
  traverseReadonlyNonEmptyArrayWithIndex,
  tryCatch
} from "./chunk-3AL4EMTU.mjs";
import {
  getApplicativeMonoid
} from "./chunk-LN4UQKIO.mjs";
import {
  chainOptionK,
  filterOrElse,
  fromEitherK,
  fromOption,
  fromOptionK,
  fromPredicate,
  tapEither
} from "./chunk-SEYMJLV6.mjs";
import {
  as,
  asUnit,
  bindTo,
  flap,
  let_
} from "./chunk-WKK6AYQI.mjs";
import {
  ap,
  apFirst,
  apS,
  apSecond,
  getApplySemigroup
} from "./chunk-ZMCLCMOU.mjs";
import {
  bind,
  tap
} from "./chunk-MDNVYLGG.mjs";
import {
  emptyReadonlyArray,
  emptyRecord,
  flatMapEither,
  flatMapIO,
  flatMapNullable,
  flatMapOption,
  head,
  isLeft,
  isNonEmpty,
  liftNullable,
  liftOption,
  right
} from "./chunk-WPJQNT3M.mjs";
import {
  SK,
  dual,
  flow,
  identity,
  pipe
} from "./chunk-ZI34HWXZ.mjs";
import {
  __export
} from "./chunk-QK4PKNB3.mjs";

// src/IOEither.ts
var IOEither_exports = {};
__export(IOEither_exports, {
  Alt: () => Alt,
  ApT: () => ApT,
  Applicative: () => Applicative,
  ApplicativePar: () => ApplicativePar,
  ApplicativeSeq: () => ApplicativeSeq,
  ApplyPar: () => ApplyPar,
  Bifunctor: () => Bifunctor,
  Chain: () => Chain,
  ChainRec: () => ChainRec,
  Do: () => Do,
  FromEither: () => FromEither,
  FromIO: () => FromIO,
  Functor: () => Functor3,
  Monad: () => Monad2,
  MonadIO: () => MonadIO,
  MonadThrow: () => MonadThrow,
  Pointed: () => Pointed2,
  URI: () => URI,
  alt: () => alt2,
  altW: () => altW,
  ap: () => ap3,
  apFirst: () => apFirst2,
  apFirstW: () => apFirstW,
  apS: () => apS2,
  apSW: () => apSW,
  apSecond: () => apSecond2,
  apSecondW: () => apSecondW,
  apW: () => apW,
  as: () => as2,
  asUnit: () => asUnit2,
  bimap: () => bimap,
  bind: () => bind2,
  bindTo: () => bindTo2,
  bindW: () => bindW,
  bracket: () => bracket,
  bracketW: () => bracketW,
  chain: () => chain,
  chainEitherK: () => chainEitherK,
  chainEitherKW: () => chainEitherKW,
  chainFirst: () => chainFirst,
  chainFirstEitherK: () => chainFirstEitherK,
  chainFirstEitherKW: () => chainFirstEitherKW,
  chainFirstIOK: () => chainFirstIOK,
  chainFirstW: () => chainFirstW,
  chainIOK: () => chainIOK,
  chainOptionK: () => chainOptionK2,
  chainOptionKW: () => chainOptionKW,
  chainRec: () => chainRec,
  chainW: () => chainW,
  do: () => do_2,
  filterOrElse: () => filterOrElse2,
  filterOrElseW: () => filterOrElseW,
  flap: () => flap2,
  flatMap: () => flatMap3,
  flatMapEither: () => flatMapEither2,
  flatMapIO: () => flatMapIO2,
  flatMapNullable: () => flatMapNullable2,
  flatMapOption: () => flatMapOption2,
  flatten: () => flatten,
  flattenW: () => flattenW,
  fold: () => fold,
  foldW: () => foldW,
  fromEither: () => fromEither,
  fromEitherK: () => fromEitherK2,
  fromIO: () => fromIO,
  fromIOK: () => fromIOK2,
  fromOption: () => fromOption2,
  fromOptionK: () => fromOptionK2,
  fromPredicate: () => fromPredicate2,
  getAltIOValidation: () => getAltIOValidation,
  getApplicativeIOValidation: () => getApplicativeIOValidation,
  getApplyMonoid: () => getApplyMonoid,
  getApplySemigroup: () => getApplySemigroup2,
  getCompactable: () => getCompactable2,
  getFilterable: () => getFilterable2,
  getIOValidation: () => getIOValidation,
  getOrElse: () => getOrElse2,
  getOrElseW: () => getOrElseW,
  getSemigroup: () => getSemigroup2,
  ioEither: () => ioEither,
  left: () => left2,
  leftIO: () => leftIO,
  let: () => let_2,
  liftNullable: () => liftNullable2,
  liftOption: () => liftOption2,
  map: () => map3,
  mapBoth: () => mapBoth2,
  mapError: () => mapError2,
  mapLeft: () => mapLeft,
  match: () => match2,
  matchE: () => matchE2,
  matchEW: () => matchEW,
  matchW: () => matchW,
  of: () => of2,
  orElse: () => orElse2,
  orElseFirst: () => orElseFirst,
  orElseFirstIOK: () => orElseFirstIOK,
  orElseFirstW: () => orElseFirstW,
  orElseW: () => orElseW,
  orLeft: () => orLeft2,
  right: () => right4,
  rightIO: () => rightIO,
  sequenceArray: () => sequenceArray,
  sequenceSeqArray: () => sequenceSeqArray,
  swap: () => swap2,
  tap: () => tap2,
  tapEither: () => tapEither2,
  tapError: () => tapError2,
  tapIO: () => tapIO2,
  throwError: () => throwError,
  toUnion: () => toUnion2,
  traverseArray: () => traverseArray,
  traverseArrayWithIndex: () => traverseArrayWithIndex,
  traverseReadonlyArrayWithIndex: () => traverseReadonlyArrayWithIndex,
  traverseReadonlyArrayWithIndexSeq: () => traverseReadonlyArrayWithIndexSeq,
  traverseReadonlyNonEmptyArrayWithIndex: () => traverseReadonlyNonEmptyArrayWithIndex3,
  traverseReadonlyNonEmptyArrayWithIndexSeq: () => traverseReadonlyNonEmptyArrayWithIndexSeq,
  traverseSeqArray: () => traverseSeqArray,
  traverseSeqArrayWithIndex: () => traverseSeqArrayWithIndex,
  tryCatch: () => tryCatch2,
  tryCatchK: () => tryCatchK
});
var left2 = /* @__PURE__ */ left(Pointed);
var right4 = /* @__PURE__ */ right3(Pointed);
var rightIO = /* @__PURE__ */ rightF(Functor2);
var leftIO = /* @__PURE__ */ leftF(Functor2);
var fromEither = of;
var fromIO = rightIO;
var match2 = /* @__PURE__ */ match(Functor2);
var matchW = match2;
var matchE2 = /* @__PURE__ */ matchE(Monad);
var fold = matchE2;
var matchEW = matchE2;
var foldW = matchEW;
var getOrElse2 = /* @__PURE__ */ getOrElse(
  Monad
);
var getOrElseW = getOrElse2;
var tryCatch2 = (f, onThrow) => () => tryCatch(f, onThrow);
var tryCatchK = (f, onThrow) => (...a) => tryCatch2(() => f(...a), onThrow);
var toUnion2 = /* @__PURE__ */ toUnion(Functor2);
var orElse2 = /* @__PURE__ */ orElse(Monad);
var orElseW = orElse2;
var tapError2 = /* @__PURE__ */ dual(2, tapError(Monad));
var orElseFirstIOK = (onLeft) => tapError2(fromIOK2(onLeft));
var orLeft2 = /* @__PURE__ */ orLeft(Monad);
var swap2 = /* @__PURE__ */ swap(Functor2);
var _map = (fa, f) => pipe(fa, map3(f));
var _ap = (fab, fa) => pipe(fab, ap3(fa));
var _apSeq = (fab, fa) => flatMap3(fab, (f) => pipe(fa, map3(f)));
var _alt = (fa, that) => pipe(fa, alt2(that));
var map3 = /* @__PURE__ */ map(Functor2);
var mapBoth2 = /* @__PURE__ */ dual(3, mapBoth(Functor2));
var bimap = mapBoth2;
var mapError2 = /* @__PURE__ */ dual(2, mapError(Functor2));
var mapLeft = mapError2;
var ap3 = /* @__PURE__ */ ap2(Apply);
var apW = ap3;
var of2 = right4;
var flatMap3 = /* @__PURE__ */ dual(2, flatMap(Monad));
var flattenW = /* @__PURE__ */ flatMap3(identity);
var flatten = flattenW;
var alt2 = /* @__PURE__ */ alt(Monad);
var altW = alt2;
var throwError = left2;
var URI = "IOEither";
function getApplicativeIOValidation(S) {
  const ap4 = ap(Apply, getApplicativeValidation(S));
  return {
    URI,
    _E: void 0,
    map: _map,
    ap: (fab, fa) => pipe(fab, ap4(fa)),
    of: of2
  };
}
function getAltIOValidation(S) {
  const alt3 = altValidation(Monad, S);
  return {
    URI,
    _E: void 0,
    map: _map,
    alt: (fa, that) => pipe(fa, alt3(that))
  };
}
var getCompactable2 = (M) => {
  const C = getCompactable(M);
  return {
    URI,
    _E: void 0,
    compact: compact(Functor2, C),
    separate: separate(Functor2, C, Functor)
  };
};
function getFilterable2(M) {
  const F = getFilterable(M);
  const C = getCompactable2(M);
  const filter2 = filter(Functor2, F);
  const filterMap2 = filterMap(Functor2, F);
  const partition2 = partition(Functor2, F);
  const partitionMap2 = partitionMap(Functor2, F);
  return {
    URI,
    _E: void 0,
    map: _map,
    compact: C.compact,
    separate: C.separate,
    filter: (fa, predicate) => pipe(fa, filter2(predicate)),
    filterMap: (fa, f) => pipe(fa, filterMap2(f)),
    partition: (fa, predicate) => pipe(fa, partition2(predicate)),
    partitionMap: (fa, f) => pipe(fa, partitionMap2(f))
  };
}
var Functor3 = {
  URI,
  map: _map
};
var as2 = dual(2, as(Functor3));
var asUnit2 = asUnit(Functor3);
var flap2 = /* @__PURE__ */ flap(Functor3);
var Pointed2 = {
  URI,
  of: of2
};
var Bifunctor = {
  URI,
  bimap: mapBoth2,
  mapLeft: mapError2
};
var ApplyPar = {
  URI,
  map: _map,
  ap: _ap
};
var apFirst2 = /* @__PURE__ */ apFirst(ApplyPar);
var apFirstW = apFirst2;
var apSecond2 = /* @__PURE__ */ apSecond(ApplyPar);
var apSecondW = apSecond2;
var ApplicativePar = {
  URI,
  map: _map,
  ap: _ap,
  of: of2
};
var ApplicativeSeq = {
  URI,
  map: _map,
  ap: _apSeq,
  of: of2
};
var Chain = {
  URI,
  map: _map,
  ap: _ap,
  chain: flatMap3
};
var chainRec = (a, f) => () => {
  let current = f(a)();
  while (true) {
    if (isRight(current) && isLeft2(current.right)) {
      current = f(current.right.left)();
      continue;
    }
    if (isLeft2(current)) {
      return current;
    }
    if (isRight(current.right)) {
      return right2(current.right.right);
    }
  }
};
var ChainRec = {
  ...Chain,
  chainRec
};
var Monad2 = {
  URI,
  map: _map,
  ap: _ap,
  of: of2,
  chain: flatMap3
};
var FromEither = {
  URI,
  fromEither
};
var FromIO = {
  URI,
  fromIO
};
var tap2 = /* @__PURE__ */ dual(2, tap(Chain));
var tapEither2 = /* @__PURE__ */ dual(2, tapEither(FromEither, Chain));
var tapIO2 = /* @__PURE__ */ dual(2, tapIO(FromIO, Chain));
var Alt = {
  URI,
  map: _map,
  alt: _alt
};
var MonadIO = {
  URI,
  map: _map,
  ap: _ap,
  of: of2,
  chain: flatMap3,
  fromIO
};
var MonadThrow = {
  URI,
  map: _map,
  ap: _ap,
  of: of2,
  chain: flatMap3,
  throwError
};
var fromIOK2 = /* @__PURE__ */ fromIOK(FromIO);
var chainFirstIOK = tapIO2;
var fromOption2 = /* @__PURE__ */ fromOption(FromEither);
var fromOptionK2 = /* @__PURE__ */ fromOptionK(FromEither);
var chainOptionK2 = /* @__PURE__ */ chainOptionK(
  FromEither,
  Chain
);
var chainOptionKW = chainOptionK2;
var _FromEither = {
  fromEither: FromEither.fromEither
};
var _FromIO = {
  fromIO
};
var liftNullable2 = /* @__PURE__ */ liftNullable(_FromEither);
var liftOption2 = /* @__PURE__ */ liftOption(_FromEither);
var _FlatMap = {
  flatMap: flatMap3
};
var flatMapNullable2 = /* @__PURE__ */ flatMapNullable(_FromEither, _FlatMap);
var flatMapOption2 = /* @__PURE__ */ flatMapOption(_FromEither, _FlatMap);
var flatMapEither2 = /* @__PURE__ */ flatMapEither(_FromEither, _FlatMap);
var flatMapIO2 = /* @__PURE__ */ flatMapIO(_FromIO, _FlatMap);
var chainIOK = flatMapIO2;
var chainEitherK = flatMapEither2;
var chainEitherKW = flatMapEither2;
var chainFirstEitherK = tapEither2;
var chainFirstEitherKW = tapEither2;
var fromPredicate2 = /* @__PURE__ */ fromPredicate(FromEither);
var filterOrElse2 = /* @__PURE__ */ filterOrElse(FromEither, Chain);
var filterOrElseW = filterOrElse2;
var fromEitherK2 = /* @__PURE__ */ fromEitherK(FromEither);
var bracket = (acquire, use, release) => bracketW(acquire, use, release);
var bracketW = (acquire, use, release) => flatMap3(acquire, (a) => flatMap2(use(a), (e) => flatMap3(release(a, e), () => of(e))));
var Do = /* @__PURE__ */ of2(emptyRecord);
var bindTo2 = /* @__PURE__ */ bindTo(Functor3);
var let_2 = /* @__PURE__ */ let_(Functor3);
var bind2 = /* @__PURE__ */ bind(Chain);
var bindW = bind2;
var apS2 = /* @__PURE__ */ apS(ApplyPar);
var apSW = apS2;
var ApT = /* @__PURE__ */ of2(emptyReadonlyArray);
var do_2 = do_(Pointed2, ChainRec);
var traverseReadonlyNonEmptyArrayWithIndex3 = (f) => flow(traverseReadonlyNonEmptyArrayWithIndex2(f), map2(traverseReadonlyNonEmptyArrayWithIndex(SK)));
var traverseReadonlyArrayWithIndex = (f) => {
  const g = traverseReadonlyNonEmptyArrayWithIndex3(f);
  return (as3) => isNonEmpty(as3) ? g(as3) : ApT;
};
var traverseReadonlyNonEmptyArrayWithIndexSeq = (f) => (as3) => () => {
  const e = f(0, head(as3))();
  if (isLeft(e)) {
    return e;
  }
  const out = [e.right];
  for (let i = 1; i < as3.length; i++) {
    const e2 = f(i, as3[i])();
    if (isLeft(e2)) {
      return e2;
    }
    out.push(e2.right);
  }
  return right(out);
};
var traverseReadonlyArrayWithIndexSeq = (f) => {
  const g = traverseReadonlyNonEmptyArrayWithIndexSeq(f);
  return (as3) => isNonEmpty(as3) ? g(as3) : ApT;
};
var traverseArrayWithIndex = traverseReadonlyArrayWithIndex;
var traverseArray = (f) => traverseReadonlyArrayWithIndex((_, a) => f(a));
var sequenceArray = /* @__PURE__ */ traverseArray(identity);
var traverseSeqArrayWithIndex = traverseReadonlyArrayWithIndexSeq;
var traverseSeqArray = (f) => traverseReadonlyArrayWithIndexSeq((_, a) => f(a));
var sequenceSeqArray = /* @__PURE__ */ traverseSeqArray(identity);
var Applicative = ApplicativePar;
var chain = flatMap3;
var chainW = flatMap3;
var chainFirst = tap2;
var chainFirstW = tap2;
var orElseFirst = tapError2;
var orElseFirstW = tapError2;
var ioEither = {
  URI,
  bimap: mapBoth2,
  mapLeft: mapError2,
  map: _map,
  of: of2,
  ap: _ap,
  chain: flatMap3,
  alt: _alt,
  fromIO,
  throwError
};
var getApplySemigroup2 = /* @__PURE__ */ getApplySemigroup(ApplyPar);
var getApplyMonoid = /* @__PURE__ */ getApplicativeMonoid(ApplicativePar);
var getSemigroup2 = (S) => getApplySemigroup(Apply)(getSemigroup(S));
function getIOValidation(SE) {
  const applicativeIOValidation = getApplicativeIOValidation(SE);
  const altIOValidation = getAltIOValidation(SE);
  return {
    URI,
    _E: void 0,
    map: _map,
    ap: applicativeIOValidation.ap,
    of: of2,
    chain: flatMap3,
    bimap: mapBoth2,
    mapLeft: mapError2,
    alt: altIOValidation.alt,
    fromIO,
    throwError
  };
}

export {
  left2 as left,
  right4 as right,
  rightIO,
  leftIO,
  fromEither,
  fromIO,
  match2 as match,
  matchW,
  matchE2 as matchE,
  fold,
  matchEW,
  foldW,
  getOrElse2 as getOrElse,
  getOrElseW,
  tryCatch2 as tryCatch,
  tryCatchK,
  toUnion2 as toUnion,
  orElse2 as orElse,
  orElseW,
  tapError2 as tapError,
  orElseFirstIOK,
  orLeft2 as orLeft,
  swap2 as swap,
  map3 as map,
  mapBoth2 as mapBoth,
  bimap,
  mapError2 as mapError,
  mapLeft,
  ap3 as ap,
  apW,
  of2 as of,
  flatMap3 as flatMap,
  flattenW,
  flatten,
  alt2 as alt,
  altW,
  throwError,
  URI,
  getApplicativeIOValidation,
  getAltIOValidation,
  getCompactable2 as getCompactable,
  getFilterable2 as getFilterable,
  Functor3 as Functor,
  as2 as as,
  asUnit2 as asUnit,
  flap2 as flap,
  Pointed2 as Pointed,
  Bifunctor,
  ApplyPar,
  apFirst2 as apFirst,
  apFirstW,
  apSecond2 as apSecond,
  apSecondW,
  ApplicativePar,
  ApplicativeSeq,
  Chain,
  chainRec,
  ChainRec,
  Monad2 as Monad,
  FromEither,
  FromIO,
  tap2 as tap,
  tapEither2 as tapEither,
  tapIO2 as tapIO,
  Alt,
  MonadIO,
  MonadThrow,
  fromIOK2 as fromIOK,
  chainFirstIOK,
  fromOption2 as fromOption,
  fromOptionK2 as fromOptionK,
  chainOptionK2 as chainOptionK,
  chainOptionKW,
  liftNullable2 as liftNullable,
  liftOption2 as liftOption,
  flatMapNullable2 as flatMapNullable,
  flatMapOption2 as flatMapOption,
  flatMapEither2 as flatMapEither,
  flatMapIO2 as flatMapIO,
  chainIOK,
  chainEitherK,
  chainEitherKW,
  chainFirstEitherK,
  chainFirstEitherKW,
  fromPredicate2 as fromPredicate,
  filterOrElse2 as filterOrElse,
  filterOrElseW,
  fromEitherK2 as fromEitherK,
  bracket,
  bracketW,
  Do,
  bindTo2 as bindTo,
  let_2 as let_,
  bind2 as bind,
  bindW,
  apS2 as apS,
  apSW,
  ApT,
  do_2 as do_,
  traverseReadonlyNonEmptyArrayWithIndex3 as traverseReadonlyNonEmptyArrayWithIndex,
  traverseReadonlyArrayWithIndex,
  traverseReadonlyNonEmptyArrayWithIndexSeq,
  traverseReadonlyArrayWithIndexSeq,
  traverseArrayWithIndex,
  traverseArray,
  sequenceArray,
  traverseSeqArrayWithIndex,
  traverseSeqArray,
  sequenceSeqArray,
  Applicative,
  chain,
  chainW,
  chainFirst,
  chainFirstW,
  orElseFirst,
  orElseFirstW,
  ioEither,
  getApplySemigroup2 as getApplySemigroup,
  getApplyMonoid,
  getSemigroup2 as getSemigroup,
  getIOValidation,
  IOEither_exports
};
