import {
  fromTheseK
} from "./chunk-YRRTPGRV.mjs";
import {
  ap,
  bimap,
  both as both2,
  chain,
  left,
  leftF,
  map as map3,
  mapLeft,
  match,
  matchE,
  right,
  rightF,
  swap,
  toTuple2
} from "./chunk-O4IKOJCW.mjs";
import {
  both,
  getSemigroup,
  isBoth,
  isLeft,
  map as map2,
  traverseReadonlyNonEmptyArrayWithIndex as traverseReadonlyNonEmptyArrayWithIndex2
} from "./chunk-DTQCPQTO.mjs";
import {
  fromTaskK
} from "./chunk-WCFCQNDF.mjs";
import {
  ApplicativePar,
  ApplySeq,
  Functor,
  Monad,
  Pointed,
  fromIO,
  map,
  of,
  traverseReadonlyNonEmptyArrayWithIndex
} from "./chunk-64PLWQD3.mjs";
import {
  fromIOK
} from "./chunk-TUMMPMRG.mjs";
import {
  fromOption,
  fromOptionK,
  fromPredicate
} from "./chunk-SEYMJLV6.mjs";
import {
  flap
} from "./chunk-WKK6AYQI.mjs";
import {
  getApplySemigroup
} from "./chunk-ZMCLCMOU.mjs";
import {
  emptyReadonlyArray,
  head,
  isNonEmpty,
  singleton,
  tail
} from "./chunk-WPJQNT3M.mjs";
import {
  SK,
  flow,
  pipe
} from "./chunk-ZI34HWXZ.mjs";
import {
  __export
} from "./chunk-QK4PKNB3.mjs";

