import {
  do_3 as do_,
  fold,
  isLeft,
  left as left2,
  right as right2
} from "./chunk-3AL4EMTU.mjs";
import {
  getApplicativeMonoid
} from "./chunk-LN4UQKIO.mjs";
import {
  bindTo,
  flap,
  let_
} from "./chunk-WKK6AYQI.mjs";
import {
  apFirst,
  apS,
  apSecond,
  getApplySemigroup
} from "./chunk-ZMCLCMOU.mjs";
import {
  bind,
  tap
} from "./chunk-MDNVYLGG.mjs";
import {
  emptyReadonlyArray,
  emptyRecord,
  head,
  isNonEmpty,
  left,
  right
} from "./chunk-WPJQNT3M.mjs";
import {
  constant,
  dual,
  flow,
  identity,
  pipe
} from "./chunk-ZI34HWXZ.mjs";
import {
  __export
} from "./chunk-QK4PKNB3.mjs";

// src/Reader.ts
var Reader_exports = {};
__export(Reader_exports, {
  ApT: () => ApT,
  Applicative: () => Applicative,
  Apply: () => Apply,
  Category: () => Category,
  Chain: () => Chain,
  ChainRec: () => ChainRec,
  Choice: () => Choice,
  Do: () => Do,
  Functor: () => Functor,
  Monad: () => Monad,
  Pointed: () => Pointed,
  Profunctor: () => Profunctor,
  Strong: () => Strong,
  URI: () => URI,
  ap: () => ap,
  apFirst: () => apFirst2,
  apFirstW: () => apFirstW,
  apS: () => apS2,
  apSW: () => apSW,
  apSecond: () => apSecond2,
  apSecondW: () => apSecondW,
  apW: () => apW,
  ask: () => ask,
  asks: () => asks,
  asksReader: () => asksReader,
  asksReaderW: () => asksReaderW,
  bind: () => bind2,
  bindTo: () => bindTo2,
  bindW: () => bindW,
  chain: () => chain,
  chainFirst: () => chainFirst,
  chainFirstW: () => chainFirstW,
  chainRec: () => chainRec,
  chainW: () => chainW,
  compose: () => compose,
  do: () => do_2,
  first: () => first,
  flap: () => flap2,
  flatMap: () => flatMap,
  flatten: () => flatten,
  flattenW: () => flattenW,
  getMonoid: () => getMonoid,
  getSemigroup: () => getSemigroup,
  id: () => id,
  left: () => left3,
  let: () => let_2,
  local: () => local,
  map: () => map,
  of: () => of,
  promap: () => promap,
  reader: () => reader,
  right: () => right3,
  second: () => second,
  sequenceArray: () => sequenceArray,
  tap: () => tap2,
  traverseArray: () => traverseArray,
  traverseArrayWithIndex: () => traverseArrayWithIndex,
  traverseReadonlyArrayWithIndex: () => traverseReadonlyArrayWithIndex,
  traverseReadonlyNonEmptyArrayWithIndex: () => traverseReadonlyNonEmptyArrayWithIndex
});
var ask = () => identity;
var asks = identity;
var local = (f) => (ma) => (r2) => ma(f(r2));
var asksReaderW = (f) => (r) => f(r)(r);
var asksReader = asksReaderW;
var _map = (fa, f) => pipe(fa, map(f));
var _ap = (fab, fa) => pipe(fab, ap(fa));
var _compose = (bc, ab) => pipe(bc, compose(ab));
var _promap = (fea, f, g) => pipe(fea, promap(f, g));
var map = (f) => (fa) => (r) => f(fa(r));
var apW = (fa) => (fab) => (r) => fab(r)(fa(r));
var ap = apW;
var of = constant;
var flatMap = /* @__PURE__ */ dual(
  2,
  (ma, f) => (r) => f(ma(r))(r)
);
var flattenW = /* @__PURE__ */ flatMap(identity);
var flatten = flattenW;
var compose = (ab) => (bc) => flow(ab, bc);
var promap = (f, g) => (fea) => (a) => g(fea(f(a)));
var id = () => identity;
var first = (pab) => ([a, c]) => [pab(a), c];
var second = (pbc) => ([a, b]) => [a, pbc(b)];
var left3 = (pab) => fold((a) => left(pab(a)), right2);
var right3 = (pbc) => fold(left2, (b) => right(pbc(b)));
var URI = "Reader";
var Functor = {
  URI,
  map: _map
};
var flap2 = /* @__PURE__ */ flap(Functor);
var Pointed = {
  URI,
  of
};
var Apply = {
  URI,
  map: _map,
  ap: _ap
};
var apFirst2 = /* @__PURE__ */ apFirst(Apply);
var apFirstW = apFirst2;
var apSecond2 = /* @__PURE__ */ apSecond(Apply);
var apSecondW = apSecond2;
var Applicative = {
  URI,
  map: _map,
  ap: _ap,
  of
};
var Chain = {
  URI,
  map: _map,
  ap: _ap,
  chain: flatMap
};
var chainRec = (a, f) => (r) => {
  let current = f(a)(r);
  while (isLeft(current)) {
    current = f(current.left)(r);
  }
  return current.right;
};
var ChainRec = {
  ...Chain,
  chainRec
};
var Monad = {
  URI,
  map: _map,
  of,
  ap: _ap,
  chain: flatMap
};
var tap2 = /* @__PURE__ */ dual(2, tap(Chain));
var Profunctor = {
  URI,
  map: _map,
  promap: _promap
};
var Category = {
  URI,
  compose: _compose,
  id
};
var Strong = {
  URI,
  map: _map,
  promap: _promap,
  first,
  second
};
var Choice = {
  URI,
  map: _map,
  promap: _promap,
  left: left3,
  right: right3
};
var bindTo2 = /* @__PURE__ */ bindTo(Functor);
var let_2 = /* @__PURE__ */ let_(Functor);
var bind2 = /* @__PURE__ */ bind(Chain);
var bindW = bind2;
var Do = /* @__PURE__ */ of(emptyRecord);
var apS2 = /* @__PURE__ */ apS(Apply);
var apSW = apS2;
var ApT = /* @__PURE__ */ of(emptyReadonlyArray);
var do_2 = do_(
  Pointed,
  ChainRec
);
var traverseReadonlyNonEmptyArrayWithIndex = (f) => (as) => (r) => {
  const out = [f(0, head(as))(r)];
  for (let i = 1; i < as.length; i++) {
    out.push(f(i, as[i])(r));
  }
  return out;
};
var traverseReadonlyArrayWithIndex = (f) => {
  const g = traverseReadonlyNonEmptyArrayWithIndex(f);
  return (as) => isNonEmpty(as) ? g(as) : ApT;
};
var traverseArrayWithIndex = traverseReadonlyArrayWithIndex;
var traverseArray = (f) => traverseReadonlyArrayWithIndex((_, a) => f(a));
var sequenceArray = /* @__PURE__ */ traverseArray(identity);
var chainW = flatMap;
var chain = flatMap;
var chainFirst = tap2;
var chainFirstW = tap2;
var reader = {
  URI,
  map: _map,
  of,
  ap: _ap,
  chain: flatMap,
  promap: _promap,
  compose: _compose,
  id,
  first,
  second,
  left: left3,
  right: right3
};
var getSemigroup = /* @__PURE__ */ getApplySemigroup(Apply);
var getMonoid = /* @__PURE__ */ getApplicativeMonoid(Applicative);

export {
  ask,
  asks,
  local,
  asksReaderW,
  asksReader,
  map,
  apW,
  ap,
  of,
  flatMap,
  flattenW,
  flatten,
  compose,
  promap,
  id,
  first,
  second,
  left3 as left,
  right3 as right,
  URI,
  Functor,
  flap2 as flap,
  Pointed,
  Apply,
  apFirst2 as apFirst,
  apFirstW,
  apSecond2 as apSecond,
  apSecondW,
  Applicative,
  Chain,
  chainRec,
  ChainRec,
  Monad,
  tap2 as tap,
  Profunctor,
  Category,
  Strong,
  Choice,
  bindTo2 as bindTo,
  let_2 as let_,
  bind2 as bind,
  bindW,
  Do,
  apS2 as apS,
  apSW,
  ApT,
  do_2 as do_,
  traverseReadonlyNonEmptyArrayWithIndex,
  traverseReadonlyArrayWithIndex,
  traverseArrayWithIndex,
  traverseArray,
  sequenceArray,
  chainW,
  chain,
  chainFirst,
  chainFirstW,
  reader,
  getSemigroup,
  getMonoid,
  Reader_exports
};
