import {
  chainStateK,
  fromStateK,
  get,
  gets,
  modify,
  put
} from "./chunk-HYFEMPZU.mjs";
import {
  Chain,
  Functor,
  Monad,
  Pointed,
  alt,
  altW,
  bimap,
  fromIOEither,
  fromReaderEither,
  fromTaskEither,
  left,
  leftIO,
  leftReader,
  leftTask,
  map as map2,
  mapLeft,
  right as right2,
  rightIO,
  rightReader,
  rightTask
} from "./chunk-VH2NFAWQ.mjs";
import {
  ask,
  asks,
  fromReaderK,
  tapReader
} from "./chunk-ZUUTK32Y.mjs";
import {
  local
} from "./chunk-AHSGWEN4.mjs";
import {
  ap,
  evaluate,
  execute,
  flatMap,
  fromF,
  fromState,
  map as map3,
  of
} from "./chunk-U3ES5KG5.mjs";
import {
  fromTaskK,
  tapTask
} from "./chunk-WCFCQNDF.mjs";
import {
  fromIOK,
  tapIO
} from "./chunk-TUMMPMRG.mjs";
import {
  do_3 as do_,
  isLeft as isLeft2,
  map,
  match,
  right
} from "./chunk-3AL4EMTU.mjs";
import {
  chainOptionK,
  filterOrElse,
  fromEitherK,
  fromOption,
  fromOptionK,
  fromPredicate,
  tapEither
} from "./chunk-SEYMJLV6.mjs";
import {
  as,
  asUnit,
  bindTo,
  flap,
  let_
} from "./chunk-WKK6AYQI.mjs";
import {
  apFirst,
  apS,
  apSecond
} from "./chunk-ZMCLCMOU.mjs";
import {
  bind,
  tap
} from "./chunk-MDNVYLGG.mjs";
import {
  emptyReadonlyArray,
  flatMapEither,
  flatMapIO,
  flatMapOption,
  flatMapReader,
  flatMapTask,
  head,
  isLeft,
  isNonEmpty,
  tail
} from "./chunk-WPJQNT3M.mjs";
import {
  dual,
  flow,
  identity,
  pipe
} from "./chunk-ZI34HWXZ.mjs";
import {
  __export
} from "./chunk-QK4PKNB3.mjs";

