import {
  getApplicativeMonoid
} from "./chunk-LN4UQKIO.mjs";
import {
  not
} from "./chunk-RE4IFSW6.mjs";
import {
  separated
} from "./chunk-RAKKSTP2.mjs";
import {
  chainEitherK,
  chainOptionK,
  filterOrElse,
  fromEitherK,
  fromOption,
  fromOptionK,
  fromPredicate,
  tapEither
} from "./chunk-SEYMJLV6.mjs";
import {
  guard
} from "./chunk-OVCPI5FM.mjs";
import {
  wiltDefault,
  witherDefault
} from "./chunk-DQQFH64X.mjs";
import {
  as,
  asUnit,
  bindTo,
  flap,
  let_
} from "./chunk-WKK6AYQI.mjs";
import {
  first,
  last
} from "./chunk-CRY5FFSR.mjs";
import {
  apFirst,
  apS,
  apSecond,
  getApplySemigroup
} from "./chunk-ZMCLCMOU.mjs";
import {
  bind,
  tap
} from "./chunk-MDNVYLGG.mjs";
import {
  emptyReadonlyArray,
  emptyRecord,
  flatMapNullable,
  flatMapOption,
  head,
  isLeft,
  isNonEmpty,
  isNone,
  isRight,
  isSome,
  left,
  liftNullable,
  liftOption,
  none,
  right,
  some
} from "./chunk-WPJQNT3M.mjs";
import {
  constNull,
  constUndefined,
  dual,
  flow,
  identity,
  pipe,
  tuple
} from "./chunk-ZI34HWXZ.mjs";
import {
  __export
} from "./chunk-QK4PKNB3.mjs";

// src/Either.ts
var Either_exports = {};
__export(Either_exports, {
  Alt: () => Alt2,
  ApT: () => ApT2,
  Applicative: () => Applicative2,
  Apply: () => Apply2,
  Bifunctor: () => Bifunctor,
  Chain: () => Chain2,
  ChainRec: () => ChainRec2,
  Do: () => Do2,
  Extend: () => Extend2,
  Foldable: () => Foldable2,
  FromEither: () => FromEither2,
  Functor: () => Functor2,
  Monad: () => Monad2,
  MonadThrow: () => MonadThrow2,
  Pointed: () => Pointed2,
  Traversable: () => Traversable2,
  URI: () => URI2,
  alt: () => alt2,
  altW: () => altW2,
  ap: () => ap2,
  apFirst: () => apFirst3,
  apFirstW: () => apFirstW,
  apS: () => apS3,
  apSW: () => apSW,
  apSecond: () => apSecond3,
  apSecondW: () => apSecondW,
  apW: () => apW,
  as: () => as3,
  asUnit: () => asUnit3,
  bimap: () => bimap,
  bind: () => bind3,
  bindTo: () => bindTo3,
  bindW: () => bindW,
  chain: () => chain2,
  chainFirst: () => chainFirst2,
  chainFirstW: () => chainFirstW,
  chainNullableK: () => chainNullableK2,
  chainOptionK: () => chainOptionK2,
  chainOptionKW: () => chainOptionKW,
  chainW: () => chainW,
  do: () => do_3,
  duplicate: () => duplicate2,
  either: () => either,
  elem: () => elem2,
  exists: () => exists2,
  extend: () => extend2,
  filterOrElse: () => filterOrElse2,
  filterOrElseW: () => filterOrElseW,
  flap: () => flap3,
  flatMap: () => flatMap2,
  flatMapNullable: () => flatMapNullable2,
  flatMapOption: () => flatMapOption2,
  flatten: () => flatten2,
  flattenW: () => flattenW,
  fold: () => fold2,
  foldMap: () => foldMap2,
  foldW: () => foldW2,
  fromNullable: () => fromNullable2,
  fromNullableK: () => fromNullableK2,
  fromOption: () => fromOption2,
  fromOptionK: () => fromOptionK2,
  fromPredicate: () => fromPredicate3,
  getAltValidation: () => getAltValidation,
  getApplicativeValidation: () => getApplicativeValidation,
  getApplyMonoid: () => getApplyMonoid2,
  getApplySemigroup: () => getApplySemigroup3,
  getCompactable: () => getCompactable,
  getEq: () => getEq2,
  getFilterable: () => getFilterable,
  getOrElse: () => getOrElse2,
  getOrElseW: () => getOrElseW2,
  getSemigroup: () => getSemigroup,
  getShow: () => getShow2,
  getValidation: () => getValidation,
  getValidationMonoid: () => getValidationMonoid,
  getValidationSemigroup: () => getValidationSemigroup,
  getWitherable: () => getWitherable,
  isLeft: () => isLeft2,
  isRight: () => isRight2,
  left: () => left2,
  let: () => let_3,
  liftNullable: () => liftNullable2,
  liftOption: () => liftOption2,
  map: () => map2,
  mapLeft: () => mapLeft,
  match: () => match2,
  matchW: () => matchW2,
  of: () => of2,
  orElse: () => orElse2,
  orElseW: () => orElseW,
  parseJSON: () => parseJSON,
  reduce: () => reduce2,
  reduceRight: () => reduceRight2,
  right: () => right2,
  sequence: () => sequence2,
  sequenceArray: () => sequenceArray2,
  stringifyJSON: () => stringifyJSON,
  swap: () => swap,
  tap: () => tap3,
  throwError: () => throwError2,
  toError: () => toError,
  toUnion: () => toUnion,
  traverse: () => traverse2,
  traverseArray: () => traverseArray2,
  traverseArrayWithIndex: () => traverseArrayWithIndex2,
  traverseReadonlyArrayWithIndex: () => traverseReadonlyArrayWithIndex2,
  traverseReadonlyNonEmptyArrayWithIndex: () => traverseReadonlyNonEmptyArrayWithIndex2,
  tryCatch: () => tryCatch2,
  tryCatchK: () => tryCatchK2
});

