import {
  alt,
  ap,
  chainNullableK,
  chainOptionK,
  flatMap,
  fromEither as fromEither2,
  fromF,
  fromNullable,
  fromNullableK,
  fromOptionK,
  fromPredicate,
  getOrElse,
  map as map2,
  match,
  matchE,
  some as some3,
  zero
} from "./chunk-S7TLXYLA.mjs";
import {
  filter,
  filterMap,
  partition,
  partitionMap
} from "./chunk-NNZUJMKX.mjs";
import {
  compact,
  separate
} from "./chunk-47WJXJHK.mjs";
import {
  fromTaskK,
  tapTask
} from "./chunk-WCFCQNDF.mjs";
import {
  ApplyPar,
  Chain,
  Functor as Functor2,
  Monad,
  Pointed,
  fromIO,
  map as map3,
  of,
  traverseReadonlyNonEmptyArrayWithIndex as traverseReadonlyNonEmptyArrayWithIndex2
} from "./chunk-64PLWQD3.mjs";
import {
  fromIOK,
  tapIO
} from "./chunk-TUMMPMRG.mjs";
import {
  Compactable,
  Filterable,
  Functor2 as Functor,
  do_3 as do_,
  fromEither,
  isNone as isNone2,
  isSome,
  map2 as map,
  some as some2,
  traverseReadonlyNonEmptyArrayWithIndex2 as traverseReadonlyNonEmptyArrayWithIndex
} from "./chunk-3AL4EMTU.mjs";
import {
  chainEitherK,
  fromEitherK,
  tapEither
} from "./chunk-SEYMJLV6.mjs";
import {
  guard
} from "./chunk-OVCPI5FM.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,
  emptyRecord,
  flatMapIO,
  flatMapTask,
  head,
  isLeft,
  isNonEmpty,
  isNone,
  isRight,
  none,
  singleton,
  some,
  tail
} from "./chunk-WPJQNT3M.mjs";
import {
  SK,
  dual,
  flow,
  identity,
  pipe
} from "./chunk-ZI34HWXZ.mjs";
import {
  __export
} from "./chunk-QK4PKNB3.mjs";