// src/StateReaderTaskEither.ts
var StateReaderTaskEither_exports = {};
__export(StateReaderTaskEither_exports, {
  Alt: () => Alt,
  Applicative: () => Applicative,
  Apply: () => Apply,
  Bifunctor: () => Bifunctor,
  Chain: () => Chain2,
  ChainRec: () => ChainRec,
  FromEither: () => FromEither,
  FromIO: () => FromIO,
  FromReader: () => FromReader,
  FromState: () => FromState,
  FromTask: () => FromTask,
  Functor: () => Functor2,
  Monad: () => Monad2,
  MonadIO: () => MonadIO,
  MonadTask: () => MonadTask,
  MonadThrow: () => MonadThrow,
  Pointed: () => Pointed2,
  URI: () => URI,
  alt: () => alt2,
  altW: () => altW2,
  ap: () => ap2,
  apFirst: () => apFirst2,
  apFirstW: () => apFirstW,
  apS: () => apS2,
  apSW: () => apSW,
  apSecond: () => apSecond2,
  apSecondW: () => apSecondW,
  apW: () => apW,
  as: () => as2,
  asUnit: () => asUnit2,
  ask: () => ask2,
  asks: () => asks2,
  asksStateReaderTaskEither: () => asksStateReaderTaskEither,
  asksStateReaderTaskEitherW: () => asksStateReaderTaskEitherW,
  bimap: () => bimap2,
  bind: () => bind2,
  bindTo: () => bindTo2,
  bindW: () => bindW,
  chain: () => chain,
  chainEitherK: () => chainEitherK,
  chainEitherKW: () => chainEitherKW,
  chainFirst: () => chainFirst,
  chainFirstEitherK: () => chainFirstEitherK,
  chainFirstEitherKW: () => chainFirstEitherKW,
  chainFirstIOK: () => chainFirstIOK,
  chainFirstReaderK: () => chainFirstReaderK,
  chainFirstReaderKW: () => chainFirstReaderKW,
  chainFirstTaskK: () => chainFirstTaskK,
  chainFirstW: () => chainFirstW,
  chainIOEitherK: () => chainIOEitherK,
  chainIOEitherKW: () => chainIOEitherKW,
  chainIOK: () => chainIOK,
  chainOptionK: () => chainOptionK2,
  chainOptionKW: () => chainOptionKW,
  chainReaderK: () => chainReaderK,
  chainReaderKW: () => chainReaderKW,
  chainReaderTaskEitherK: () => chainReaderTaskEitherK,
  chainReaderTaskEitherKW: () => chainReaderTaskEitherKW,
  chainRec: () => chainRec,
  chainStateK: () => chainStateK2,
  chainTaskEitherK: () => chainTaskEitherK,
  chainTaskEitherKW: () => chainTaskEitherKW,
  chainTaskK: () => chainTaskK,
  chainW: () => chainW,
  do: () => do_2,
  evalState: () => evalState,
  evaluate: () => evaluate2,
  execState: () => execState,
  execute: () => execute2,
  filterOrElse: () => filterOrElse2,
  filterOrElseW: () => filterOrElseW,
  flap: () => flap2,
  flatMap: () => flatMap2,
  flatMapEither: () => flatMapEither2,
  flatMapIO: () => flatMapIO2,
  flatMapIOEither: () => flatMapIOEither,
  flatMapOption: () => flatMapOption2,
  flatMapReader: () => flatMapReader2,
  flatMapReaderTaskEither: () => flatMapReaderTaskEither,
  flatMapState: () => flatMapState,
  flatMapTask: () => flatMapTask2,
  flatMapTaskEither: () => flatMapTaskEither,
  flatten: () => flatten,
  flattenW: () => flattenW,
  fromEither: () => fromEither,
  fromEitherK: () => fromEitherK2,
  fromIO: () => fromIO,
  fromIOEither: () => fromIOEither2,
  fromIOEitherK: () => fromIOEitherK,
  fromIOK: () => fromIOK2,
  fromOption: () => fromOption2,
  fromOptionK: () => fromOptionK2,
  fromPredicate: () => fromPredicate2,
  fromReader: () => fromReader,
  fromReaderEither: () => fromReaderEither2,
  fromReaderK: () => fromReaderK2,
  fromReaderTaskEither: () => fromReaderTaskEither,
  fromReaderTaskEitherK: () => fromReaderTaskEitherK,
  fromState: () => fromState2,
  fromStateK: () => fromStateK2,
  fromTask: () => fromTask,
  fromTaskEither: () => fromTaskEither2,
  fromTaskEitherK: () => fromTaskEitherK,
  fromTaskK: () => fromTaskK2,
  get: () => get2,
  gets: () => gets2,
  left: () => left2,
  leftIO: () => leftIO2,
  leftReader: () => leftReader2,
  leftState: () => leftState,
  leftTask: () => leftTask2,
  let: () => let_2,
  local: () => local2,
  map: () => map4,
  mapLeft: () => mapLeft2,
  modify: () => modify2,
  of: () => of2,
  put: () => put2,
  right: () => right3,
  rightIO: () => rightIO2,
  rightReader: () => rightReader2,
  rightState: () => rightState,
  rightTask: () => rightTask2,
  run: () => run,
  sequenceArray: () => sequenceArray,
  stateReaderTaskEither: () => stateReaderTaskEither,
  stateReaderTaskEitherSeq: () => stateReaderTaskEitherSeq,
  tap: () => tap2,
  tapEither: () => tapEither2,
  tapIO: () => tapIO2,
  tapReader: () => tapReader2,
  tapTask: () => tapTask2,
  throwError: () => throwError,
  traverseArray: () => traverseArray,
  traverseArrayWithIndex: () => traverseArrayWithIndex,
  traverseReadonlyArrayWithIndex: () => traverseReadonlyArrayWithIndex,
  traverseReadonlyNonEmptyArrayWithIndex: () => traverseReadonlyNonEmptyArrayWithIndex
});
var left2 = (e) => () => left(e);
var right3 = /* @__PURE__ */ of(
  Pointed
);
function rightTask2(ma) {
  return fromReaderTaskEither(rightTask(ma));
}
function leftTask2(me) {
  return fromReaderTaskEither(leftTask(me));
}
function rightReader2(ma) {
  return fromReaderTaskEither(rightReader(ma));
}
function leftReader2(me) {
  return fromReaderTaskEither(leftReader(me));
}
function rightIO2(ma) {
  return fromReaderTaskEither(rightIO(ma));
}
function leftIO2(me) {
  return fromReaderTaskEither(leftIO(me));
}
var rightState = (sa) => flow(sa, right2);
var leftState = (me) => (s) => left(me(s)[0]);
var fromEither = /* @__PURE__ */ match((e) => left2(e), right3);
var fromReader = rightReader2;
var fromIO = rightIO2;
var fromTask = rightTask2;
var fromState2 = /* @__PURE__ */ fromState(Pointed);
var fromTaskEither2 = (ma) => fromReaderTaskEither(fromTaskEither(ma));
var fromIOEither2 = (ma) => fromReaderTaskEither(fromIOEither(ma));
var fromReaderEither2 = (ma) => fromReaderTaskEither(fromReaderEither(ma));
var fromReaderTaskEither = /* @__PURE__ */ fromF(Functor);
var local2 = (f) => (ma) => flow(ma, local(f));
var asksStateReaderTaskEitherW = (f) => (s) => (r) => f(r)(s)(r);
var asksStateReaderTaskEither = asksStateReaderTaskEitherW;
var fromIOEitherK = (f) => (...a) => fromIOEither2(f(...a));
var fromTaskEitherK = (f) => (...a) => fromTaskEither2(f(...a));
var fromReaderTaskEitherK = (f) => (...a) => fromReaderTaskEither(f(...a));
var chainReaderTaskEitherKW = (f) => (ma) => flatMap2(ma, fromReaderTaskEitherK(f));
var chainReaderTaskEitherK = chainReaderTaskEitherKW;
var _map = (fa, f) => pipe(fa, map4(f));
var _ap = (fab, fa) => pipe(fab, ap2(fa));
var _alt = (fa, that) => (s) => pipe(
  fa(s),
  alt(() => that()(s))
);
var _bimap = (fea, f, g) => (s) => pipe(
  fea(s),
  bimap(f, ([a, s2]) => [g(a), s2])
);
var _mapLeft = (fea, f) => (s) => pipe(fea(s), mapLeft(f));
var map4 = /* @__PURE__ */ map3(
  Functor
);
var bimap2 = (f, g) => (fa) => _bimap(fa, f, g);
var mapLeft2 = (f) => (fa) => _mapLeft(fa, f);
var ap2 = /* @__PURE__ */ ap(
  Chain
);
var apW = ap2;
var of2 = right3;
var _FromIO = {
  fromIO
};
var _FromTask = {
  fromTask
};
var _FromReader = {
  fromReader
};
var _FromEither = {
  fromEither
};
var flatMap2 = /* @__PURE__ */ dual(2, flatMap(Monad));
var _FlatMap = {
  flatMap: flatMap2
};
var flatMapTaskEither = /* @__PURE__ */ dual(
  2,
  (self, f) => flatMap2(self, (a) => fromTaskEitherK(f)(a))
);
var flatMapIO2 = /* @__PURE__ */ flatMapIO(_FromIO, _FlatMap);
var flatMapTask2 = /* @__PURE__ */ flatMapTask(_FromTask, _FlatMap);
var flatMapReader2 = /* @__PURE__ */ flatMapReader(_FromReader, _FlatMap);
var flatMapIOEither = /* @__PURE__ */ dual(
  2,
  (self, f) => flatMap2(self, (a) => fromIOEitherK(f)(a))
);
var flatMapEither2 = /* @__PURE__ */ dual(2, flatMapEither(_FromEither, _FlatMap));
var flatMapOption2 = /* @__PURE__ */ flatMapOption(_FromEither, _FlatMap);
var flatMapReaderTaskEither = /* @__PURE__ */ dual(
  2,
  (self, f) => flatMap2(self, (a) => fromReaderTaskEitherK(f)(a))
);
var flatMapState = /* @__PURE__ */ dual(
  2,
  (self, f) => flatMap2(self, fromStateK2(f))
);
var flattenW = /* @__PURE__ */ flatMap2(identity);
var flatten = flattenW;
var altW2 = (that) => (fa) => (r) => pipe(
  fa(r),
  altW(() => that()(r))
);
var alt2 = altW2;
var throwError = left2;
var URI = "StateReaderTaskEither";
var Functor2 = {
  URI,
  map: _map
};
var as2 = dual(2, as(Functor2));
var asUnit2 = asUnit(Functor2);
var flap2 = /* @__PURE__ */ flap(Functor2);
var Pointed2 = {
  URI,
  of: of2
};
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: of2
};
var Chain2 = {
  URI,
  map: _map,
  ap: _ap,
  chain: flatMap2
};
var chainRec = (a, f) => (initialState) => (r) => async () => {
  let current = await f(a)(initialState)(r)();
  while (true) {
    if (isLeft2(current)) {
      return current;
    }
    const [result, state] = current.right;
    if (isLeft2(result)) {
      current = await f(result.left)(state)(r)();
      continue;
    } else {
      return right([result.right, state]);
    }
  }
};
var ChainRec = {
  ...Chain2,
  chainRec
};
var FromState = {
  URI,
  fromState: fromState2
};
var get2 = /* @__PURE__ */ get(FromState);
var put2 = /* @__PURE__ */ put(FromState);
var modify2 = /* @__PURE__ */ modify(FromState);
var gets2 = /* @__PURE__ */ gets(FromState);
var fromStateK2 = /* @__PURE__ */ fromStateK(FromState);
var chainStateK2 = /* @__PURE__ */ chainStateK(
  FromState,
  Chain2
);
var Monad2 = {
  URI,
  map: _map,
  ap: _ap,
  of: of2,
  chain: flatMap2
};
var MonadIO = {
  URI,
  map: _map,
  ap: _ap,
  of: of2,
  chain: flatMap2,
  fromIO
};
var MonadTask = {
  URI,
  map: _map,
  ap: _ap,
  of: of2,
  chain: flatMap2,
  fromIO,
  fromTask
};
var MonadThrow = {
  URI,
  map: _map,
  ap: _ap,
  of: of2,
  chain: flatMap2,
  throwError
};
var FromEither = {
  URI,
  fromEither
};
var FromIO = {
  URI,
  fromIO
};
var FromTask = {
  URI,
  fromIO,
  fromTask
};
var FromReader = {
  URI,
  fromReader
};
var tap2 = /* @__PURE__ */ dual(2, tap(Chain2));
var tapEither2 = /* @__PURE__ */ dual(2, tapEither(FromEither, Chain2));
var tapIO2 = /* @__PURE__ */ dual(2, tapIO(FromIO, Chain2));
var tapTask2 = /* @__PURE__ */ dual(2, tapTask(FromTask, Chain2));
var tapReader2 = /* @__PURE__ */ dual(2, tapReader(FromReader, Chain2));
var Bifunctor = {
  URI,
  bimap: _bimap,
  mapLeft: _mapLeft
};
var Alt = {
  URI,
  map: _map,
  alt: _alt
};
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 fromOption2 = /* @__PURE__ */ fromOption(FromEither);
var fromOptionK2 = /* @__PURE__ */ fromOptionK(FromEither);
var chainOptionK2 = /* @__PURE__ */ chainOptionK(
  FromEither,
  Chain2
);
var chainOptionKW = chainOptionK2;
var chainEitherK = flatMapEither2;
var chainEitherKW = flatMapEither2;
var chainFirstEitherK = tapEither2;
var chainFirstEitherKW = tapEither2;
var fromPredicate2 = /* @__PURE__ */ fromPredicate(FromEither);
var filterOrElse2 = /* @__PURE__ */ filterOrElse(FromEither, Chain2);
var filterOrElseW = filterOrElse2;
var fromEitherK2 = /* @__PURE__ */ fromEitherK(FromEither);
var fromIOK2 = /* @__PURE__ */ fromIOK(FromIO);
var chainIOK = flatMapIO2;
var chainFirstIOK = tapIO2;
var chainTaskEitherKW = flatMapTaskEither;
var chainTaskEitherK = flatMapTaskEither;
var chainIOEitherKW = flatMapIOEither;
var chainIOEitherK = flatMapIOEither;
var fromTaskK2 = /* @__PURE__ */ fromTaskK(FromTask);
var chainTaskK = flatMapTask2;
var chainFirstTaskK = tapTask2;
var evaluate2 = /* @__PURE__ */ evaluate(Functor);
var execute2 = /* @__PURE__ */ execute(Functor);
var bindTo2 = /* @__PURE__ */ bindTo(Functor2);
var let_2 = /* @__PURE__ */ let_(Functor2);
var bind2 = /* @__PURE__ */ bind(Chain2);
var bindW = bind2;
var do_2 = do_(Pointed2, ChainRec);
var apS2 = /* @__PURE__ */ apS(Apply);
var apSW = apS2;
var traverseReadonlyNonEmptyArrayWithIndex = (f) => (as3) => (s) => (r) => () => tail(as3).reduce(
  (acc, a, i) => acc.then(
    (ebs) => isLeft(ebs) ? acc : f(
      i + 1,
      a
    )(ebs.right[1])(r)().then((eb) => {
      if (isLeft(eb)) {
        return eb;
      }
      const [b, s2] = eb.right;
      ebs.right[0].push(b);
      ebs.right[1] = s2;
      return ebs;
    })
  ),
  f(0, head(as3))(s)(r)().then(map(([b, s2]) => [[b], s2]))
);
var traverseReadonlyArrayWithIndex = (f) => {
  const g = traverseReadonlyNonEmptyArrayWithIndex(f);
  return (as3) => isNonEmpty(as3) ? g(as3) : of2(emptyReadonlyArray);
};
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 stateReaderTaskEither = {
  URI,
  map: _map,
  of: of2,
  ap: _ap,
  chain: flatMap2,
  bimap: _bimap,
  mapLeft: _mapLeft,
  alt: _alt,
  fromIO,
  fromTask,
  throwError
};
var stateReaderTaskEitherSeq = {
  URI,
  map: _map,
  of: of2,
  ap: _ap,
  chain: flatMap2,
  bimap: _bimap,
  mapLeft: _mapLeft,
  alt: _alt,
  fromIO,
  fromTask,
  throwError
};
var evalState = (fsa, s) => pipe(
  fsa(s),
  map2(([a]) => a)
);
var execState = (fsa, s) => pipe(
  fsa(s),
  map2(([_, s2]) => s2)
);
function run(ma, s, r) {
  return ma(s)(r)();
}

