import {
  Apply,
  Functor,
  Monad,
  Pointed,
  traverseReadonlyNonEmptyArrayWithIndex as traverseReadonlyNonEmptyArrayWithIndex2
} from "./chunk-DHOW4GBY.mjs";
import {
  ask,
  asks,
  fromReaderK,
  tapReader
} from "./chunk-ZUUTK32Y.mjs";
import {
  asksReaderW,
  local,
  map,
  of,
  traverseReadonlyNonEmptyArrayWithIndex
} from "./chunk-AHSGWEN4.mjs";
import {
  ap,
  flatMap,
  fromReader,
  map as map2,
  of as of2
} from "./chunk-T6BWR2PC.mjs";
import {
  fromIOK,
  tapIO
} from "./chunk-TUMMPMRG.mjs";
import {
  do_3 as do_
} from "./chunk-3AL4EMTU.mjs";
import {
  as,
  asUnit,
  bindTo,
  flap
} from "./chunk-WKK6AYQI.mjs";
import {
  apFirst,
  apS,
  apSecond
} from "./chunk-ZMCLCMOU.mjs";
import {
  bind,
  tap
} from "./chunk-MDNVYLGG.mjs";
import {
  emptyReadonlyArray,
  emptyRecord,
  flatMapIO,
  flatMapReader,
  isLeft,
  isNonEmpty
} from "./chunk-WPJQNT3M.mjs";
import {
  SK,
  dual,
  flow,
  identity,
  pipe
} from "./chunk-ZI34HWXZ.mjs";
import {
  __export
} from "./chunk-QK4PKNB3.mjs";

