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

// src/ReadonlyTuple.ts
var ReadonlyTuple_exports = {};
__export(ReadonlyTuple_exports, {
  Bifunctor: () => Bifunctor,
  Comonad: () => Comonad,
  Foldable: () => Foldable,
  Functor: () => Functor,
  Semigroupoid: () => Semigroupoid,
  Traversable: () => Traversable,
  URI: () => URI,
  bimap: () => bimap,
  compose: () => compose,
  duplicate: () => duplicate,
  extend: () => extend,
  extract: () => extract,
  flap: () => flap2,
  foldMap: () => foldMap,
  fst: () => fst,
  getApplicative: () => getApplicative,
  getApply: () => getApply,
  getChain: () => getChain,
  getChainRec: () => getChainRec,
  getMonad: () => getMonad,
  map: () => map,
  mapFst: () => mapFst,
  mapLeft: () => mapLeft,
  mapSnd: () => mapSnd,
  readonlyTuple: () => readonlyTuple,
  reduce: () => reduce,
  reduceRight: () => reduceRight,
  sequence: () => sequence,
  snd: () => snd,
  swap: () => swap,
  traverse: () => traverse
});
function fst(ea) {
  return ea[0];
}
function snd(ea) {
  return ea[1];
}
var swap = (ea) => [snd(ea), fst(ea)];
function getApply(S) {
  return {
    URI,
    _E: void 0,
    map: _map,
    ap: (fab, fa) => [fst(fab)(fst(fa)), S.concat(snd(fab), snd(fa))]
  };
}
var of = (M) => (a) => {
  return [a, M.empty];
};
function getApplicative(M) {
  const A = getApply(M);
  return {
    URI,
    _E: void 0,
    map: _map,
    ap: A.ap,
    of: of(M)
  };
}
function getChain(S) {
  const A = getApply(S);
  return {
    URI,
    _E: void 0,
    map: _map,
    ap: A.ap,
    chain: (ma, f) => {
      const [b, s] = f(fst(ma));
      return [b, S.concat(snd(ma), s)];
    }
  };
}
function getMonad(M) {
  const C = getChain(M);
  return {
    URI,
    _E: void 0,
    map: _map,
    ap: C.ap,
    chain: C.chain,
    of: of(M)
  };
}
function getChainRec(M) {
  const chainRec = (a, f) => {
    let result = f(a);
    let acc = M.empty;
    let s = fst(result);
    while (s._tag === "Left") {
      acc = M.concat(acc, snd(result));
      result = f(s.left);
      s = fst(result);
    }
    return [s.right, M.concat(acc, snd(result))];
  };
  const C = getChain(M);
  return {
    URI,
    _E: void 0,
    map: _map,
    ap: C.ap,
    chain: C.chain,
    chainRec
  };
}
var _compose = (bc, ab) => pipe(bc, compose(ab));
var _map = (fa, f) => pipe(fa, mapFst(f));
var _bimap = (fa, f, g) => pipe(fa, bimap(f, g));
var _mapLeft = (fa, f) => pipe(fa, mapSnd(f));
var _extend = (wa, f) => pipe(wa, extend(f));
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 bimap = (f, g) => (fa) => [g(fst(fa)), f(snd(fa))];
var mapFst = (f) => (fa) => [
  f(fst(fa)),
  snd(fa)
];
var mapSnd = (f) => (fa) => [
  fst(fa),
  f(snd(fa))
];
var compose = (ab) => (bc) => [
  fst(bc),
  snd(ab)
];
var extend = (f) => (wa) => [
  f(wa),
  snd(wa)
];
var extract = fst;
var duplicate = /* @__PURE__ */ extend(identity);
var reduce = (b, f) => (fa) => f(b, fst(fa));
var foldMap = () => {
  return (f) => (fa) => f(fst(fa));
};
var reduceRight = (b, f) => (fa) => f(fst(fa), b);
var traverse = (F) => {
  return (f) => (ta) => F.map(f(fst(ta)), (b) => [b, snd(ta)]);
};
var sequence = (F) => (fas) => {
  return F.map(fst(fas), (a) => [a, snd(fas)]);
};
var URI = "ReadonlyTuple";
var Functor = {
  URI,
  map: _map
};
var flap2 = /* @__PURE__ */ flap(Functor);
var map = mapFst;
var mapLeft = mapSnd;
var Bifunctor = {
  URI,
  bimap: _bimap,
  mapLeft: _mapLeft
};
var Semigroupoid = {
  URI,
  compose: _compose
};
var Comonad = {
  URI,
  map: _map,
  extend: _extend,
  extract
};
var Foldable = {
  URI,
  reduce: _reduce,
  foldMap: _foldMap,
  reduceRight: _reduceRight
};
var Traversable = {
  URI,
  map: _map,
  reduce: _reduce,
  foldMap: _foldMap,
  reduceRight: _reduceRight,
  traverse: _traverse,
  sequence
};
var readonlyTuple = {
  URI,
  compose: _compose,
  map: _map,
  bimap: _bimap,
  mapLeft: _mapLeft,
  extract,
  extend: _extend,
  reduce: _reduce,
  foldMap: _foldMap,
  reduceRight: _reduceRight,
  traverse: _traverse,
  sequence
};

export {
  fst,
  snd,
  swap,
  getApply,
  getApplicative,
  getChain,
  getMonad,
  getChainRec,
  bimap,
  mapFst,
  mapSnd,
  compose,
  extend,
  extract,
  duplicate,
  reduce,
  foldMap,
  reduceRight,
  traverse,
  sequence,
  URI,
  Functor,
  flap2 as flap,
  map,
  mapLeft,
  Bifunctor,
  Semigroupoid,
  Comonad,
  Foldable,
  Traversable,
  readonlyTuple,
  ReadonlyTuple_exports
};