// src/TaskOption.ts
var TaskOption_exports = {};
__export(TaskOption_exports, {
  Alt: () => Alt,
  Alternative: () => Alternative,
  ApT: () => ApT,
  ApplicativePar: () => ApplicativePar,
  ApplicativeSeq: () => ApplicativeSeq,
  ApplyPar: () => ApplyPar2,
  ApplySeq: () => ApplySeq,
  Chain: () => Chain2,
  ChainRec: () => ChainRec,
  Compactable: () => Compactable2,
  Do: () => Do,
  Filterable: () => Filterable2,
  FromEither: () => FromEither,
  FromIO: () => FromIO,
  FromTask: () => FromTask,
  Functor: () => Functor3,
  Monad: () => Monad2,
  MonadIO: () => MonadIO,
  MonadTask: () => MonadTask,
  MonadThrow: () => MonadThrow,
  Pointed: () => Pointed2,
  URI: () => URI,
  Zero: () => Zero,
  alt: () => alt2,
  altW: () => altW,
  ap: () => ap2,
  apFirst: () => apFirst2,
  apS: () => apS2,
  apSecond: () => apSecond2,
  as: () => as2,
  asUnit: () => asUnit2,
  bind: () => bind2,
  bindTo: () => bindTo2,
  chain: () => chain,
  chainEitherK: () => chainEitherK2,
  chainFirst: () => chainFirst,
  chainFirstEitherK: () => chainFirstEitherK,
  chainFirstIOK: () => chainFirstIOK,
  chainFirstTaskK: () => chainFirstTaskK,
  chainIOK: () => chainIOK,
  chainNullableK: () => chainNullableK2,
  chainOptionK: () => chainOptionK2,
  chainRec: () => chainRec,
  chainTaskK: () => chainTaskK,
  compact: () => compact2,
  do: () => do_2,
  filter: () => filter2,
  filterMap: () => filterMap2,
  flap: () => flap2,
  flatMap: () => flatMap2,
  flatMapIO: () => flatMapIO2,
  flatMapTask: () => flatMapTask2,
  flatten: () => flatten,
  fold: () => fold,
  foldW: () => foldW,
  fromEither: () => fromEither3,
  fromEitherK: () => fromEitherK2,
  fromIO: () => fromIO2,
  fromIOK: () => fromIOK2,
  fromNullable: () => fromNullable2,
  fromNullableK: () => fromNullableK2,
  fromOption: () => fromOption,
  fromOptionK: () => fromOptionK2,
  fromPredicate: () => fromPredicate2,
  fromTask: () => fromTask,
  fromTaskEither: () => fromTaskEither,
  fromTaskK: () => fromTaskK2,
  getOrElse: () => getOrElse2,
  getOrElseW: () => getOrElseW,
  guard: () => guard2,
  let: () => let_2,
  map: () => map4,
  match: () => match2,
  matchE: () => matchE2,
  matchEW: () => matchEW,
  matchW: () => matchW,
  none: () => none2,
  of: () => of2,
  partition: () => partition2,
  partitionMap: () => partitionMap2,
  separate: () => separate2,
  sequenceArray: () => sequenceArray,
  sequenceSeqArray: () => sequenceSeqArray,
  some: () => some4,
  tap: () => tap2,
  tapEither: () => tapEither2,
  tapIO: () => tapIO2,
  tapTask: () => tapTask2,
  throwError: () => throwError,
  traverseArray: () => traverseArray,
  traverseArrayWithIndex: () => traverseArrayWithIndex,
  traverseReadonlyArrayWithIndex: () => traverseReadonlyArrayWithIndex,
  traverseReadonlyArrayWithIndexSeq: () => traverseReadonlyArrayWithIndexSeq,
  traverseReadonlyNonEmptyArrayWithIndex: () => traverseReadonlyNonEmptyArrayWithIndex3,
  traverseReadonlyNonEmptyArrayWithIndexSeq: () => traverseReadonlyNonEmptyArrayWithIndexSeq,
  traverseSeqArray: () => traverseSeqArray,
  traverseSeqArrayWithIndex: () => traverseSeqArrayWithIndex,
  tryCatch: () => tryCatch,
  tryCatchK: () => tryCatchK,
  zero: () => zero2
});
var some4 = /* @__PURE__ */ some3(Pointed);
var fromPredicate2 = /* @__PURE__ */ fromPredicate(Pointed);
var fromOption = of;
var fromEither3 = /* @__PURE__ */ fromEither2(Pointed);
var fromIO2 = (ma) => fromTask(fromIO(ma));
var fromTask = /* @__PURE__ */ fromF(Functor2);
var fromTaskEither = /* @__PURE__ */ map3(fromEither);
var match2 = /* @__PURE__ */ match(Functor2);
var matchW = match2;
var matchE2 = /* @__PURE__ */ matchE(Chain);
var fold = matchE2;
var matchEW = matchE2;
var foldW = matchEW;
var getOrElse2 = /* @__PURE__ */ getOrElse(
  Monad
);
var getOrElseW = getOrElse2;
var fromNullable2 = /* @__PURE__ */ fromNullable(Pointed);
var tryCatch = (f) => async () => {
  try {
    return await f().then(some);
  } catch (reason) {
    return none;
  }
};
var tryCatchK = (f) => (...a) => tryCatch(() => f(...a));
var fromNullableK2 = /* @__PURE__ */ fromNullableK(Pointed);
var chainNullableK2 = /* @__PURE__ */ chainNullableK(Monad);
var fromOptionK2 = /* @__PURE__ */ fromOptionK(Pointed);
var chainOptionK2 = /* @__PURE__ */ chainOptionK(Monad);
var map4 = /* @__PURE__ */ map2(Functor2);
var ap2 = /* @__PURE__ */ ap(
  ApplyPar
);
var of2 = some4;
var _FromIO = {
  fromIO: fromIO2
};
var _FromTask = {
  fromTask
};
var flatMap2 = /* @__PURE__ */ dual(2, flatMap(Monad));
var _FlatMap = {
  flatMap: flatMap2
};
var flatMapIO2 = /* @__PURE__ */ flatMapIO(_FromIO, _FlatMap);
var flatMapTask2 = /* @__PURE__ */ flatMapTask(_FromTask, _FlatMap);
var flatten = /* @__PURE__ */ flatMap2(identity);
var alt2 = /* @__PURE__ */ alt(
  Monad
);
var altW = alt2;
var zero2 = /* @__PURE__ */ zero(Pointed);
var throwError = zero2;
var none2 = /* @__PURE__ */ zero2();
var compact2 = /* @__PURE__ */ compact(Functor2, Compactable);
var separate2 = /* @__PURE__ */ separate(Functor2, Compactable, Functor);
var filter2 = /* @__PURE__ */ filter(Functor2, Filterable);
var filterMap2 = /* @__PURE__ */ filterMap(Functor2, Filterable);
var partition2 = /* @__PURE__ */ partition(Functor2, Filterable);
var partitionMap2 = /* @__PURE__ */ partitionMap(
  Functor2,
  Filterable
);
var _map = (fa, f) => pipe(fa, map4(f));
var _ap = (fab, fa) => pipe(fab, ap2(fa));
var _alt = (fa, that) => pipe(fa, alt2(that));
var _filter = (fa, predicate) => pipe(fa, filter2(predicate));
var _filterMap = (fa, f) => pipe(fa, filterMap2(f));
var _partition = (fa, predicate) => pipe(fa, partition2(predicate));
var _partitionMap = (fa, f) => pipe(fa, partitionMap2(f));
var URI = "TaskOption";
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 ApplyPar2 = {
  URI,
  map: _map,
  ap: _ap
};
var apFirst2 = /* @__PURE__ */ apFirst(ApplyPar2);
var apSecond2 = /* @__PURE__ */ apSecond(ApplyPar2);
var ApplicativePar = {
  URI,
  map: _map,
  ap: _ap,
  of: of2
};
var _apSeq = (fab, fa) => flatMap2(fab, (f) => pipe(fa, map4(f)));
var ApplySeq = {
  URI,
  map: _map,
  ap: _apSeq
};
var ApplicativeSeq = {
  URI,
  map: _map,
  ap: _apSeq,
  of: of2
};
var Chain2 = {
  URI,
  map: _map,
  ap: _ap,
  chain: flatMap2
};
var chainRec = (a, f) => async () => {
  let current = await f(a)();
  while (true) {
    if (isSome(current) && isLeft(current.value)) {
      current = await f(current.value.left)();
      continue;
    }
    if (isNone2(current)) {
      return current;
    }
    if (isRight(current.value)) {
      return some2(current.value.right);
    }
  }
};
var ChainRec = {
  ...Chain2,
  chainRec
};
var FromEither = {
  URI,
  fromEither: fromEither3
};
var FromIO = {
  URI,
  fromIO: fromIO2
};
var FromTask = {
  URI,
  fromIO: fromIO2,
  fromTask
};
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 Alt = {
  URI,
  map: _map,
  alt: _alt
};
var Zero = {
  URI,
  zero: zero2
};
var guard2 = /* @__PURE__ */ guard(Zero, Pointed2);
var Alternative = {
  URI,
  map: _map,
  ap: _ap,
  of: of2,
  alt: _alt,
  zero: zero2
};
var Monad2 = {
  URI,
  map: _map,
  ap: _ap,
  of: of2,
  chain: flatMap2
};
var MonadIO = {
  URI,
  map: _map,
  ap: _ap,
  of: of2,
  chain: flatMap2,
  fromIO: fromIO2
};
var MonadThrow = {
  ...Monad2,
  throwError
};
var MonadTask = {
  URI,
  map: _map,
  ap: _ap,
  of: of2,
  chain: flatMap2,
  fromIO: fromIO2,
  fromTask
};
var Compactable2 = {
  URI,
  compact: compact2,
  separate: separate2
};
var Filterable2 = {
  URI,
  map: _map,
  compact: compact2,
  separate: separate2,
  filter: _filter,
  filterMap: _filterMap,
  partition: _partition,
  partitionMap: _partitionMap
};
var fromIOK2 = /* @__PURE__ */ fromIOK(FromIO);
var chainIOK = flatMapIO2;
var chainFirstIOK = tapIO2;
var fromEitherK2 = /* @__PURE__ */ fromEitherK(FromEither);
var chainEitherK2 = /* @__PURE__ */ chainEitherK(FromEither, Chain2);
var chainFirstEitherK = tapEither2;
var fromTaskK2 = /* @__PURE__ */ fromTaskK(FromTask);
var chainTaskK = flatMapTask2;
var chainFirstTaskK = tapTask2;
var Do = /* @__PURE__ */ of2(emptyRecord);
var bindTo2 = /* @__PURE__ */ bindTo(Functor3);
var let_2 = /* @__PURE__ */ let_(Functor3);
var bind2 = /* @__PURE__ */ bind(Chain2);
var do_2 = do_(Pointed2, ChainRec);
var apS2 = /* @__PURE__ */ apS(ApplyPar2);
var ApT = /* @__PURE__ */ of2(emptyReadonlyArray);
var traverseReadonlyNonEmptyArrayWithIndex3 = (f) => flow(traverseReadonlyNonEmptyArrayWithIndex2(f), map3(traverseReadonlyNonEmptyArrayWithIndex(SK)));
var traverseReadonlyArrayWithIndex = (f) => {
  const g = traverseReadonlyNonEmptyArrayWithIndex3(f);
  return (as3) => isNonEmpty(as3) ? g(as3) : ApT;
};
var traverseReadonlyNonEmptyArrayWithIndexSeq = (f) => (as3) => () => tail(as3).reduce(
  (acc, a, i) => acc.then(
    (obs) => isNone(obs) ? acc : f(i + 1, a)().then((ob) => {
      if (isNone(ob)) {
        return ob;
      }
      obs.value.push(ob.value);
      return obs;
    })
  ),
  f(0, head(as3))().then(map(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 = flatMap2;
var chainFirst = tap2;

export {
  some4 as some,
  fromPredicate2 as fromPredicate,
  fromOption,
  fromEither3 as fromEither,
  fromIO2 as fromIO,
  fromTask,
  fromTaskEither,
  match2 as match,
  matchW,
  matchE2 as matchE,
  fold,
  matchEW,
  foldW,
  getOrElse2 as getOrElse,
  getOrElseW,
  fromNullable2 as fromNullable,
  tryCatch,
  tryCatchK,
  fromNullableK2 as fromNullableK,
  chainNullableK2 as chainNullableK,
  fromOptionK2 as fromOptionK,
  chainOptionK2 as chainOptionK,
  map4 as map,
  ap2 as ap,
  of2 as of,
  flatMap2 as flatMap,
  flatMapIO2 as flatMapIO,
  flatMapTask2 as flatMapTask,
  flatten,
  alt2 as alt,
  altW,
  zero2 as zero,
  throwError,
  none2 as none,
  compact2 as compact,
  separate2 as separate,
  filter2 as filter,
  filterMap2 as filterMap,
  partition2 as partition,
  partitionMap2 as partitionMap,
  URI,
  Functor3 as Functor,
  as2 as as,
  asUnit2 as asUnit,
  flap2 as flap,
  Pointed2 as Pointed,
  ApplyPar2 as ApplyPar,
  apFirst2 as apFirst,
  apSecond2 as apSecond,
  ApplicativePar,
  ApplySeq,
  ApplicativeSeq,
  Chain2 as Chain,
  chainRec,
  ChainRec,
  FromEither,
  FromIO,
  FromTask,
  tap2 as tap,
  tapEither2 as tapEither,
  tapIO2 as tapIO,
  tapTask2 as tapTask,
  Alt,
  Zero,
  guard2 as guard,
  Alternative,
  Monad2 as Monad,
  MonadIO,
  MonadThrow,
  MonadTask,
  Compactable2 as Compactable,
  Filterable2 as Filterable,
  fromIOK2 as fromIOK,
  chainIOK,
  chainFirstIOK,
  fromEitherK2 as fromEitherK,
  chainEitherK2 as chainEitherK,
  chainFirstEitherK,
  fromTaskK2 as fromTaskK,
  chainTaskK,
  chainFirstTaskK,
  Do,
  bindTo2 as bindTo,
  let_2 as let_,
  bind2 as bind,
  do_2 as do_,
  apS2 as apS,
  ApT,
  traverseReadonlyNonEmptyArrayWithIndex3 as traverseReadonlyNonEmptyArrayWithIndex,
  traverseReadonlyArrayWithIndex,
  traverseReadonlyNonEmptyArrayWithIndexSeq,
  traverseReadonlyArrayWithIndexSeq,
  traverseArrayWithIndex,
  traverseArray,
  sequenceArray,
  traverseSeqArrayWithIndex,
  traverseSeqArray,
  sequenceSeqArray,
  chain,
  chainFirst,
  TaskOption_exports
};
