import {
  fromIOK,
  tapIO
} from "./chunk-TUMMPMRG.mjs";
import {
  do_3 as do_
} from "./chunk-3AL4EMTU.mjs";
import {
  getApplicativeMonoid
} from "./chunk-LN4UQKIO.mjs";
import {
  as,
  asUnit,
  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,
  flatMapIO,
  head,
  isLeft,
  isNonEmpty,
  singleton,
  tail
} from "./chunk-WPJQNT3M.mjs";
import {
  dual,
  identity,
  pipe
} from "./chunk-ZI34HWXZ.mjs";
import {
  __export
} from "./chunk-QK4PKNB3.mjs";

// src/Task.ts
var Task_exports = {};
__export(Task_exports, {
  ApT: () => ApT,
  ApplicativePar: () => ApplicativePar,
  ApplicativeSeq: () => ApplicativeSeq,
  ApplyPar: () => ApplyPar,
  ApplySeq: () => ApplySeq,
  Chain: () => Chain,
  ChainRec: () => ChainRec,
  Do: () => Do,
  FromIO: () => FromIO,
  FromTask: () => FromTask,
  Functor: () => Functor,
  Monad: () => Monad,
  MonadIO: () => MonadIO,
  MonadTask: () => MonadTask,
  Pointed: () => Pointed,
  URI: () => URI,
  ap: () => ap,
  apFirst: () => apFirst2,
  apS: () => apS2,
  apSecond: () => apSecond2,
  as: () => as2,
  asUnit: () => asUnit2,
  bind: () => bind2,
  bindTo: () => bindTo2,
  chain: () => chain,
  chainFirst: () => chainFirst,
  chainFirstIOK: () => chainFirstIOK,
  chainIOK: () => chainIOK,
  chainRec: () => chainRec,
  delay: () => delay,
  do: () => do_2,
  flap: () => flap2,
  flatMap: () => flatMap,
  flatMapIO: () => flatMapIO2,
  flatten: () => flatten,
  fromIO: () => fromIO,
  fromIOK: () => fromIOK2,
  fromTask: () => fromTask,
  getMonoid: () => getMonoid,
  getRaceMonoid: () => getRaceMonoid,
  getSemigroup: () => getSemigroup,
  let: () => let_2,
  map: () => map,
  never: () => never,
  of: () => of,
  sequenceArray: () => sequenceArray,
  sequenceSeqArray: () => sequenceSeqArray,
  tap: () => tap2,
  tapIO: () => tapIO2,
  task: () => task,
  taskSeq: () => taskSeq,
  traverseArray: () => traverseArray,
  traverseArrayWithIndex: () => traverseArrayWithIndex,
  traverseReadonlyArrayWithIndex: () => traverseReadonlyArrayWithIndex,
  traverseReadonlyArrayWithIndexSeq: () => traverseReadonlyArrayWithIndexSeq,
  traverseReadonlyNonEmptyArrayWithIndex: () => traverseReadonlyNonEmptyArrayWithIndex,
  traverseReadonlyNonEmptyArrayWithIndexSeq: () => traverseReadonlyNonEmptyArrayWithIndexSeq,
  traverseSeqArray: () => traverseSeqArray,
  traverseSeqArrayWithIndex: () => traverseSeqArrayWithIndex
});
var fromIO = (ma) => () => Promise.resolve().then(ma);
function delay(millis) {
  return (ma) => () => new Promise((resolve) => {
    setTimeout(() => {
      Promise.resolve().then(ma).then(resolve);
    }, millis);
  });
}
var _map = (fa, f) => pipe(fa, map(f));
var _apPar = (fab, fa) => pipe(fab, ap(fa));
var _apSeq = (fab, fa) => flatMap(fab, (f) => pipe(fa, map(f)));
var map = (f) => (fa) => () => Promise.resolve().then(fa).then(f);
var ap = (fa) => (fab) => () => Promise.all([Promise.resolve().then(fab), Promise.resolve().then(fa)]).then(([f, a]) => f(a));
var of = (a) => () => Promise.resolve(a);
var flatMap = /* @__PURE__ */ dual(
  2,
  (ma, f) => () => Promise.resolve().then(ma).then((a) => f(a)())
);
var flatten = /* @__PURE__ */ flatMap(identity);
var URI = "Task";
function getRaceMonoid() {
  return {
    concat: (x, y) => () => Promise.race([Promise.resolve().then(x), Promise.resolve().then(y)]),
    empty: never
  };
}
var Functor = {
  URI,
  map: _map
};
var as2 = dual(2, as(Functor));
var asUnit2 = asUnit(Functor);
var flap2 = /* @__PURE__ */ flap(Functor);
var Pointed = {
  URI,
  of
};
var ApplyPar = {
  URI,
  map: _map,
  ap: _apPar
};
var apFirst2 = /* @__PURE__ */ apFirst(ApplyPar);
var apSecond2 = /* @__PURE__ */ apSecond(ApplyPar);
var ApplicativePar = {
  URI,
  map: _map,
  ap: _apPar,
  of
};
var ApplySeq = {
  URI,
  map: _map,
  ap: _apSeq
};
var ApplicativeSeq = {
  URI,
  map: _map,
  ap: _apSeq,
  of
};
var Chain = {
  URI,
  map: _map,
  ap: _apPar,
  chain: flatMap
};
var chainRec = (a, f) => async () => {
  let current = await f(a)();
  while (isLeft(current)) {
    current = await f(current.left)();
  }
  return current.right;
};
var ChainRec = {
  ...Chain,
  chainRec
};
var Monad = {
  URI,
  map: _map,
  of,
  ap: _apPar,
  chain: flatMap
};
var MonadIO = {
  URI,
  map: _map,
  of,
  ap: _apPar,
  chain: flatMap,
  fromIO
};
var fromTask = identity;
var MonadTask = {
  URI,
  map: _map,
  of,
  ap: _apPar,
  chain: flatMap,
  fromIO,
  fromTask
};
var FromIO = {
  URI,
  fromIO
};
var _FlatMap = {
  flatMap
};
var _FromIO = {
  fromIO: FromIO.fromIO
};
var flatMapIO2 = flatMapIO(_FromIO, _FlatMap);
var tap2 = /* @__PURE__ */ dual(2, tap(Chain));
var tapIO2 = /* @__PURE__ */ dual(2, tapIO(FromIO, Chain));
var fromIOK2 = /* @__PURE__ */ fromIOK(FromIO);
var chainIOK = flatMapIO2;
var chainFirstIOK = tapIO2;
var FromTask = {
  URI,
  fromIO,
  fromTask
};
var never = () => new Promise((_) => void 0);
var Do = /* @__PURE__ */ of(emptyRecord);
var bindTo2 = /* @__PURE__ */ bindTo(Functor);
var let_2 = /* @__PURE__ */ let_(Functor);
var bind2 = /* @__PURE__ */ bind(Chain);
var do_2 = do_(Pointed, ChainRec);
var apS2 = /* @__PURE__ */ apS(ApplyPar);
var ApT = /* @__PURE__ */ of(emptyReadonlyArray);
var traverseReadonlyNonEmptyArrayWithIndex = (f) => (as3) => () => Promise.all(as3.map((a, i) => Promise.resolve().then(() => f(i, a)())));
var traverseReadonlyArrayWithIndex = (f) => {
  const g = traverseReadonlyNonEmptyArrayWithIndex(f);
  return (as3) => isNonEmpty(as3) ? g(as3) : ApT;
};
var traverseReadonlyNonEmptyArrayWithIndexSeq = (f) => (as3) => () => tail(as3).reduce(
  (acc, a, i) => acc.then(
    (bs) => Promise.resolve().then(f(i + 1, a)).then((b) => {
      bs.push(b);
      return bs;
    })
  ),
  Promise.resolve().then(f(0, head(as3))).then(singleton)
);
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 chain = flatMap;
var chainFirst = tap2;
var task = {
  URI,
  map: _map,
  of,
  ap: _apPar,
  chain: flatMap,
  fromIO,
  fromTask
};
var taskSeq = {
  URI,
  map: _map,
  of,
  ap: _apSeq,
  chain: flatMap,
  fromIO,
  fromTask
};
var getSemigroup = /* @__PURE__ */ getApplySemigroup(ApplySeq);
var getMonoid = /* @__PURE__ */ getApplicativeMonoid(ApplicativeSeq);

