import {
  tap
} from "./chunk-MDNVYLGG.mjs";
import {
  isNone,
  left,
  right
} from "./chunk-WPJQNT3M.mjs";
import {
  flow
} from "./chunk-ZI34HWXZ.mjs";
import {
  __export
} from "./chunk-QK4PKNB3.mjs";

// src/FromEither.ts
var FromEither_exports = {};
__export(FromEither_exports, {
  chainEitherK: () => chainEitherK,
  chainFirstEitherK: () => chainFirstEitherK,
  chainOptionK: () => chainOptionK,
  filterOrElse: () => filterOrElse,
  fromEitherK: () => fromEitherK,
  fromOption: () => fromOption,
  fromOptionK: () => fromOptionK,
  fromPredicate: () => fromPredicate,
  tapEither: () => tapEither
});
function fromOption(F) {
  return (onNone) => (ma) => F.fromEither(isNone(ma) ? left(onNone()) : right(ma.value));
}
function fromPredicate(F) {
  return (predicate, onFalse) => (a) => F.fromEither(predicate(a) ? right(a) : left(onFalse(a)));
}
function fromOptionK(F) {
  const fromOptionF = fromOption(F);
  return (onNone) => {
    const from = fromOptionF(onNone);
    return (f) => flow(f, from);
  };
}
function chainOptionK(F, M) {
  const fromOptionKF = fromOptionK(F);
  return (onNone) => {
    const from = fromOptionKF(onNone);
    return (f) => (ma) => M.chain(ma, from(f));
  };
}
function fromEitherK(F) {
  return (f) => flow(f, F.fromEither);
}
function chainEitherK(F, M) {
  const fromEitherKF = fromEitherK(F);
  return (f) => (ma) => M.chain(ma, fromEitherKF(f));
}
function chainFirstEitherK(F, M) {
  const tapEitherM = tapEither(F, M);
  return (f) => (ma) => tapEitherM(ma, f);
}
function filterOrElse(F, M) {
  return (predicate, onFalse) => (ma) => M.chain(ma, (a) => F.fromEither(predicate(a) ? right(a) : left(onFalse(a))));
}
function tapEither(F, M) {
  const fromEither = fromEitherK(F);
  const tapM = tap(M);
  return (self, f) => tapM(self, fromEither(f));
}

export {
  fromOption,
  fromPredicate,
  fromOptionK,
  chainOptionK,
  fromEitherK,
  chainEitherK,
  chainFirstEitherK,
  filterOrElse,
  tapEither,
  FromEither_exports
};