// src/ChainRec.ts
var ChainRec_exports = {};
__export(ChainRec_exports, {
  chainRec2: () => chainRec2,
  chainRec3: () => chainRec3,
  do: () => do_,
  forever: () => forever,
  tailRec: () => tailRec,
  tailRec2: () => tailRec2,
  tailRec3: () => tailRec3,
  untilSome: () => untilSome,
  whileSome: () => whileSome
});

// src/Option.ts
var Option_exports = {};
__export(Option_exports, {
  Alt: () => Alt,
  Alternative: () => Alternative,
  ApT: () => ApT,
  Applicative: () => Applicative,
  Apply: () => Apply,
  Chain: () => Chain,
  ChainRec: () => ChainRec,
  Compactable: () => Compactable,
  Do: () => Do,
  Extend: () => Extend,
  Filterable: () => Filterable,
  Foldable: () => Foldable,
  FromEither: () => FromEither,
  Functor: () => Functor,
  Monad: () => Monad,
  MonadThrow: () => MonadThrow,
  Pointed: () => Pointed,
  Traversable: () => Traversable,
  URI: () => URI,
  Witherable: () => Witherable,
  Zero: () => Zero,
  alt: () => alt,
  altW: () => altW,
  ap: () => ap,
  apFirst: () => apFirst2,
  apS: () => apS2,
  apSecond: () => apSecond2,
  as: () => as2,
  asUnit: () => asUnit2,
  bind: () => bind2,
  bindTo: () => bindTo2,
  chain: () => chain,
  chainEitherK: () => chainEitherK2,
  chainFirst: () => chainFirst,
  chainFirstEitherK: () => chainFirstEitherK,
  chainNullableK: () => chainNullableK,
  chainRec: () => chainRec,
  compact: () => compact,
  do: () => do_2,
  duplicate: () => duplicate,
  elem: () => elem,
  exists: () => exists,
  extend: () => extend,
  filter: () => filter,
  filterMap: () => filterMap,
  flap: () => flap2,
  flatMap: () => flatMap,
  flatten: () => flatten,
  fold: () => fold,
  foldMap: () => foldMap,
  foldW: () => foldW,
  fromEither: () => fromEither,
  fromEitherK: () => fromEitherK2,
  fromNullable: () => fromNullable,
  fromNullableK: () => fromNullableK,
  fromPredicate: () => fromPredicate2,
  getApplyMonoid: () => getApplyMonoid,
  getApplySemigroup: () => getApplySemigroup2,
  getEq: () => getEq,
  getFirstMonoid: () => getFirstMonoid,
  getLastMonoid: () => getLastMonoid,
  getLeft: () => getLeft,
  getMonoid: () => getMonoid,
  getOrElse: () => getOrElse,
  getOrElseW: () => getOrElseW,
  getOrd: () => getOrd,
  getRefinement: () => getRefinement,
  getRight: () => getRight,
  getShow: () => getShow,
  guard: () => guard2,
  isNone: () => isNone2,
  isSome: () => isSome2,
  let: () => let_2,
  map: () => map,
  mapNullable: () => mapNullable,
  match: () => match,
  matchW: () => matchW,
  none: () => none2,
  of: () => of,
  option: () => option,
  orElse: () => orElse,
  partition: () => partition,
  partitionMap: () => partitionMap,
  reduce: () => reduce,
  reduceRight: () => reduceRight,
  separate: () => separate,
  sequence: () => sequence,
  sequenceArray: () => sequenceArray,
  some: () => some2,
  tap: () => tap2,
  tapEither: () => tapEither2,
  throwError: () => throwError,
  toNullable: () => toNullable,
  toUndefined: () => toUndefined,
  traverse: () => traverse,
  traverseArray: () => traverseArray,
  traverseArrayWithIndex: () => traverseArrayWithIndex,
  traverseReadonlyArrayWithIndex: () => traverseReadonlyArrayWithIndex,
  traverseReadonlyNonEmptyArrayWithIndex: () => traverseReadonlyNonEmptyArrayWithIndex,
  tryCatch: () => tryCatch,
  tryCatchK: () => tryCatchK,
  wilt: () => wilt,
  wither: () => wither,
  zero: () => zero
});
var none2 = none;
var some2 = some;
function fromPredicate2(predicate) {
  return (a) => predicate(a) ? some2(a) : none2;
}
var getLeft = (ma) => ma._tag === "Right" ? none2 : some2(ma.left);
var getRight = (ma) => ma._tag === "Left" ? none2 : some2(ma.right);
var _map = (fa, f) => pipe(fa, map(f));
var _ap = (fab, fa) => pipe(fab, ap(fa));
var _reduce = (fa, b, f) => pipe(fa, reduce(b, f));
var _foldMap = (M) => {
  const foldMapM = foldMap(M);
  return (fa, f) => pipe(fa, foldMapM(f));
};
var _reduceRight = (fa, b, f) => pipe(fa, reduceRight(b, f));
var _traverse = (F) => {
  const traverseF = traverse(F);
  return (ta, f) => pipe(ta, traverseF(f));
};
var _alt = (fa, that) => pipe(fa, alt(that));
var _filter = (fa, predicate) => pipe(fa, filter(predicate));
var _filterMap = (fa, f) => pipe(fa, filterMap(f));
var _extend = (wa, f) => pipe(wa, extend(f));
var _partition = (fa, predicate) => pipe(fa, partition(predicate));
var _partitionMap = (fa, f) => pipe(fa, partitionMap(f));
var URI = "Option";
var getShow = (S) => ({
  show: (ma) => isNone2(ma) ? "none" : `some(${S.show(ma.value)})`
});
var getEq = (E) => ({
  equals: (x, y) => x === y || (isNone2(x) ? isNone2(y) : isNone2(y) ? false : E.equals(x.value, y.value))
});
var getOrd = (O) => ({
  equals: getEq(O).equals,
  compare: (x, y) => x === y ? 0 : isSome2(x) ? isSome2(y) ? O.compare(x.value, y.value) : 1 : -1
});
var getMonoid = (S) => ({
  concat: (x, y) => isNone2(x) ? y : isNone2(y) ? x : some2(S.concat(x.value, y.value)),
  empty: none2
});
var map = (f) => (fa) => isNone2(fa) ? none2 : some2(f(fa.value));
var Functor = {
  URI,
  map: _map
};
var as2 = dual(2, as(Functor));
var asUnit2 = asUnit(Functor);
var of = some2;
var Pointed = {
  URI,
  of
};
var ap = (fa) => (fab) => isNone2(fab) ? none2 : isNone2(fa) ? none2 : some2(fab.value(fa.value));
var Apply = {
  URI,
  map: _map,
  ap: _ap
};
var Applicative = {
  URI,
  map: _map,
  ap: _ap,
  of
};
var flatMap = /* @__PURE__ */ dual(2, (ma, f) => isNone2(ma) ? none2 : f(ma.value));
var Chain = {
  URI,
  map: _map,
  ap: _ap,
  chain: flatMap
};
var chainRec = (a, f) => {
  let current = f(a);
  while (true) {
    if (isSome2(current) && isLeft(current.value)) {
      current = f(current.value.left);
      continue;
    }
    if (isNone(current)) {
      return current;
    }
    if (isRight(current.value)) {
      return some2(current.value.right);
    }
  }
};
var ChainRec = {
  ...Chain,
  chainRec
};
var Monad = {
  URI,
  map: _map,
  ap: _ap,
  of,
  chain: flatMap
};
var reduce = (b, f) => (fa) => isNone2(fa) ? b : f(b, fa.value);
var foldMap = (M) => (f) => (fa) => isNone2(fa) ? M.empty : f(fa.value);
var reduceRight = (b, f) => (fa) => isNone2(fa) ? b : f(fa.value, b);
var Foldable = {
  URI,
  reduce: _reduce,
  foldMap: _foldMap,
  reduceRight: _reduceRight
};
var orElse = dual(2, (self, that) => isNone2(self) ? that() : self);
var altW = orElse;
var alt = orElse;
var Alt = {
  URI,
  map: _map,
  alt: _alt
};
var zero = () => none2;
var Zero = {
  URI,
  zero
};
var guard2 = /* @__PURE__ */ guard(Zero, Pointed);
var Alternative = {
  URI,
  map: _map,
  ap: _ap,
  of,
  alt: _alt,
  zero
};
var extend = (f) => (wa) => isNone2(wa) ? none2 : some2(f(wa));
var Extend = {
  URI,
  map: _map,
  extend: _extend
};
var compact = /* @__PURE__ */ flatMap(identity);
var defaultSeparated = /* @__PURE__ */ separated(none2, none2);
var separate = (ma) => isNone2(ma) ? defaultSeparated : separated(getLeft(ma.value), getRight(ma.value));
var Compactable = {
  URI,
  compact,
  separate
};
var filter = (predicate) => (fa) => isNone2(fa) ? none2 : predicate(fa.value) ? fa : none2;
var filterMap = (f) => (fa) => isNone2(fa) ? none2 : f(fa.value);
var partition = (predicate) => (fa) => separated(_filter(fa, not(predicate)), _filter(fa, predicate));
var partitionMap = (f) => flow(map(f), separate);
var Filterable = {
  URI,
  map: _map,
  compact,
  separate,
  filter: _filter,
  filterMap: _filterMap,
  partition: _partition,
  partitionMap: _partitionMap
};
var traverse = (F) => (f) => (ta) => isNone2(ta) ? F.of(none2) : F.map(f(ta.value), some2);
var sequence = (F) => (ta) => isNone2(ta) ? F.of(none2) : F.map(ta.value, some2);
var Traversable = {
  URI,
  map: _map,
  reduce: _reduce,
  foldMap: _foldMap,
  reduceRight: _reduceRight,
  traverse: _traverse,
  sequence
};
var _wither = /* @__PURE__ */ witherDefault(Traversable, Compactable);
var _wilt = /* @__PURE__ */ wiltDefault(Traversable, Compactable);
var wither = (F) => {
  const _witherF = _wither(F);
  return (f) => (fa) => _witherF(fa, f);
};
var wilt = (F) => {
  const _wiltF = _wilt(F);
  return (f) => (fa) => _wiltF(fa, f);
};
var Witherable = {
  URI,
  map: _map,
  reduce: _reduce,
  foldMap: _foldMap,
  reduceRight: _reduceRight,
  traverse: _traverse,
  sequence,
  compact,
  separate,
  filter: _filter,
  filterMap: _filterMap,
  partition: _partition,
  partitionMap: _partitionMap,
  wither: _wither,
  wilt: _wilt
};
var throwError = () => none2;
var MonadThrow = {
  URI,
  map: _map,
  ap: _ap,
  of,
  chain: flatMap,
  throwError
};
var fromEither = getRight;
var FromEither = {
  URI,
  fromEither
};
var isSome2 = isSome;
var isNone2 = (fa) => fa._tag === "None";
var matchW = (onNone, onSome) => (ma) => isNone2(ma) ? onNone() : onSome(ma.value);
var foldW = matchW;
var match = matchW;
var fold = match;
var getOrElseW = (onNone) => (ma) => isNone2(ma) ? onNone() : ma.value;
var getOrElse = getOrElseW;
var flap2 = /* @__PURE__ */ flap(Functor);
var apFirst2 = /* @__PURE__ */ apFirst(Apply);
var apSecond2 = /* @__PURE__ */ apSecond(Apply);
var flatten = compact;
var tap2 = /* @__PURE__ */ dual(2, tap(Chain));
var tapEither2 = /* @__PURE__ */ dual(2, tapEither(FromEither, Chain));
var duplicate = /* @__PURE__ */ extend(identity);
var fromEitherK2 = /* @__PURE__ */ fromEitherK(FromEither);
var chainEitherK2 = /* @__PURE__ */ chainEitherK(FromEither, Chain);
var chainFirstEitherK = tapEither2;
var fromNullable = (a) => a == null ? none2 : some2(a);
var tryCatch = (f) => {
  try {
    return some2(f());
  } catch (e) {
    return none2;
  }
};
var tryCatchK = (f) => (...a) => tryCatch(() => f(...a));
var fromNullableK = (f) => flow(f, fromNullable);
var chainNullableK = (f) => (ma) => isNone2(ma) ? none2 : fromNullable(f(ma.value));
var toNullable = /* @__PURE__ */ match(constNull, identity);
var toUndefined = /* @__PURE__ */ match(constUndefined, identity);
function elem(E) {
  return (a, ma) => {
    if (ma === void 0) {
      const elemE = elem(E);
      return (ma2) => elemE(a, ma2);
    }
    return isNone2(ma) ? false : E.equals(a, ma.value);
  };
}
var exists = (predicate) => (ma) => isNone2(ma) ? false : predicate(ma.value);
var Do = /* @__PURE__ */ of(emptyRecord);
var bindTo2 = /* @__PURE__ */ bindTo(Functor);
var let_2 = /* @__PURE__ */ let_(Functor);
var bind2 = /* @__PURE__ */ bind(Chain);
var apS2 = /* @__PURE__ */ apS(Apply);
var ApT = /* @__PURE__ */ of(emptyReadonlyArray);
var do_2 = do_(Pointed, ChainRec);
var traverseReadonlyNonEmptyArrayWithIndex = (f) => (as4) => {
  const o = f(0, head(as4));
  if (isNone2(o)) {
    return none2;
  }
  const out = [o.value];
  for (let i = 1; i < as4.length; i++) {
    const o2 = f(i, as4[i]);
    if (isNone2(o2)) {
      return none2;
    }
    out.push(o2.value);
  }
  return some2(out);
};
var traverseReadonlyArrayWithIndex = (f) => {
  const g = traverseReadonlyNonEmptyArrayWithIndex(f);
  return (as4) => isNonEmpty(as4) ? g(as4) : ApT;
};
var traverseArrayWithIndex = traverseReadonlyArrayWithIndex;
var traverseArray = (f) => traverseReadonlyArrayWithIndex((_, a) => f(a));
var sequenceArray = /* @__PURE__ */ traverseArray(identity);
var chain = flatMap;
var chainFirst = tap2;
function getRefinement(getOption) {
  return (a) => isSome2(getOption(a));
}
var mapNullable = chainNullableK;
var option = {
  URI,
  map: _map,
  of,
  ap: _ap,
  chain: flatMap,
  reduce: _reduce,
  foldMap: _foldMap,
  reduceRight: _reduceRight,
  traverse: _traverse,
  sequence,
  zero,
  alt: _alt,
  extend: _extend,
  compact,
  separate,
  filter: _filter,
  filterMap: _filterMap,
  partition: _partition,
  partitionMap: _partitionMap,
  wither: _wither,
  wilt: _wilt,
  throwError
};
var getApplySemigroup2 = /* @__PURE__ */ getApplySemigroup(Apply);
var getApplyMonoid = /* @__PURE__ */ getApplicativeMonoid(Applicative);
var getFirstMonoid = () => getMonoid(first());
var getLastMonoid = () => getMonoid(last());

