import {
  ask,
  asks,
  fromReaderK,
  tapReader
} from "./chunk-ZUUTK32Y.mjs";
import {
  Apply,
  Functor as Functor2,
  Monad,
  Pointed,
  asksReaderW,
  local,
  map as map2,
  of,
  traverseReadonlyNonEmptyArrayWithIndex as traverseReadonlyNonEmptyArrayWithIndex2
} from "./chunk-AHSGWEN4.mjs";
import {
  alt,
  altValidation,
  ap as ap2,
  flatMap,
  getOrElse,
  left,
  leftF,
  map,
  mapBoth,
  mapError,
  match,
  matchE,
  orElse,
  orLeft,
  right as right2,
  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 {
  Functor,
  do_3 as do_,
  getApplicativeValidation,
  getCompactable,
  getFilterable,
  getSemigroup,
  isLeft,
  isRight,
  right,
  traverseReadonlyNonEmptyArrayWithIndex
} 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,
  flatMapNullable,
  flatMapOption,
  flatMapReader,
  isNonEmpty,
  liftNullable,
  liftOption
} from "./chunk-WPJQNT3M.mjs";
import {
  SK,
  dual,
  flow,
  identity,
  pipe
} from "./chunk-ZI34HWXZ.mjs";
import {
  __export
} from "./chunk-QK4PKNB3.mjs";