// src/ReaderIO.ts
var ReaderIO_exports = {};
__export(ReaderIO_exports, {
  ApT: () => ApT,
  Applicative: () => Applicative,
  Apply: () => Apply2,
  Chain: () => Chain,
  ChainRec: () => ChainRec,
  Do: () => Do,
  FromIO: () => FromIO,
  FromReader: () => FromReader,
  Functor: () => Functor2,
  Monad: () => Monad2,
  MonadIO: () => MonadIO,
  Pointed: () => Pointed2,
  URI: () => URI,
  ap: () => ap2,
  apFirst: () => apFirst2,
  apS: () => apS2,
  apSW: () => apSW,
  apSecond: () => apSecond2,
  apW: () => apW,
  as: () => as2,
  asUnit: () => asUnit2,
  ask: () => ask2,
  asks: () => asks2,
  asksReaderIO: () => asksReaderIO,
  asksReaderIOW: () => asksReaderIOW,
  bind: () => bind2,
  bindTo: () => bindTo2,
  bindW: () => bindW,
  chain: () => chain,
  chainFirst: () => chainFirst,
  chainFirstIOK: () => chainFirstIOK,
  chainFirstReaderK: () => chainFirstReaderK,
  chainFirstReaderKW: () => chainFirstReaderKW,
  chainFirstW: () => chainFirstW,
  chainIOK: () => chainIOK,
  chainReaderK: () => chainReaderK,
  chainReaderKW: () => chainReaderKW,
  chainRec: () => chainRec,
  chainW: () => chainW,
  do: () => do_2,
  flap: () => flap2,
  flatMap: () => flatMap2,
  flatMapIO: () => flatMapIO2,
  flatMapReader: () => flatMapReader2,
  flatten: () => flatten,
  flattenW: () => flattenW,
  fromIO: () => fromIO,
  fromIOK: () => fromIOK2,
  fromReader: () => fromReader2,
  fromReaderK: () => fromReaderK2,
  local: () => local2,
  map: () => map3,
  of: () => of3,
  sequenceArray: () => sequenceArray,
  tap: () => tap2,
  tapIO: () => tapIO2,
  tapReader: () => tapReader2,
  traverseArray: () => traverseArray,
  traverseArrayWithIndex: () => traverseArrayWithIndex,
  traverseReadonlyArrayWithIndex: () => traverseReadonlyArrayWithIndex,
  traverseReadonlyNonEmptyArrayWithIndex: () => traverseReadonlyNonEmptyArrayWithIndex3
});
var fromReader2 = /* @__PURE__ */ fromReader(Pointed);
var fromIO = of;
var local2 = local;
var asksReaderIOW = asksReaderW;
var asksReaderIO = asksReaderIOW;
var _map = (fa, f) => pipe(fa, map3(f));
var _ap = (fab, fa) => pipe(fab, ap2(fa));
var map3 = /* @__PURE__ */ map2(Functor);
var ap2 = /* @__PURE__ */ ap(Apply);
var apW = ap2;
var of3 = /* @__PURE__ */ of2(Pointed);
var flatMap2 = /* @__PURE__ */ dual(2, flatMap(Monad));
var flattenW = /* @__PURE__ */ flatMap2(identity);
var flatten = flattenW;
var URI = "ReaderIO";
var Functor2 = {
  URI,
  map: _map
};
var as2 = dual(2, as(Functor2));
var asUnit2 = asUnit(Functor2);
var flap2 = /* @__PURE__ */ flap(Functor2);
var Pointed2 = {
  URI,
  of: of3
};
var Apply2 = {
  URI,
  map: _map,
  ap: _ap
};
var apFirst2 = /* @__PURE__ */ apFirst(Apply2);
var apSecond2 = /* @__PURE__ */ apSecond(Apply2);
var Applicative = {
  URI,
  map: _map,
  ap: _ap,
  of: of3
};
var Chain = {
  URI,
  map: _map,
  ap: _ap,
  chain: flatMap2
};
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 Monad2 = {
  URI,
  map: _map,
  of: of3,
  ap: _ap,
  chain: flatMap2
};
var MonadIO = {
  URI,
  map: _map,
  of: of3,
  ap: _ap,
  chain: flatMap2,
  fromIO
};
var FromIO = {
  URI,
  fromIO
};
var FromReader = {
  URI,
  fromReader: fromReader2
};
var _FlatMap = {
  flatMap: flatMap2
};
var _FromIO = {
  fromIO: FromIO.fromIO
};
var _FromReader = {
  fromReader: fromReader2
};
var flatMapIO2 = flatMapIO(_FromIO, _FlatMap);
var flatMapReader2 = /* @__PURE__ */ flatMapReader(_FromReader, _FlatMap);
var tap2 = /* @__PURE__ */ dual(2, tap(Chain));
var tapIO2 = /* @__PURE__ */ dual(2, tapIO(FromIO, Chain));
var tapReader2 = /* @__PURE__ */ dual(2, tapReader(FromReader, Chain));
var fromIOK2 = /* @__PURE__ */ fromIOK(FromIO);
var chainIOK = flatMapIO2;
var chainFirstIOK = tapIO2;
var ask2 = /* @__PURE__ */ ask(FromReader);
var asks2 = /* @__PURE__ */ asks(FromReader);
var fromReaderK2 = /* @__PURE__ */ fromReaderK(FromReader);
var chainReaderK = flatMapReader2;
var chainReaderKW = flatMapReader2;
var chainFirstReaderK = tapReader2;
var chainFirstReaderKW = tapReader2;
var Do = /* @__PURE__ */ of3(emptyRecord);
var bindTo2 = /* @__PURE__ */ bindTo(Functor2);
var bind2 = /* @__PURE__ */ bind(Chain);
var bindW = bind2;
var apS2 = /* @__PURE__ */ apS(Apply2);
var apSW = apS2;
var ApT = /* @__PURE__ */ of3(emptyReadonlyArray);
var do_2 = do_(
  Pointed2,
  ChainRec
);
var traverseReadonlyNonEmptyArrayWithIndex3 = (f) => flow(traverseReadonlyNonEmptyArrayWithIndex(f), map(traverseReadonlyNonEmptyArrayWithIndex2(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;

export {
  fromReader2 as fromReader,
  fromIO,
  local2 as local,
  asksReaderIOW,
  asksReaderIO,
  map3 as map,
  ap2 as ap,
  apW,
  of3 as of,
  flatMap2 as flatMap,
  flattenW,
  flatten,
  URI,
  Functor2 as Functor,
  as2 as as,
  asUnit2 as asUnit,
  flap2 as flap,
  Pointed2 as Pointed,
  Apply2 as Apply,
  apFirst2 as apFirst,
  apSecond2 as apSecond,
  Applicative,
  Chain,
  chainRec,
  ChainRec,
  Monad2 as Monad,
  MonadIO,
  FromIO,
  FromReader,
  flatMapIO2 as flatMapIO,
  flatMapReader2 as flatMapReader,
  tap2 as tap,
  tapIO2 as tapIO,
  tapReader2 as tapReader,
  fromIOK2 as fromIOK,
  chainIOK,
  chainFirstIOK,
  ask2 as ask,
  asks2 as asks,
  fromReaderK2 as fromReaderK,
  chainReaderK,
  chainReaderKW,
  chainFirstReaderK,
  chainFirstReaderKW,
  Do,
  bindTo2 as bindTo,
  bind2 as bind,
  bindW,
  apS2 as apS,
  apSW,
  ApT,
  do_2 as do_,
  traverseReadonlyNonEmptyArrayWithIndex3 as traverseReadonlyNonEmptyArrayWithIndex,
  traverseReadonlyArrayWithIndex,
  traverseArrayWithIndex,
  traverseArray,
  sequenceArray,
  chain,
  chainW,
  chainFirst,
  chainFirstW,
  ReaderIO_exports
};