// src/ChainRec.ts
var tailRec = (startWith, f) => {
  let ab = f(startWith);
  while (ab._tag === "Left") {
    ab = f(ab.left);
  }
  return ab.right;
};
function forever(M) {
  return (ma) => M.chainRec(void 0, () => M.map(ma, left2));
}
function whileSome(M, Mn) {
  return (ma) => M.chainRec(
    Mn.empty,
    (v) => M.map(
      ma,
      fold(
        () => right2(v),
        (x) => left2(Mn.concat(v, x))
      )
    )
  );
}
function untilSome(M) {
  return (ma) => M.chainRec(void 0, () => M.map(ma, fromOption2(constUndefined)));
}
function chainRec2(M) {
  return (a, b, f) => M.chainRec(tuple(a, b), ([a2, b2]) => f(a2, b2));
}
function chainRec3(M) {
  return (a, b, c, f) => M.chainRec(tuple(a, b, c), ([a2, b2, c2]) => f(a2, b2, c2));
}
var tailRec2 = (ai, bi, f) => {
  let result = f(ai, bi);
  while (isLeft2(result)) {
    const [a, b] = result.left;
    result = f(a, b);
  }
  return result.right;
};
var tailRec3 = (ai, bi, ci, f) => {
  let result = f(ai, bi, ci);
  while (isLeft2(result)) {
    const [a, b, c] = result.left;
    result = f(a, b, c);
  }
  return result.right;
};
function do_(P, M) {
  return (yieldFunction) => {
    const iterator = yieldFunction(unwrap);
    const state = iterator.next();
    const go = (state2) => {
      if (state2.done) {
        return P.of(right2(state2.value));
      }
      return M.map(
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore -- Shouldn't be an error
        state2.value.value,
        (a) => left2(iterator.next(a))
      );
    };
    return M.chainRec(state, go);
  };
}
var SingletonGenerator = class _SingletonGenerator {
  constructor(value) {
    this.value = value;
  }
  finished = false;
  next(value) {
    if (this.finished) {
      return { done: true, value };
    }
    this.finished = true;
    return { done: false, value: this.value };
  }
  return(value) {
    return this.next(value);
  }
  throw(e) {
    throw e;
  }
  [Symbol.iterator]() {
    return new _SingletonGenerator(this.value);
  }
};
function kindGenerator(ma) {
  class KindGenerator {
    constructor(value) {
      this.value = value;
    }
    [Symbol.iterator]() {
      return new SingletonGenerator(this);
    }
  }
  return new KindGenerator(ma);
}
function unwrap(ma) {
  return kindGenerator(ma);
}