// src/ReaderEither.ts
var ReaderEither_exports = {};
__export(ReaderEither_exports, {
  Alt: () => Alt,
  ApT: () => ApT,
  Applicative: () => Applicative,
  Apply: () => Apply2,
  Bifunctor: () => Bifunctor,
  Chain: () => Chain,
  ChainRec: () => ChainRec,
  Do: () => Do,
  FromEither: () => FromEither,
  FromReader: () => FromReader,
  Functor: () => Functor3,
  Monad: () => Monad2,
  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,
  ask: () => ask2,
  asks: () => asks2,
  asksReaderEither: () => asksReaderEither,
  asksReaderEitherW: () => asksReaderEitherW,
  bimap: () => bimap,
  bind: () => bind2,
  bindTo: () => bindTo2,
  bindW: () => bindW,
  chain: () => chain,
  chainEitherK: () => chainEitherK,
  chainEitherKW: () => chainEitherKW,
  chainFirst: () => chainFirst,
  chainFirstEitherK: () => chainFirstEitherK,
  chainFirstEitherKW: () => chainFirstEitherKW,
  chainFirstReaderK: () => chainFirstReaderK,
  chainFirstReaderKW: () => chainFirstReaderKW,
  chainFirstW: () => chainFirstW,
  chainOptionK: () => chainOptionK2,
  chainOptionKW: () => chainOptionKW,
  chainReaderK: () => chainReaderK,
  chainReaderKW: () => chainReaderKW,
  chainRec: () => chainRec,
  chainW: () => chainW,
  do: () => do_2,
  filterOrElse: () => filterOrElse2,
  filterOrElseW: () => filterOrElseW,
  flap: () => flap2,
  flatMap: () => flatMap2,
  flatMapEither: () => flatMapEither2,
  flatMapNullable: () => flatMapNullable2,
  flatMapOption: () => flatMapOption2,
  flatMapReader: () => flatMapReader2,
  flatten: () => flatten,
  flattenW: () => flattenW,
  fold: () => fold,
  foldW: () => foldW,
  fromEither: () => fromEither,
  fromEitherK: () => fromEitherK2,
  fromOption: () => fromOption2,
  fromOptionK: () => fromOptionK2,
  fromPredicate: () => fromPredicate2,
  fromReader: () => fromReader,
  fromReaderK: () => fromReaderK2,
  getAltReaderValidation: () => getAltReaderValidation,
  getApplicativeReaderValidation: () => getApplicativeReaderValidation,
  getApplyMonoid: () => getApplyMonoid,
  getApplySemigroup: () => getApplySemigroup2,
  getCompactable: () => getCompactable2,
  getFilterable: () => getFilterable2,
  getOrElse: () => getOrElse2,
  getOrElseW: () => getOrElseW,
  getReaderValidation: () => getReaderValidation,
  getSemigroup: () => getSemigroup2,
  left: () => left2,
  leftReader: () => leftReader,
  let: () => let_2,
  liftNullable: () => liftNullable2,
  liftOption: () => liftOption2,
  local: () => local2,
  map: () => map3,
  mapBoth: () => mapBoth2,
  mapError: () => mapError2,
  mapLeft: () => mapLeft,
  match: () => match2,
  matchE: () => matchE2,
  matchEW: () => matchEW,
  matchW: () => matchW,
  of: () => of2,
  orElse: () => orElse2,
  orElseFirst: () => orElseFirst,
  orElseFirstW: () => orElseFirstW,
  orElseW: () => orElseW,
  orLeft: () => orLeft2,
  readerEither: () => readerEither,
  right: () => right3,
  rightReader: () => rightReader,
  sequenceArray: () => sequenceArray,
  swap: () => swap2,
  tap: () => tap2,
  tapEither: () => tapEither2,
  tapError: () => tapError2,
  tapReader: () => tapReader2,
  throwError: () => throwError,
  toUnion: () => toUnion2,
  traverseArray: () => traverseArray,
  traverseArrayWithIndex: () => traverseArrayWithIndex,
  traverseReadonlyArrayWithIndex: () => traverseReadonlyArrayWithIndex,
  traverseReadonlyNonEmptyArrayWithIndex: () => traverseReadonlyNonEmptyArrayWithIndex3
});
var left2 = /* @__PURE__ */ left(Pointed);
var right3 = /* @__PURE__ */ right2(Pointed);
var rightReader = /* @__PURE__ */ rightF(Functor2);
var leftReader = /* @__PURE__ */ leftF(
  Functor2
);
var fromEither = of;
var fromReader = rightReader;
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 toUnion2 = /* @__PURE__ */ toUnion(Functor2);
var local2 = local;
var asksReaderEitherW = asksReaderW;
var asksReaderEither = asksReaderEitherW;
var orElse2 = /* @__PURE__ */ orElse(Monad);
var orElseW = orElse2;
var tapError2 = /* @__PURE__ */ dual(2, tapError(Monad));
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 _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 = right3;
var flatMap2 = /* @__PURE__ */ dual(2, flatMap(Monad));
var flattenW = /* @__PURE__ */ flatMap2(identity);
var flatten = flattenW;
var alt2 = /* @__PURE__ */ alt(Monad);
var altW = alt2;
var throwError = left2;
var URI = "ReaderEither";
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))
  };
}
function getApplicativeReaderValidation(S) {
  const ap4 = ap(Apply, getApplicativeValidation(S));
  return {
    URI,
    _E: void 0,
    map: _map,
    ap: (fab, fa) => pipe(fab, ap4(fa)),
    of: of2
  };
}
function getAltReaderValidation(S) {
  const alt3 = altValidation(Monad, S);
  return {
    URI,
    _E: void 0,
    map: _map,
    alt: (fa, that) => pipe(fa, alt3(that))
  };
}
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 Apply2 = {
  URI,
  map: _map,
  ap: _ap
};
var apFirst2 = /* @__PURE__ */ apFirst(Apply2);
var apFirstW = apFirst2;
var apSecond2 = /* @__PURE__ */ apSecond(Apply2);
var apSecondW = apSecond2;
var Applicative = {
  URI,
  map: _map,
  ap: _ap,
  of: of2
};
var Chain = {
  URI,
  map: _map,
  ap: _ap,
  chain: flatMap2
};
var chainRec = (a, f) => (r) => {
  let current = f(a)(r);
  while (true) {
    if (isRight(current) && isLeft(current.right)) {
      current = f(current.right.left)(r);
      continue;
    }
    if (isLeft(current)) {
      return current;
    }
    if (isRight(current.right)) {
      return right(current.right.right);
    }
  }
};
var ChainRec = {
  ...Chain,
  chainRec
};
var Monad2 = {
  URI,
  map: _map,
  ap: _ap,
  of: of2,
  chain: flatMap2
};
var FromEither = {
  URI,
  fromEither
};
var FromReader = {
  URI,
  fromReader
};
var tap2 = /* @__PURE__ */ dual(2, tap(Chain));
var tapEither2 = /* @__PURE__ */ dual(2, tapEither(FromEither, Chain));
var tapReader2 = /* @__PURE__ */ dual(2, tapReader(FromReader, Chain));
var Bifunctor = {
  URI,
  bimap: mapBoth2,
  mapLeft: mapError2
};
var Alt = {
  URI,
  map: _map,
  alt: _alt
};
var ask2 = /* @__PURE__ */ ask(FromReader);
var asks2 = /* @__PURE__ */ asks(FromReader);
var fromReaderK2 = /* @__PURE__ */ fromReaderK(FromReader);
var chainFirstReaderK = tapReader2;
var chainFirstReaderKW = tapReader2;
var MonadThrow = {
  URI,
  map: _map,
  ap: _ap,
  of: of2,
  chain: flatMap2,
  throwError
};
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 _FromReader = {
  fromReader: FromReader.fromReader
};
var liftNullable2 = /* @__PURE__ */ liftNullable(_FromEither);
var liftOption2 = /* @__PURE__ */ liftOption(_FromEither);
var _FlatMap = {
  flatMap: flatMap2
};
var flatMapNullable2 = /* @__PURE__ */ flatMapNullable(_FromEither, _FlatMap);
var flatMapOption2 = /* @__PURE__ */ flatMapOption(_FromEither, _FlatMap);
var flatMapEither2 = /* @__PURE__ */ flatMapEither(_FromEither, _FlatMap);
var flatMapReader2 = /* @__PURE__ */ flatMapReader(_FromReader, _FlatMap);
var chainEitherK = flatMapEither2;
var chainEitherKW = flatMapEither2;
var chainFirstEitherK = tapEither2;
var chainFirstEitherKW = tapEither2;
var chainReaderK = flatMapReader2;
var chainReaderKW = flatMapReader2;
var fromPredicate2 = /* @__PURE__ */ fromPredicate(FromEither);
var filterOrElse2 = /* @__PURE__ */ filterOrElse(FromEither, Chain);
var filterOrElseW = filterOrElse2;
var fromEitherK2 = /* @__PURE__ */ fromEitherK(FromEither);
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(Apply2);
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 traverseArrayWithIndex = traverseReadonlyArrayWithIndex;
var traverseArray = (f) => traverseReadonlyArrayWithIndex((_, a) => f(a));
var sequenceArray = /* @__PURE__ */ traverseArray(identity);
var chain = flatMap2;
var chainW = flatMap2;
var chainFirst = tap2;
var chainFirstW = tap2;
var orElseFirst = tapError2;
var orElseFirstW = tapError2;
var readerEither = {
  URI,
  bimap: mapBoth2,
  mapLeft: mapError2,
  map: _map,
  of: of2,
  ap: _ap,
  chain: flatMap2,
  alt: _alt,
  throwError: left2
};
var getApplySemigroup2 = /* @__PURE__ */ getApplySemigroup(Apply2);
var getApplyMonoid = /* @__PURE__ */ getApplicativeMonoid(Applicative);
var getSemigroup2 = (S) => getApplySemigroup(Apply)(getSemigroup(S));
function getReaderValidation(SE) {
  const applicativeReaderValidation = getApplicativeReaderValidation(SE);
  const altReaderValidation = getAltReaderValidation(SE);
  return {
    URI,
    _E: void 0,
    map: _map,
    ap: applicativeReaderValidation.ap,
    of: of2,
    chain: flatMap2,
    bimap: mapBoth2,
    mapLeft: mapError2,
    alt: altReaderValidation.alt,
    throwError
  };
}

