import {
  do_3 as do_
} from "./chunk-3AL4EMTU.mjs";
import {
  bindTo,
  flap,
  let_
} from "./chunk-WKK6AYQI.mjs";
import {
  apFirst,
  apS,
  apSecond
} from "./chunk-ZMCLCMOU.mjs";
import {
  bind,
  tap
} from "./chunk-MDNVYLGG.mjs";
import {
  emptyReadonlyArray,
  head,
  isLeft,
  isNonEmpty
} from "./chunk-WPJQNT3M.mjs";
import {
  dual,
  identity,
  pipe
} from "./chunk-ZI34HWXZ.mjs";
import {
  __export
} from "./chunk-QK4PKNB3.mjs";

// src/State.ts
var State_exports = {};
__export(State_exports, {
  Applicative: () => Applicative,
  Apply: () => Apply,
  Chain: () => Chain,
  ChainRec: () => ChainRec,
  FromState: () => FromState,
  Functor: () => Functor,
  Monad: () => Monad,
  Pointed: () => Pointed,
  URI: () => URI,
  ap: () => ap,
  apFirst: () => apFirst2,
  apS: () => apS2,
  apSecond: () => apSecond2,
  bind: () => bind2,
  bindTo: () => bindTo2,
  chain: () => chain,
  chainFirst: () => chainFirst,
  chainRec: () => chainRec,
  do: () => do_2,
  evalState: () => evalState,
  evaluate: () => evaluate,
  execState: () => execState,
  execute: () => execute,
  flap: () => flap2,
  flatMap: () => flatMap,
  flatten: () => flatten,
  get: () => get,
  gets: () => gets,
  let: () => let_2,
  map: () => map,
  modify: () => modify,
  of: () => of,
  put: () => put,
  sequenceArray: () => sequenceArray,
  state: () => state,
  tap: () => tap2,
  traverseArray: () => traverseArray,
  traverseArrayWithIndex: () => traverseArrayWithIndex,
  traverseReadonlyArrayWithIndex: () => traverseReadonlyArrayWithIndex,
  traverseReadonlyNonEmptyArrayWithIndex: () => traverseReadonlyNonEmptyArrayWithIndex
});
var get = () => (s) => [s, s];
var put = (s) => () => [void 0, s];
var modify = (f) => (s) => [void 0, f(s)];
var gets = (f) => (s) => [f(s), s];
var _map = (fa, f) => pipe(fa, map(f));
var _ap = (fab, fa) => pipe(fab, ap(fa));
var map = (f) => (fa) => (s1) => {
  const [a, s2] = fa(s1);
  return [f(a), s2];
};
var ap = (fa) => (fab) => (s1) => {
  const [f, s2] = fab(s1);
  const [a, s3] = fa(s2);
  return [f(a), s3];
};
var of = (a) => (s) => [a, s];
var flatMap = /* @__PURE__ */ dual(
  2,
  (ma, f) => (s1) => {
    const [a, s2] = ma(s1);
    return f(a)(s2);
  }
);
var flatten = /* @__PURE__ */ flatMap(identity);
var URI = "State";
var Functor = {
  URI,
  map: _map
};
var flap2 = /* @__PURE__ */ flap(Functor);
var Pointed = {
  URI,
  of
};
var Apply = {
  URI,
  map: _map,
  ap: _ap
};
var apFirst2 = /* @__PURE__ */ apFirst(Apply);
var apSecond2 = /* @__PURE__ */ apSecond(Apply);
var Applicative = {
  URI,
  map: _map,
  ap: _ap,
  of
};
var Chain = {
  URI,
  map: _map,
  ap: _ap,
  chain: flatMap
};
var chainRec = (a, f) => (initialState) => {
  let [current, state2] = f(a)(initialState);
  while (isLeft(current)) {
    ;
    [current, state2] = f(current.left)(state2);
  }
  return [current.right, state2];
};
var ChainRec = {
  ...Chain,
  chainRec
};
var Monad = {
  URI,
  map: _map,
  ap: _ap,
  of,
  chain: flatMap
};
var tap2 = /* @__PURE__ */ dual(2, tap(Chain));
var FromState = {
  URI,
  fromState: identity
};
var evaluate = (s) => (ma) => ma(s)[0];
var execute = (s) => (ma) => ma(s)[1];
var bindTo2 = /* @__PURE__ */ bindTo(Functor);
var let_2 = /* @__PURE__ */ let_(Functor);
var bind2 = /* @__PURE__ */ bind(Chain);
var do_2 = do_(Pointed, ChainRec);
var apS2 = /* @__PURE__ */ apS(Apply);
var traverseReadonlyNonEmptyArrayWithIndex = (f) => (as) => (s) => {
  const [b, s2] = f(0, head(as))(s);
  const bs = [b];
  let out = s2;
  for (let i = 1; i < as.length; i++) {
    const [b2, s22] = f(i, as[i])(out);
    bs.push(b2);
    out = s22;
  }
  return [bs, out];
};
var traverseReadonlyArrayWithIndex = (f) => {
  const g = traverseReadonlyNonEmptyArrayWithIndex(f);
  return (as) => isNonEmpty(as) ? g(as) : of(emptyReadonlyArray);
};
var traverseArrayWithIndex = traverseReadonlyArrayWithIndex;
var traverseArray = (f) => traverseReadonlyArrayWithIndex((_, a) => f(a));
var sequenceArray = /* @__PURE__ */ traverseArray(identity);
var chain = flatMap;
var chainFirst = tap2;
var evalState = (ma, s) => ma(s)[0];
var execState = (ma, s) => ma(s)[1];
var state = Monad;

export {
  get,
  put,
  modify,
  gets,
  map,
  ap,
  of,
  flatMap,
  flatten,
  URI,
  Functor,
  flap2 as flap,
  Pointed,
  Apply,
  apFirst2 as apFirst,
  apSecond2 as apSecond,
  Applicative,
  Chain,
  chainRec,
  ChainRec,
  Monad,
  tap2 as tap,
  FromState,
  evaluate,
  execute,
  bindTo2 as bindTo,
  let_2 as let_,
  bind2 as bind,
  do_2 as do_,
  apS2 as apS,
  traverseReadonlyNonEmptyArrayWithIndex,
  traverseReadonlyArrayWithIndex,
  traverseArrayWithIndex,
  traverseArray,
  sequenceArray,
  chain,
  chainFirst,
  evalState,
  execState,
  state,
  State_exports
};