// src/Either.ts
var left2 = left;
var right2 = right;
var flatMap2 = /* @__PURE__ */ dual(
  2,
  (ma, f) => isLeft2(ma) ? ma : f(ma.right)
);
var _map2 = (fa, f) => pipe(fa, map2(f));
var _ap2 = (fab, fa) => pipe(fab, ap2(fa));
var _reduce2 = (fa, b, f) => pipe(fa, reduce2(b, f));
var _foldMap2 = (M) => (fa, f) => {
  const foldMapM = foldMap2(M);
  return pipe(fa, foldMapM(f));
};
var _reduceRight2 = (fa, b, f) => pipe(fa, reduceRight2(b, f));
var _traverse2 = (F) => {
  const traverseF = traverse2(F);
  return (ta, f) => pipe(ta, traverseF(f));
};
var _bimap = (fa, f, g) => pipe(fa, bimap(f, g));
var _mapLeft = (fa, f) => pipe(fa, mapLeft(f));
var _alt2 = (fa, that) => pipe(fa, alt2(that));
var _extend2 = (wa, f) => pipe(wa, extend2(f));
var URI2 = "Either";
var getShow2 = (SE, SA) => ({
  show: (ma) => isLeft2(ma) ? `left(${SE.show(ma.left)})` : `right(${SA.show(ma.right)})`
});
var getEq2 = (EL, EA) => ({
  equals: (x, y) => x === y || (isLeft2(x) ? isLeft2(y) && EL.equals(x.left, y.left) : isRight2(y) && EA.equals(x.right, y.right))
});
var getSemigroup = (S) => ({
  concat: (x, y) => isLeft2(y) ? x : isLeft2(x) ? y : right2(S.concat(x.right, y.right))
});
var getCompactable = (M) => {
  const empty = left2(M.empty);
  return {
    URI: URI2,
    _E: void 0,
    compact: (ma) => isLeft2(ma) ? ma : ma.right._tag === "None" ? empty : right2(ma.right.value),
    separate: (ma) => isLeft2(ma) ? separated(ma, ma) : isLeft2(ma.right) ? separated(right2(ma.right.left), empty) : separated(empty, right2(ma.right.right))
  };
};
var getFilterable = (M) => {
  const empty = left2(M.empty);
  const { compact: compact2, separate: separate2 } = getCompactable(M);
  const filter2 = (ma, predicate) => isLeft2(ma) ? ma : predicate(ma.right) ? ma : empty;
  const partition2 = (ma, p) => {
    return isLeft2(ma) ? separated(ma, ma) : p(ma.right) ? separated(empty, right2(ma.right)) : separated(right2(ma.right), empty);
  };
  return {
    URI: URI2,
    _E: void 0,
    map: _map2,
    compact: compact2,
    separate: separate2,
    filter: filter2,
    filterMap: (ma, f) => {
      if (isLeft2(ma)) {
        return ma;
      }
      const ob = f(ma.right);
      return ob._tag === "None" ? empty : right2(ob.value);
    },
    partition: partition2,
    partitionMap: (ma, f) => {
      if (isLeft2(ma)) {
        return separated(ma, ma);
      }
      const e = f(ma.right);
      return isLeft2(e) ? separated(right2(e.left), empty) : separated(empty, right2(e.right));
    }
  };
};
var getWitherable = (M) => {
  const F_ = getFilterable(M);
  const C = getCompactable(M);
  return {
    URI: URI2,
    _E: void 0,
    map: _map2,
    compact: F_.compact,
    separate: F_.separate,
    filter: F_.filter,
    filterMap: F_.filterMap,
    partition: F_.partition,
    partitionMap: F_.partitionMap,
    traverse: _traverse2,
    sequence: sequence2,
    reduce: _reduce2,
    foldMap: _foldMap2,
    reduceRight: _reduceRight2,
    wither: witherDefault(Traversable2, C),
    wilt: wiltDefault(Traversable2, C)
  };
};
var getApplicativeValidation = (SE) => ({
  URI: URI2,
  _E: void 0,
  map: _map2,
  ap: (fab, fa) => isLeft2(fab) ? isLeft2(fa) ? left2(SE.concat(fab.left, fa.left)) : fab : isLeft2(fa) ? fa : right2(fab.right(fa.right)),
  of: of2
});
var getAltValidation = (SE) => ({
  URI: URI2,
  _E: void 0,
  map: _map2,
  alt: (me, that) => {
    if (isRight2(me)) {
      return me;
    }
    const ea = that();
    return isLeft2(ea) ? left2(SE.concat(me.left, ea.left)) : ea;
  }
});
var map2 = (f) => (fa) => isLeft2(fa) ? fa : right2(f(fa.right));
var Functor2 = {
  URI: URI2,
  map: _map2
};
var as3 = dual(2, as(Functor2));
var asUnit3 = asUnit(Functor2);
var of2 = right2;
var Pointed2 = {
  URI: URI2,
  of: of2
};
var apW = (fa) => (fab) => isLeft2(fab) ? fab : isLeft2(fa) ? fa : right2(fab.right(fa.right));
var ap2 = apW;
var Apply2 = {
  URI: URI2,
  map: _map2,
  ap: _ap2
};
var Applicative2 = {
  URI: URI2,
  map: _map2,
  ap: _ap2,
  of: of2
};
var Chain2 = {
  URI: URI2,
  map: _map2,
  ap: _ap2,
  chain: flatMap2
};
var Monad2 = {
  URI: URI2,
  map: _map2,
  ap: _ap2,
  of: of2,
  chain: flatMap2
};
var reduce2 = (b, f) => (fa) => isLeft2(fa) ? b : f(b, fa.right);
var foldMap2 = (M) => (f) => (fa) => isLeft2(fa) ? M.empty : f(fa.right);
var reduceRight2 = (b, f) => (fa) => isLeft2(fa) ? b : f(fa.right, b);
var Foldable2 = {
  URI: URI2,
  reduce: _reduce2,
  foldMap: _foldMap2,
  reduceRight: _reduceRight2
};
var traverse2 = (F) => (f) => (ta) => isLeft2(ta) ? F.of(left2(ta.left)) : F.map(f(ta.right), right2);
var sequence2 = (F) => (ma) => {
  return isLeft2(ma) ? F.of(left2(ma.left)) : F.map(ma.right, right2);
};
var Traversable2 = {
  URI: URI2,
  map: _map2,
  reduce: _reduce2,
  foldMap: _foldMap2,
  reduceRight: _reduceRight2,
  traverse: _traverse2,
  sequence: sequence2
};
var bimap = (f, g) => (fa) => isLeft2(fa) ? left2(f(fa.left)) : right2(g(fa.right));
var mapLeft = (f) => (fa) => isLeft2(fa) ? left2(f(fa.left)) : fa;
var Bifunctor = {
  URI: URI2,
  bimap: _bimap,
  mapLeft: _mapLeft
};
var altW2 = (that) => (fa) => isLeft2(fa) ? that() : fa;
var alt2 = altW2;
var Alt2 = {
  URI: URI2,
  map: _map2,
  alt: _alt2
};
var extend2 = (f) => (wa) => isLeft2(wa) ? wa : right2(f(wa));
var Extend2 = {
  URI: URI2,
  map: _map2,
  extend: _extend2
};
var chainRec4 = (a, f) => tailRec(
  f(a),
  (e) => isLeft2(e) ? right2(left2(e.left)) : isLeft2(e.right) ? left2(f(e.right.left)) : right2(right2(e.right.right))
);
var ChainRec2 = {
  URI: URI2,
  map: _map2,
  ap: _ap2,
  chain: flatMap2,
  chainRec: chainRec4
};
var throwError2 = left2;
var MonadThrow2 = {
  URI: URI2,
  map: _map2,
  ap: _ap2,
  of: of2,
  chain: flatMap2,
  throwError: throwError2
};
var FromEither2 = {
  URI: URI2,
  fromEither: identity
};
var fromPredicate3 = /* @__PURE__ */ fromPredicate(FromEither2);
var fromOption2 = /* @__PURE__ */ fromOption(FromEither2);
var isLeft2 = isLeft;
var isRight2 = isRight;
var matchW2 = (onLeft, onRight) => (ma) => isLeft2(ma) ? onLeft(ma.left) : onRight(ma.right);
var foldW2 = matchW2;
var match2 = matchW2;
var fold2 = match2;
var getOrElseW2 = (onLeft) => (ma) => isLeft2(ma) ? onLeft(ma.left) : ma.right;
var getOrElse2 = getOrElseW2;
var flap3 = /* @__PURE__ */ flap(Functor2);
var apFirst3 = /* @__PURE__ */ apFirst(Apply2);
var apFirstW = apFirst3;
var apSecond3 = /* @__PURE__ */ apSecond(Apply2);
var apSecondW = apSecond3;
var tap3 = /* @__PURE__ */ dual(2, tap(Chain2));
var flattenW = /* @__PURE__ */ flatMap2(identity);
var flatten2 = flattenW;
var duplicate2 = /* @__PURE__ */ extend2(identity);
var fromOptionK2 = /* @__PURE__ */ fromOptionK(FromEither2);
var chainOptionK2 = /* @__PURE__ */ chainOptionK(
  FromEither2,
  Chain2
);
var chainOptionKW = chainOptionK2;
var _FromEither = {
  fromEither: FromEither2.fromEither
};
var liftNullable2 = /* @__PURE__ */ liftNullable(_FromEither);
var liftOption2 = /* @__PURE__ */ liftOption(_FromEither);
var _FlatMap = {
  flatMap: flatMap2
};
var flatMapNullable2 = /* @__PURE__ */ flatMapNullable(_FromEither, _FlatMap);
var flatMapOption2 = /* @__PURE__ */ flatMapOption(_FromEither, _FlatMap);
var filterOrElse2 = /* @__PURE__ */ filterOrElse(FromEither2, Chain2);
var filterOrElseW = filterOrElse2;
var swap = (ma) => isLeft2(ma) ? right2(ma.left) : left2(ma.right);
var orElseW = (onLeft) => (ma) => isLeft2(ma) ? onLeft(ma.left) : ma;
var orElse2 = orElseW;
var fromNullable2 = (e) => (a) => a == null ? left2(e) : right2(a);
var tryCatch2 = (f, onThrow) => {
  try {
    return right2(f());
  } catch (e) {
    return left2(onThrow(e));
  }
};
var tryCatchK2 = (f, onThrow) => (...a) => tryCatch2(() => f(...a), onThrow);
var fromNullableK2 = (e) => {
  const from = fromNullable2(e);
  return (f) => flow(f, from);
};
var chainNullableK2 = (e) => {
  const from = fromNullableK2(e);
  return (f) => flatMap2(from(f));
};
var toUnion = /* @__PURE__ */ foldW2(identity, identity);
function toError(e) {
  return e instanceof Error ? e : new Error(String(e));
}
function elem2(E) {
  return (a, ma) => {
    if (ma === void 0) {
      const elemE = elem2(E);
      return (ma2) => elemE(a, ma2);
    }
    return isLeft2(ma) ? false : E.equals(a, ma.right);
  };
}
var exists2 = (predicate) => (ma) => isLeft2(ma) ? false : predicate(ma.right);
var Do2 = /* @__PURE__ */ of2(emptyRecord);
var bindTo3 = /* @__PURE__ */ bindTo(Functor2);
var let_3 = /* @__PURE__ */ let_(Functor2);
var bind3 = /* @__PURE__ */ bind(Chain2);
var bindW = bind3;
var apS3 = /* @__PURE__ */ apS(Apply2);
var apSW = apS3;
var ApT2 = /* @__PURE__ */ of2(emptyReadonlyArray);
var do_3 = do_(Pointed2, ChainRec2);
var traverseReadonlyNonEmptyArrayWithIndex2 = (f) => (as4) => {
  const e = f(0, head(as4));
  if (isLeft2(e)) {
    return e;
  }
  const out = [e.right];
  for (let i = 1; i < as4.length; i++) {
    const e2 = f(i, as4[i]);
    if (isLeft2(e2)) {
      return e2;
    }
    out.push(e2.right);
  }
  return right2(out);
};
var traverseReadonlyArrayWithIndex2 = (f) => {
  const g = traverseReadonlyNonEmptyArrayWithIndex2(f);
  return (as4) => isNonEmpty(as4) ? g(as4) : ApT2;
};
var traverseArrayWithIndex2 = traverseReadonlyArrayWithIndex2;
var traverseArray2 = (f) => traverseReadonlyArrayWithIndex2((_, a) => f(a));
var sequenceArray2 = /* @__PURE__ */ traverseArray2(identity);
var chainW = flatMap2;
var chain2 = flatMap2;
var chainFirst2 = tap3;
var chainFirstW = tap3;
function parseJSON(s, onError) {
  return tryCatch2(() => JSON.parse(s), onError);
}
var stringifyJSON = (u, onError) => tryCatch2(() => {
  const s = JSON.stringify(u);
  if (typeof s !== "string") {
    throw new Error("Converting unsupported structure to JSON");
  }
  return s;
}, onError);
var either = {
  URI: URI2,
  map: _map2,
  of: of2,
  ap: _ap2,
  chain: flatMap2,
  reduce: _reduce2,
  foldMap: _foldMap2,
  reduceRight: _reduceRight2,
  traverse: _traverse2,
  sequence: sequence2,
  bimap: _bimap,
  mapLeft: _mapLeft,
  alt: _alt2,
  extend: _extend2,
  chainRec: chainRec4,
  throwError: throwError2
};
var getApplySemigroup3 = /* @__PURE__ */ getApplySemigroup(Apply2);
var getApplyMonoid2 = /* @__PURE__ */ getApplicativeMonoid(Applicative2);
var getValidationSemigroup = (SE, SA) => getApplySemigroup(getApplicativeValidation(SE))(SA);
var getValidationMonoid = (SE, MA) => getApplicativeMonoid(getApplicativeValidation(SE))(MA);
function getValidation(SE) {
  const ap3 = getApplicativeValidation(SE).ap;
  const alt3 = getAltValidation(SE).alt;
  return {
    URI: URI2,
    _E: void 0,
    map: _map2,
    of: of2,
    chain: flatMap2,
    bimap: _bimap,
    mapLeft: _mapLeft,
    reduce: _reduce2,
    foldMap: _foldMap2,
    reduceRight: _reduceRight2,
    extend: _extend2,
    traverse: _traverse2,
    sequence: sequence2,
    chainRec: chainRec4,
    throwError: throwError2,
    ap: ap3,
    alt: alt3
  };
}