// src/TaskThese.ts
var TaskThese_exports = {};
__export(TaskThese_exports, {
  ApT: () => ApT,
  Bifunctor: () => Bifunctor,
  FromEither: () => FromEither,
  FromIO: () => FromIO,
  FromTask: () => FromTask,
  FromThese: () => FromThese,
  Functor: () => Functor2,
  Pointed: () => Pointed2,
  URI: () => URI,
  bifunctorTaskThese: () => bifunctorTaskThese,
  bimap: () => bimap2,
  both: () => both3,
  flap: () => flap2,
  fold: () => fold,
  foldW: () => foldW,
  fromEither: () => fromEither,
  fromIO: () => fromIO2,
  fromIOEither: () => fromIOEither,
  fromIOK: () => fromIOK2,
  fromOption: () => fromOption2,
  fromOptionK: () => fromOptionK2,
  fromPredicate: () => fromPredicate2,
  fromTask: () => fromTask,
  fromTaskK: () => fromTaskK2,
  fromThese: () => fromThese,
  fromTheseK: () => fromTheseK2,
  functorTaskThese: () => functorTaskThese,
  getApplicative: () => getApplicative,
  getApply: () => getApply,
  getChain: () => getChain,
  getMonad: () => getMonad,
  getSemigroup: () => getSemigroup2,
  left: () => left2,
  leftIO: () => leftIO,
  leftTask: () => leftTask,
  map: () => map4,
  mapLeft: () => mapLeft2,
  match: () => match2,
  matchE: () => matchE2,
  matchEW: () => matchEW,
  matchW: () => matchW,
  of: () => of2,
  right: () => right2,
  rightIO: () => rightIO,
  rightTask: () => rightTask,
  swap: () => swap2,
  taskThese: () => taskThese,
  toTuple: () => toTuple,
  toTuple2: () => toTuple22,
  traverseReadonlyArrayWithIndex: () => traverseReadonlyArrayWithIndex,
  traverseReadonlyArrayWithIndexSeq: () => traverseReadonlyArrayWithIndexSeq,
  traverseReadonlyNonEmptyArrayWithIndex: () => traverseReadonlyNonEmptyArrayWithIndex3,
  traverseReadonlyNonEmptyArrayWithIndexSeq: () => traverseReadonlyNonEmptyArrayWithIndexSeq
});
var left2 = /* @__PURE__ */ left(Pointed);
var right2 = /* @__PURE__ */ right(Pointed);
var both3 = /* @__PURE__ */ both2(Pointed);
var rightTask = /* @__PURE__ */ rightF(Functor);
var leftTask = /* @__PURE__ */ leftF(Functor);
var rightIO = /* @__PURE__ */ flow(fromIO, rightTask);
var leftIO = /* @__PURE__ */ flow(fromIO, leftTask);
var fromEither = of;
var fromThese = of;
var fromIO2 = rightIO;
var fromIOEither = fromIO;
var fromTask = rightTask;
var match2 = /* @__PURE__ */ match(Functor);
var matchW = match2;
var matchE2 = /* @__PURE__ */ matchE(Monad);
var fold = matchE2;
var matchEW = fold;
var foldW = matchEW;
var swap2 = /* @__PURE__ */ swap(Functor);
var _map = (fa, f) => pipe(fa, map4(f));
var _bimap = (fa, f, g) => pipe(fa, bimap2(f, g));
var _mapLeft = (fa, f) => pipe(fa, mapLeft2(f));
var map4 = /* @__PURE__ */ map3(
  Functor
);
var bimap2 = /* @__PURE__ */ bimap(Functor);
var mapLeft2 = /* @__PURE__ */ mapLeft(
  Functor
);
var of2 = right2;
var URI = "TaskThese";
var getApply = (A, S) => {
  const ap2 = ap(A, S);
  return {
    URI,
    _E: void 0,
    map: _map,
    ap: (fab, fa) => pipe(fab, ap2(fa))
  };
};
function getApplicative(A, S) {
  const { ap: ap2 } = getApply(A, S);
  return {
    URI,
    _E: void 0,
    map: _map,
    ap: ap2,
    of: of2
  };
}
function getChain(S) {
  const A = getApply(ApplicativePar, S);
  const chain2 = chain(Monad, S);
  return {
    URI,
    _E: void 0,
    map: _map,
    ap: A.ap,
    chain: (ma, f) => pipe(ma, chain2(f))
  };
}
function getMonad(S) {
  const A = getApplicative(ApplicativePar, S);
  const C = getChain(S);
  return {
    URI,
    _E: void 0,
    map: _map,
    ap: A.ap,
    of: of2,
    chain: C.chain,
    fromIO: fromIO2,
    fromTask
  };
}
var Functor2 = {
  URI,
  map: _map
};
var flap2 = /* @__PURE__ */ flap(Functor2);
var Pointed2 = {
  URI,
  of: of2
};
var Bifunctor = {
  URI,
  bimap: _bimap,
  mapLeft: _mapLeft
};
var FromEither = {
  URI,
  fromEither
};
var fromOption2 = /* @__PURE__ */ fromOption(FromEither);
var fromOptionK2 = /* @__PURE__ */ fromOptionK(FromEither);
var fromPredicate2 = /* @__PURE__ */ fromPredicate(FromEither);
var FromThese = {
  URI,
  fromThese
};
var fromTheseK2 = /* @__PURE__ */ fromTheseK(FromThese);
var FromIO = {
  URI,
  fromIO: fromIO2
};
var fromIOK2 = /* @__PURE__ */ fromIOK(FromIO);
var FromTask = {
  URI,
  fromIO: fromIO2,
  fromTask
};
var fromTaskK2 = /* @__PURE__ */ fromTaskK(FromTask);
var toTuple22 = /* @__PURE__ */ toTuple2(Functor);
var ApT = /* @__PURE__ */ of2(emptyReadonlyArray);
var traverseReadonlyNonEmptyArrayWithIndex3 = (S) => {
  const g = traverseReadonlyNonEmptyArrayWithIndex2(S);
  return (f) => flow(traverseReadonlyNonEmptyArrayWithIndex(f), map(g(SK)));
};
var traverseReadonlyArrayWithIndex = (S) => (f) => {
  const g = traverseReadonlyNonEmptyArrayWithIndex3(S)(f);
  return (as) => isNonEmpty(as) ? g(as) : ApT;
};
var traverseReadonlyNonEmptyArrayWithIndexSeq = (S) => (f) => (as) => () => tail(as).reduce(
  (acc, a, i) => acc.then(
    (ebs) => isLeft(ebs) ? acc : f(i + 1, a)().then((eb) => {
      if (isLeft(eb)) {
        return eb;
      }
      if (isBoth(eb)) {
        const right3 = ebs.right;
        right3.push(eb.right);
        return isBoth(ebs) ? both(S.concat(ebs.left, eb.left), right3) : both(eb.left, right3);
      }
      ebs.right.push(eb.right);
      return ebs;
    })
  ),
  f(0, head(as))().then(map2(singleton))
);
var traverseReadonlyArrayWithIndexSeq = (S) => (f) => {
  const g = traverseReadonlyNonEmptyArrayWithIndexSeq(S)(f);
  return (as) => isNonEmpty(as) ? g(as) : ApT;
};
var functorTaskThese = {
  URI,
  map: _map
};
var bifunctorTaskThese = {
  URI,
  bimap: _bimap,
  mapLeft: _mapLeft
};
var toTuple = (e, a) => toTuple22(
  () => e,
  () => a
);
var taskThese = {
  URI,
  map: _map,
  bimap: _bimap,
  mapLeft: _mapLeft
};
var getSemigroup2 = (SE, SA) => getApplySemigroup(ApplySeq)(getSemigroup(SE, SA));

export {
  left2 as left,
  right2 as right,
  both3 as both,
  rightTask,
  leftTask,
  rightIO,
  leftIO,
  fromEither,
  fromThese,
  fromIO2 as fromIO,
  fromIOEither,
  fromTask,
  match2 as match,
  matchW,
  matchE2 as matchE,
  fold,
  matchEW,
  foldW,
  swap2 as swap,
  map4 as map,
  bimap2 as bimap,
  mapLeft2 as mapLeft,
  of2 as of,
  URI,
  getApply,
  getApplicative,
  getChain,
  getMonad,
  Functor2 as Functor,
  flap2 as flap,
  Pointed2 as Pointed,
  Bifunctor,
  FromEither,
  fromOption2 as fromOption,
  fromOptionK2 as fromOptionK,
  fromPredicate2 as fromPredicate,
  FromThese,
  fromTheseK2 as fromTheseK,
  FromIO,
  fromIOK2 as fromIOK,
  FromTask,
  fromTaskK2 as fromTaskK,
  toTuple22 as toTuple2,
  ApT,
  traverseReadonlyNonEmptyArrayWithIndex3 as traverseReadonlyNonEmptyArrayWithIndex,
  traverseReadonlyArrayWithIndex,
  traverseReadonlyNonEmptyArrayWithIndexSeq,
  traverseReadonlyArrayWithIndexSeq,
  functorTaskThese,
  bifunctorTaskThese,
  toTuple,
  taskThese,
  getSemigroup2 as getSemigroup,
  TaskThese_exports
};