export {
  left2 as left,
  right3 as right,
  rightTask2 as rightTask,
  leftTask2 as leftTask,
  rightReader2 as rightReader,
  leftReader2 as leftReader,
  rightIO2 as rightIO,
  leftIO2 as leftIO,
  rightState,
  leftState,
  fromEither,
  fromReader,
  fromIO,
  fromTask,
  fromState2 as fromState,
  fromTaskEither2 as fromTaskEither,
  fromIOEither2 as fromIOEither,
  fromReaderEither2 as fromReaderEither,
  fromReaderTaskEither,
  local2 as local,
  asksStateReaderTaskEitherW,
  asksStateReaderTaskEither,
  fromIOEitherK,
  fromTaskEitherK,
  fromReaderTaskEitherK,
  chainReaderTaskEitherKW,
  chainReaderTaskEitherK,
  map4 as map,
  bimap2 as bimap,
  mapLeft2 as mapLeft,
  ap2 as ap,
  apW,
  of2 as of,
  flatMap2 as flatMap,
  flatMapTaskEither,
  flatMapIO2 as flatMapIO,
  flatMapTask2 as flatMapTask,
  flatMapReader2 as flatMapReader,
  flatMapIOEither,
  flatMapEither2 as flatMapEither,
  flatMapOption2 as flatMapOption,
  flatMapReaderTaskEither,
  flatMapState,
  flattenW,
  flatten,
  altW2 as altW,
  alt2 as alt,
  throwError,
  URI,
  Functor2 as Functor,
  as2 as as,
  asUnit2 as asUnit,
  flap2 as flap,
  Pointed2 as Pointed,
  Apply,
  apFirst2 as apFirst,
  apFirstW,
  apSecond2 as apSecond,
  apSecondW,
  Applicative,
  Chain2 as Chain,
  chainRec,
  ChainRec,
  FromState,
  get2 as get,
  put2 as put,
  modify2 as modify,
  gets2 as gets,
  fromStateK2 as fromStateK,
  chainStateK2 as chainStateK,
  Monad2 as Monad,
  MonadIO,
  MonadTask,
  MonadThrow,
  FromEither,
  FromIO,
  FromTask,
  FromReader,
  tap2 as tap,
  tapEither2 as tapEither,
  tapIO2 as tapIO,
  tapTask2 as tapTask,
  tapReader2 as tapReader,
  Bifunctor,
  Alt,
  ask2 as ask,
  asks2 as asks,
  fromReaderK2 as fromReaderK,
  chainReaderK,
  chainReaderKW,
  chainFirstReaderK,
  chainFirstReaderKW,
  fromOption2 as fromOption,
  fromOptionK2 as fromOptionK,
  chainOptionK2 as chainOptionK,
  chainOptionKW,
  chainEitherK,
  chainEitherKW,
  chainFirstEitherK,
  chainFirstEitherKW,
  fromPredicate2 as fromPredicate,
  filterOrElse2 as filterOrElse,
  filterOrElseW,
  fromEitherK2 as fromEitherK,
  fromIOK2 as fromIOK,
  chainIOK,
  chainFirstIOK,
  chainTaskEitherKW,
  chainTaskEitherK,
  chainIOEitherKW,
  chainIOEitherK,
  fromTaskK2 as fromTaskK,
  chainTaskK,
  chainFirstTaskK,
  evaluate2 as evaluate,
  execute2 as execute,
  bindTo2 as bindTo,
  let_2 as let_,
  bind2 as bind,
  bindW,
  do_2 as do_,
  apS2 as apS,
  apSW,
  traverseReadonlyNonEmptyArrayWithIndex,
  traverseReadonlyArrayWithIndex,
  traverseArrayWithIndex,
  traverseArray,
  sequenceArray,
  chain,
  chainW,
  chainFirst,
  chainFirstW,
  stateReaderTaskEither,
  stateReaderTaskEitherSeq,
  evalState,
  execState,
  run,
  StateReaderTaskEither_exports
};