export {
  left2 as left,
  right2 as right,
  flatMap2 as flatMap,
  URI2 as URI,
  getShow2 as getShow,
  getEq2 as getEq,
  getSemigroup,
  getCompactable,
  getFilterable,
  getWitherable,
  getApplicativeValidation,
  getAltValidation,
  map2 as map,
  Functor2 as Functor,
  as3 as as,
  asUnit3 as asUnit,
  of2 as of,
  Pointed2 as Pointed,
  apW,
  ap2 as ap,
  Apply2 as Apply,
  Applicative2 as Applicative,
  Chain2 as Chain,
  Monad2 as Monad,
  reduce2 as reduce,
  foldMap2 as foldMap,
  reduceRight2 as reduceRight,
  Foldable2 as Foldable,
  traverse2 as traverse,
  sequence2 as sequence,
  Traversable2 as Traversable,
  bimap,
  mapLeft,
  Bifunctor,
  altW2 as altW,
  alt2 as alt,
  Alt2 as Alt,
  extend2 as extend,
  Extend2 as Extend,
  ChainRec2 as ChainRec,
  throwError2 as throwError,
  MonadThrow2 as MonadThrow,
  FromEither2 as FromEither,
  fromPredicate3 as fromPredicate,
  fromOption2 as fromOption,
  isLeft2 as isLeft,
  isRight2 as isRight,
  matchW2 as matchW,
  foldW2 as foldW,
  match2 as match,
  fold2 as fold,
  getOrElseW2 as getOrElseW,
  getOrElse2 as getOrElse,
  flap3 as flap,
  apFirst3 as apFirst,
  apFirstW,
  apSecond3 as apSecond,
  apSecondW,
  tap3 as tap,
  flattenW,
  flatten2 as flatten,
  duplicate2 as duplicate,
  fromOptionK2 as fromOptionK,
  chainOptionK2 as chainOptionK,
  chainOptionKW,
  liftNullable2 as liftNullable,
  liftOption2 as liftOption,
  flatMapNullable2 as flatMapNullable,
  flatMapOption2 as flatMapOption,
  filterOrElse2 as filterOrElse,
  filterOrElseW,
  swap,
  orElseW,
  orElse2 as orElse,
  fromNullable2 as fromNullable,
  tryCatch2 as tryCatch,
  tryCatchK2 as tryCatchK,
  fromNullableK2 as fromNullableK,
  chainNullableK2 as chainNullableK,
  toUnion,
  toError,
  elem2 as elem,
  exists2 as exists,
  Do2 as Do,
  bindTo3 as bindTo,
  let_3 as let_,
  bind3 as bind,
  bindW,
  apS3 as apS,
  apSW,
  ApT2 as ApT,
  do_3 as do_,
  traverseReadonlyNonEmptyArrayWithIndex2 as traverseReadonlyNonEmptyArrayWithIndex,
  traverseReadonlyArrayWithIndex2 as traverseReadonlyArrayWithIndex,
  traverseArrayWithIndex2 as traverseArrayWithIndex,
  traverseArray2 as traverseArray,
  sequenceArray2 as sequenceArray,
  chainW,
  chain2 as chain,
  chainFirst2 as chainFirst,
  chainFirstW,
  parseJSON,
  stringifyJSON,
  either,
  getApplySemigroup3 as getApplySemigroup,
  getApplyMonoid2 as getApplyMonoid,
  getValidationSemigroup,
  getValidationMonoid,
  getValidation,
  Either_exports,
  none2 as none,
  some2 as some,
  fromPredicate2,
  getLeft,
  getRight,
  URI as URI2,
  getShow as getShow2,
  getEq as getEq2,
  getOrd,
  getMonoid,
  map as map2,
  Functor as Functor2,
  as2,
  asUnit2,
  of as of2,
  Pointed as Pointed2,
  ap as ap2,
  Apply as Apply2,
  Applicative as Applicative2,
  flatMap as flatMap2,
  Chain as Chain2,
  chainRec,
  ChainRec as ChainRec2,
  Monad as Monad2,
  reduce as reduce2,
  foldMap as foldMap2,
  reduceRight as reduceRight2,
  Foldable as Foldable2,
  orElse as orElse2,
  altW as altW2,
  alt as alt2,
  Alt as Alt2,
  zero,
  Zero,
  guard2 as guard,
  Alternative,
  extend as extend2,
  Extend as Extend2,
  compact,
  separate,
  Compactable,
  filter,
  filterMap,
  partition,
  partitionMap,
  Filterable,
  traverse as traverse2,
  sequence as sequence2,
  Traversable as Traversable2,
  wither,
  wilt,
  Witherable,
  throwError as throwError2,
  MonadThrow as MonadThrow2,
  fromEither,
  FromEither as FromEither2,
  isSome2 as isSome,
  isNone2 as isNone,
  matchW as matchW2,
  foldW as foldW2,
  match as match2,
  fold as fold2,
  getOrElseW as getOrElseW2,
  getOrElse as getOrElse2,
  flap2,
  apFirst2,
  apSecond2,
  flatten as flatten2,
  tap2,
  tapEither2 as tapEither,
  duplicate as duplicate2,
  fromEitherK2 as fromEitherK,
  chainEitherK2 as chainEitherK,
  chainFirstEitherK,
  fromNullable as fromNullable2,
  tryCatch as tryCatch2,
  tryCatchK as tryCatchK2,
  fromNullableK as fromNullableK2,
  chainNullableK as chainNullableK2,
  toNullable,
  toUndefined,
  elem as elem2,
  exists as exists2,
  Do as Do2,
  bindTo2,
  let_2,
  bind2,
  apS2,
  ApT as ApT2,
  do_2,
  traverseReadonlyNonEmptyArrayWithIndex as traverseReadonlyNonEmptyArrayWithIndex2,
  traverseReadonlyArrayWithIndex as traverseReadonlyArrayWithIndex2,
  traverseArrayWithIndex as traverseArrayWithIndex2,
  traverseArray as traverseArray2,
  sequenceArray as sequenceArray2,
  chain as chain2,
  chainFirst as chainFirst2,
  getRefinement,
  mapNullable,
  option,
  getApplySemigroup2,
  getApplyMonoid as getApplyMonoid2,
  getFirstMonoid,
  getLastMonoid,
  Option_exports,
  tailRec,
  forever,
  whileSome,
  untilSome,
  chainRec2,
  chainRec3,
  tailRec2,
  tailRec3,
  do_ as do_3,
  ChainRec_exports
};
