import {
  constant,
  pipe
} from "./chunk-ZI34HWXZ.mjs";
import {
  __export
} from "./chunk-QK4PKNB3.mjs";

// src/Foldable.ts
var Foldable_exports = {};
__export(Foldable_exports, {
  foldM: () => foldM,
  foldMap: () => foldMap,
  getFoldableComposition: () => getFoldableComposition,
  intercalate: () => intercalate,
  reduce: () => reduce,
  reduceM: () => reduceM,
  reduceRight: () => reduceRight,
  toArray: () => toArray,
  toReadonlyArray: () => toReadonlyArray,
  traverse_: () => traverse_
});
function reduce(F, G) {
  return (b, f) => (fga) => F.reduce(fga, b, (b2, ga) => G.reduce(ga, b2, f));
}
function foldMap(F, G) {
  return (M) => {
    const foldMapF = F.foldMap(M);
    const foldMapG = G.foldMap(M);
    return (f) => (fga) => foldMapF(fga, (ga) => foldMapG(ga, f));
  };
}
function reduceRight(F, G) {
  return (b, f) => (fga) => F.reduceRight(fga, b, (ga, b2) => G.reduceRight(ga, b2, f));
}
function reduceM(M, F) {
  return (b, f) => (fa) => F.reduce(fa, M.of(b), (mb, a) => M.chain(mb, (b2) => f(b2, a)));
}
function intercalate(M, F) {
  return (middle, fm) => {
    const go = ({ init, acc }, x) => init ? { init: false, acc: x } : { init: false, acc: M.concat(M.concat(acc, middle), x) };
    return F.reduce(fm, { init: true, acc: M.empty }, go).acc;
  };
}
function toReadonlyArray(F) {
  return (fa) => F.reduce(fa, [], (acc, a) => {
    acc.push(a);
    return acc;
  });
}
function traverse_(M, F) {
  const applyFirst = (mu2, mb) => M.ap(M.map(mu2, constant), mb);
  const mu = M.of(void 0);
  return (fa, f) => F.reduce(fa, mu, (mu2, a) => applyFirst(mu2, f(a)));
}
function foldM(M, F) {
  return (fa, b, f) => F.reduce(fa, M.of(b), (mb, a) => M.chain(mb, (b2) => f(b2, a)));
}
var toArray = toReadonlyArray;
function getFoldableComposition(F, G) {
  const _reduce = reduce(F, G);
  const _foldMap = foldMap(F, G);
  const _reduceRight = reduceRight(F, G);
  return {
    reduce: (fga, b, f) => pipe(fga, _reduce(b, f)),
    foldMap: (M) => {
      const foldMapM = _foldMap(M);
      return (fga, f) => pipe(fga, foldMapM(f));
    },
    reduceRight: (fga, b, f) => pipe(fga, _reduceRight(b, f))
  };
}

export {
  reduce,
  foldMap,
  reduceRight,
  reduceM,
  intercalate,
  toReadonlyArray,
  traverse_,
  foldM,
  toArray,
  getFoldableComposition,
  Foldable_exports
};