export {
  fromIO,
  delay,
  map,
  ap,
  of,
  flatMap,
  flatten,
  URI,
  getRaceMonoid,
  Functor,
  as2 as as,
  asUnit2 as asUnit,
  flap2 as flap,
  Pointed,
  ApplyPar,
  apFirst2 as apFirst,
  apSecond2 as apSecond,
  ApplicativePar,
  ApplySeq,
  ApplicativeSeq,
  Chain,
  chainRec,
  ChainRec,
  Monad,
  MonadIO,
  fromTask,
  MonadTask,
  FromIO,
  flatMapIO2 as flatMapIO,
  tap2 as tap,
  tapIO2 as tapIO,
  fromIOK2 as fromIOK,
  chainIOK,
  chainFirstIOK,
  FromTask,
  never,
  Do,
  bindTo2 as bindTo,
  let_2 as let_,
  bind2 as bind,
  do_2 as do_,
  apS2 as apS,
  ApT,
  traverseReadonlyNonEmptyArrayWithIndex,
  traverseReadonlyArrayWithIndex,
  traverseReadonlyNonEmptyArrayWithIndexSeq,
  traverseReadonlyArrayWithIndexSeq,
  traverseArrayWithIndex,
  traverseArray,
  sequenceArray,
  traverseSeqArrayWithIndex,
  traverseSeqArray,
  sequenceSeqArray,
  chain,
  chainFirst,
  task,
  taskSeq,
  getSemigroup,
  getMonoid,
  Task_exports
};