export {
  left2 as left,
  right3 as right,
  rightReader,
  leftReader,
  fromEither,
  fromReader,
  match2 as match,
  matchW,
  matchE2 as matchE,
  fold,
  matchEW,
  foldW,
  getOrElse2 as getOrElse,
  getOrElseW,
  toUnion2 as toUnion,
  local2 as local,
  asksReaderEitherW,
  asksReaderEither,
  orElse2 as orElse,
  orElseW,
  tapError2 as tapError,
  orLeft2 as orLeft,
  swap2 as swap,
  map3 as map,
  mapBoth2 as mapBoth,
  bimap,
  mapError2 as mapError,
  mapLeft,
  ap3 as ap,
  apW,
  of2 as of,
  flatMap2 as flatMap,
  flattenW,
  flatten,
  alt2 as alt,
  altW,
  throwError,
  URI,
  getCompactable2 as getCompactable,
  getFilterable2 as getFilterable,
  getApplicativeReaderValidation,
  getAltReaderValidation,
  Functor3 as Functor,
  as2 as as,
  asUnit2 as asUnit,
  flap2 as flap,
  Pointed2 as Pointed,
  Apply2 as Apply,
  apFirst2 as apFirst,
  apFirstW,
  apSecond2 as apSecond,
  apSecondW,
  Applicative,
  Chain,
  chainRec,
  ChainRec,
  Monad2 as Monad,
  FromEither,
  FromReader,
  tap2 as tap,
  tapEither2 as tapEither,
  tapReader2 as tapReader,
  Bifunctor,
  Alt,
  ask2 as ask,
  asks2 as asks,
  fromReaderK2 as fromReaderK,
  chainFirstReaderK,
  chainFirstReaderKW,
  MonadThrow,
  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,
  flatMapReader2 as flatMapReader,
  chainEitherK,
  chainEitherKW,
  chainFirstEitherK,
  chainFirstEitherKW,
  chainReaderK,
  chainReaderKW,
  fromPredicate2 as fromPredicate,
  filterOrElse2 as filterOrElse,
  filterOrElseW,
  fromEitherK2 as fromEitherK,
  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,
  traverseArrayWithIndex,
  traverseArray,
  sequenceArray,
  chain,
  chainW,
  chainFirst,
  chainFirstW,
  orElseFirst,
  orElseFirstW,
  readerEither,
  getApplySemigroup2 as getApplySemigroup,
  getApplyMonoid,
  getSemigroup2 as getSemigroup,
  getReaderValidation,
  ReaderEither_exports
};
