import {
  bindTo,
  flap,
  let_
} from "./chunk-WKK6AYQI.mjs";
import {
  max,
  min
} from "./chunk-CRY5FFSR.mjs";
import {
  getMonoid
} from "./chunk-KKQVF4Y5.mjs";
import {
  fromEquals
} from "./chunk-YQKF55QL.mjs";
import {
  apFirst,
  apS,
  apSecond
} from "./chunk-ZMCLCMOU.mjs";
import {
  bind,
  chainFirst
} from "./chunk-MDNVYLGG.mjs";
import {
  emptyReadonlyArray,
  emptyRecord,
  fromReadonlyNonEmptyArray,
  has,
  head,
  isNonEmpty,
  none,
  singleton,
  some,
  tail
} from "./chunk-WPJQNT3M.mjs";
import {
  SK,
  dual,
  flow,
  identity,
  pipe
} from "./chunk-ZI34HWXZ.mjs";
import {
  __export
} from "./chunk-QK4PKNB3.mjs";

// src/ReadonlyNonEmptyArray.ts
var ReadonlyNonEmptyArray_exports = {};
__export(ReadonlyNonEmptyArray_exports, {
  Alt: () => Alt,
  Applicative: () => Applicative,
  Apply: () => Apply,
  Chain: () => Chain,
  Comonad: () => Comonad,
  Do: () => Do,
  Foldable: () => Foldable,
  FoldableWithIndex: () => FoldableWithIndex,
  Functor: () => Functor,
  FunctorWithIndex: () => FunctorWithIndex,
  Monad: () => Monad,
  Pointed: () => Pointed,
  Traversable: () => Traversable,
  TraversableWithIndex: () => TraversableWithIndex,
  URI: () => URI,
  alt: () => alt,
  altW: () => altW,
  ap: () => ap,
  apFirst: () => apFirst2,
  apS: () => apS2,
  apSecond: () => apSecond2,
  append: () => append,
  appendW: () => appendW,
  bind: () => bind2,
  bindTo: () => bindTo2,
  chain: () => chain,
  chainFirst: () => chainFirst2,
  chainWithIndex: () => chainWithIndex,
  chop: () => chop,
  chunksOf: () => chunksOf,
  concat: () => concat,
  concatAll: () => concatAll,
  concatW: () => concatW,
  cons: () => cons,
  duplicate: () => duplicate,
  empty: () => empty,
  extend: () => extend,
  extract: () => extract,
  filter: () => filter,
  filterWithIndex: () => filterWithIndex,
  flap: () => flap2,
  flatMap: () => flatMap,
  flatten: () => flatten,
  fold: () => fold,
  foldMap: () => foldMap,
  foldMapWithIndex: () => foldMapWithIndex,
  fromArray: () => fromArray,
  fromReadonlyArray: () => fromReadonlyArray,
  getEq: () => getEq,
  getSemigroup: () => getSemigroup,
  getShow: () => getShow,
  getUnionSemigroup: () => getUnionSemigroup,
  group: () => group,
  groupBy: () => groupBy,
  groupSort: () => groupSort,
  head: () => head2,
  init: () => init,
  insertAt: () => insertAt,
  intercalate: () => intercalate,
  intersperse: () => intersperse,
  isNonEmpty: () => isNonEmpty2,
  isOutOfBound: () => isOutOfBound,
  last: () => last,
  let: () => let_2,
  makeBy: () => makeBy,
  map: () => map,
  mapWithIndex: () => mapWithIndex,
  matchLeft: () => matchLeft,
  matchRight: () => matchRight,
  max: () => max2,
  min: () => min2,
  modifyAt: () => modifyAt,
  modifyHead: () => modifyHead,
  modifyLast: () => modifyLast,
  of: () => of,
  prepend: () => prepend,
  prependAll: () => prependAll,
  prependToAll: () => prependToAll,
  prependW: () => prependW,
  range: () => range,
  readonlyNonEmptyArray: () => readonlyNonEmptyArray,
  reduce: () => reduce,
  reduceRight: () => reduceRight,
  reduceRightWithIndex: () => reduceRightWithIndex,
  reduceWithIndex: () => reduceWithIndex,
  replicate: () => replicate,
  reverse: () => reverse,
  rotate: () => rotate,
  sequence: () => sequence,
  snoc: () => snoc,
  sort: () => sort,
  sortBy: () => sortBy,
  splitAt: () => splitAt,
  tail: () => tail2,
  traverse: () => traverse,
  traverseWithIndex: () => traverseWithIndex,
  unappend: () => unappend,
  uncons: () => uncons,
  union: () => union,
  uniq: () => uniq,
  unprepend: () => unprepend,
  unsafeInsertAt: () => unsafeInsertAt,
  unsafeUpdateAt: () => unsafeUpdateAt,
  unsnoc: () => unsnoc,
  unzip: () => unzip,
  updateAt: () => updateAt,
  updateHead: () => updateHead,
  updateLast: () => updateLast,
  zip: () => zip,
  zipWith: () => zipWith
});
var empty = emptyReadonlyArray;
var isNonEmpty2 = isNonEmpty;
var isOutOfBound = (i, as) => i < 0 || i >= as.length;
var prependW = (head3) => (tail3) => [head3, ...tail3];
var prepend = prependW;
var appendW = (end) => (init2) => [...init2, end];
var append = appendW;
var unsafeInsertAt = (i, a, as) => {
  if (isNonEmpty2(as)) {
    const xs = fromReadonlyNonEmptyArray(as);
    xs.splice(i, 0, a);
    return xs;
  }
  return [a];
};
var unsafeUpdateAt = (i, a, as) => {
  if (as[i] === a) {
    return as;
  } else {
    const xs = fromReadonlyNonEmptyArray(as);
    xs[i] = a;
    return xs;
  }
};
var uniq = (E) => (as) => {
  if (as.length === 1) {
    return as;
  }
  const out = [head2(as)];
  const rest = tail2(as);
  for (const a of rest) {
    if (out.every((o) => !E.equals(o, a))) {
      out.push(a);
    }
  }
  return out;
};
var sortBy = (ords) => {
  if (isNonEmpty2(ords)) {
    const M = getMonoid();
    return sort(ords.reduce(M.concat, M.empty));
  }
  return identity;
};
var union = (E) => {
  const uniqE = uniq(E);
  return (second) => (first) => uniqE(pipe(first, concat(second)));
};
var rotate = (n) => (as) => {
  const len = as.length;
  const m = Math.round(n) % len;
  if (isOutOfBound(Math.abs(m), as) || m === 0) {
    return as;
  }
  if (m < 0) {
    const [f, s] = splitAt(-m)(as);
    return pipe(s, concat(f));
  } else {
    return rotate(m - len)(as);
  }
};
var fromReadonlyArray = (as) => isNonEmpty2(as) ? some(as) : none;
var makeBy = (f) => (n) => {
  const j = Math.max(0, Math.floor(n));
  const out = [f(0)];
  for (let i = 1; i < j; i++) {
    out.push(f(i));
  }
  return out;
};
var replicate = (a) => makeBy(() => a);
var range = (start, end) => start <= end ? makeBy((i) => start + i)(end - start + 1) : [start];
var unprepend = (as) => [head2(as), tail2(as)];
var unappend = (as) => [init(as), last(as)];
var fromArray = (as) => fromReadonlyArray(as.slice());
function concatW(second) {
  return (first) => first.concat(second);
}
function concat(x, y) {
  return y ? x.concat(y) : (y2) => y2.concat(x);
}
var reverse = (as) => as.length === 1 ? as : [last(as), ...as.slice(0, -1).reverse()];
function group(E) {
  return (as) => {
    const len = as.length;
    if (len === 0) {
      return empty;
    }
    const out = [];
    let head3 = as[0];
    let nea = [head3];
    for (let i = 1; i < len; i++) {
      const a = as[i];
      if (E.equals(a, head3)) {
        nea.push(a);
      } else {
        out.push(nea);
        head3 = a;
        nea = [head3];
      }
    }
    out.push(nea);
    return out;
  };
}
var groupBy = (f) => (as) => {
  const out = {};
  for (const a of as) {
    const k = f(a);
    if (has.call(out, k)) {
      out[k].push(a);
    } else {
      out[k] = [a];
    }
  }
  return out;
};
var sort = (O) => (as) => as.length === 1 ? as : as.slice().sort(O.compare);
var updateAt = (i, a) => modifyAt(i, () => a);
var modifyAt = (i, f) => (as) => isOutOfBound(i, as) ? none : some(unsafeUpdateAt(i, f(as[i]), as));
var zipWith = (as, bs, f) => {
  const cs = [f(as[0], bs[0])];
  const len = Math.min(as.length, bs.length);
  for (let i = 1; i < len; i++) {
    cs[i] = f(as[i], bs[i]);
  }
  return cs;
};
function zip(as, bs) {
  if (bs === void 0) {
    return (bs2) => zip(bs2, as);
  }
  return zipWith(as, bs, (a, b) => [a, b]);
}
var unzip = (abs) => {
  const fa = [abs[0][0]];
  const fb = [abs[0][1]];
  for (let i = 1; i < abs.length; i++) {
    fa[i] = abs[i][0];
    fb[i] = abs[i][1];
  }
  return [fa, fb];
};
var prependAll = (middle) => (as) => {
  const out = [middle, as[0]];
  for (let i = 1; i < as.length; i++) {
    out.push(middle, as[i]);
  }
  return out;
};
var intersperse = (middle) => (as) => {
  const rest = tail2(as);
  return isNonEmpty2(rest) ? pipe(rest, prependAll(middle), prepend(head2(as))) : as;
};
var chainWithIndex = (f) => (as) => {
  const out = fromReadonlyNonEmptyArray(f(0, head2(as)));
  for (let i = 1; i < as.length; i++) {
    out.push(...f(i, as[i]));
  }
  return out;
};
var chop = (f) => (as) => {
  const [b, rest] = f(as);
  const out = [b];
  let next = rest;
  while (isNonEmpty2(next)) {
    const [b2, rest2] = f(next);
    out.push(b2);
    next = rest2;
  }
  return out;
};
var splitAt = (n) => (as) => {
  const m = Math.max(1, n);
  return m >= as.length ? [as, empty] : [pipe(as.slice(1, m), prepend(head2(as))), as.slice(m)];
};
var chunksOf = (n) => chop(splitAt(n));
var _map = (fa, f) => pipe(fa, map(f));
var _mapWithIndex = (fa, f) => pipe(fa, mapWithIndex(f));
var _ap = (fab, fa) => pipe(fab, ap(fa));
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 _alt = (fa, that) => pipe(fa, alt(that));
var _reduceWithIndex = (fa, b, f) => pipe(fa, reduceWithIndex(b, f));
var _foldMapWithIndex = (M) => {
  const foldMapWithIndexM = foldMapWithIndex(M);
  return (fa, f) => pipe(fa, foldMapWithIndexM(f));
};
var _reduceRightWithIndex = (fa, b, f) => pipe(fa, reduceRightWithIndex(b, f));
var _traverseWithIndex = (F) => {
  const traverseWithIndexF = traverseWithIndex(F);
  return (ta, f) => pipe(ta, traverseWithIndexF(f));
};
var of = singleton;
var altW = (that) => (as) => pipe(as, concatW(that()));
var alt = altW;
var ap = (as) => flatMap((f) => pipe(as, map(f)));
var flatMap = /* @__PURE__ */ dual(
  2,
  (ma, f) => pipe(
    ma,
    chainWithIndex((i, a) => f(a, i))
  )
);
var extend = (f) => (as) => {
  let next = tail2(as);
  const out = [f(as)];
  while (isNonEmpty2(next)) {
    out.push(f(next));
    next = tail2(next);
  }
  return out;
};
var duplicate = /* @__PURE__ */ extend(identity);
var flatten = /* @__PURE__ */ flatMap(identity);
var map = (f) => mapWithIndex((_, a) => f(a));
var mapWithIndex = (f) => (as) => {
  const out = [f(0, head2(as))];
  for (let i = 1; i < as.length; i++) {
    out.push(f(i, as[i]));
  }
  return out;
};
var reduce = (b, f) => reduceWithIndex(b, (_, b2, a) => f(b2, a));
var foldMap = (S) => (f) => (as) => as.slice(1).reduce((s, a) => S.concat(s, f(a)), f(as[0]));
var reduceRight = (b, f) => reduceRightWithIndex(b, (_, b2, a) => f(b2, a));
var reduceWithIndex = (b, f) => (as) => as.reduce((b2, a, i) => f(i, b2, a), b);
var foldMapWithIndex = (S) => (f) => (as) => as.slice(1).reduce((s, a, i) => S.concat(s, f(i + 1, a)), f(0, as[0]));
var reduceRightWithIndex = (b, f) => (as) => as.reduceRight((b2, a, i) => f(i, a, b2), b);
var traverse = (F) => {
  const traverseWithIndexF = traverseWithIndex(F);
  return (f) => traverseWithIndexF((_, a) => f(a));
};
var sequence = (F) => traverseWithIndex(F)(SK);
var traverseWithIndex = (F) => (f) => (as) => {
  let out = F.map(f(0, head2(as)), of);
  for (let i = 1; i < as.length; i++) {
    out = F.ap(
      F.map(out, (bs) => (b) => pipe(bs, append(b))),
      f(i, as[i])
    );
  }
  return out;
};
var extract = head;
var URI = "ReadonlyNonEmptyArray";
var getShow = (S) => ({
  show: (as) => `[${as.map(S.show).join(", ")}]`
});
var getSemigroup = () => ({
  concat
});
var getEq = (E) => fromEquals((xs, ys) => xs.length === ys.length && xs.every((x, i) => E.equals(x, ys[i])));
var getUnionSemigroup = (E) => {
  const unionE = union(E);
  return {
    concat: (first, second) => unionE(second)(first)
  };
};
var Functor = {
  URI,
  map: _map
};
var flap2 = /* @__PURE__ */ flap(Functor);
var Pointed = {
  URI,
  of
};
var FunctorWithIndex = {
  URI,
  map: _map,
  mapWithIndex: _mapWithIndex
};
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 chainFirst2 = /* @__PURE__ */ chainFirst(Chain);
var Monad = {
  URI,
  map: _map,
  ap: _ap,
  of,
  chain: flatMap
};
var Foldable = {
  URI,
  reduce: _reduce,
  foldMap: _foldMap,
  reduceRight: _reduceRight
};
var FoldableWithIndex = {
  URI,
  reduce: _reduce,
  foldMap: _foldMap,
  reduceRight: _reduceRight,
  reduceWithIndex: _reduceWithIndex,
  foldMapWithIndex: _foldMapWithIndex,
  reduceRightWithIndex: _reduceRightWithIndex
};
var Traversable = {
  URI,
  map: _map,
  reduce: _reduce,
  foldMap: _foldMap,
  reduceRight: _reduceRight,
  traverse: _traverse,
  sequence
};
var TraversableWithIndex = {
  URI,
  map: _map,
  mapWithIndex: _mapWithIndex,
  reduce: _reduce,
  foldMap: _foldMap,
  reduceRight: _reduceRight,
  traverse: _traverse,
  sequence,
  reduceWithIndex: _reduceWithIndex,
  foldMapWithIndex: _foldMapWithIndex,
  reduceRightWithIndex: _reduceRightWithIndex,
  traverseWithIndex: _traverseWithIndex
};
var Alt = {
  URI,
  map: _map,
  alt: _alt
};
var Comonad = {
  URI,
  map: _map,
  extend: _extend,
  extract
};
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 head2 = extract;
var tail2 = tail;
var last = (as) => as[as.length - 1];
var init = (as) => as.slice(0, -1);
var min2 = (O) => {
  const S = min(O);
  return (as) => as.reduce(S.concat);
};
var max2 = (O) => {
  const S = max(O);
  return (as) => as.reduce(S.concat);
};
var concatAll = (S) => (as) => as.reduce(S.concat);
var matchLeft = (f) => (as) => f(head2(as), tail2(as));
var matchRight = (f) => (as) => f(init(as), last(as));
var modifyHead = (f) => (as) => [f(head2(as)), ...tail2(as)];
var updateHead = (a) => modifyHead(() => a);
var modifyLast = (f) => (as) => pipe(init(as), append(f(last(as))));
var updateLast = (a) => modifyLast(() => a);
var intercalate = (S) => {
  const concatAllS = concatAll(S);
  return (middle) => flow(intersperse(middle), concatAllS);
};
var chain = flatMap;
function groupSort(O) {
  const sortO = sort(O);
  const groupO = group(O);
  return (as) => isNonEmpty2(as) ? groupO(sortO(as)) : empty;
}
function filter(predicate) {
  return filterWithIndex((_, a) => predicate(a));
}
var filterWithIndex = (predicate) => (as) => fromReadonlyArray(as.filter((a, i) => predicate(i, a)));
var uncons = unprepend;
var unsnoc = unappend;
function cons(head3, tail3) {
  return tail3 === void 0 ? prepend(head3) : pipe(tail3, prepend(head3));
}
var snoc = (init2, end) => pipe(init2, concat([end]));
var insertAt = (i, a) => (as) => i < 0 || i > as.length ? none : some(unsafeInsertAt(i, a, as));
var prependToAll = prependAll;
var fold = concatAll;
var readonlyNonEmptyArray = {
  URI,
  of,
  map: _map,
  mapWithIndex: _mapWithIndex,
  ap: _ap,
  chain: flatMap,
  extend: _extend,
  extract,
  reduce: _reduce,
  foldMap: _foldMap,
  reduceRight: _reduceRight,
  traverse: _traverse,
  sequence,
  reduceWithIndex: _reduceWithIndex,
  foldMapWithIndex: _foldMapWithIndex,
  reduceRightWithIndex: _reduceRightWithIndex,
  traverseWithIndex: _traverseWithIndex,
  alt: _alt
};

export {
  empty,
  isNonEmpty2 as isNonEmpty,
  isOutOfBound,
  prependW,
  prepend,
  appendW,
  append,
  unsafeInsertAt,
  unsafeUpdateAt,
  uniq,
  sortBy,
  union,
  rotate,
  fromReadonlyArray,
  makeBy,
  replicate,
  range,
  unprepend,
  unappend,
  fromArray,
  concatW,
  concat,
  reverse,
  group,
  groupBy,
  sort,
  updateAt,
  modifyAt,
  zipWith,
  zip,
  unzip,
  prependAll,
  intersperse,
  chainWithIndex,
  chop,
  splitAt,
  chunksOf,
  of,
  altW,
  alt,
  ap,
  flatMap,
  extend,
  duplicate,
  flatten,
  map,
  mapWithIndex,
  reduce,
  foldMap,
  reduceRight,
  reduceWithIndex,
  foldMapWithIndex,
  reduceRightWithIndex,
  traverse,
  sequence,
  traverseWithIndex,
  extract,
  URI,
  getShow,
  getSemigroup,
  getEq,
  getUnionSemigroup,
  Functor,
  flap2 as flap,
  Pointed,
  FunctorWithIndex,
  Apply,
  apFirst2 as apFirst,
  apSecond2 as apSecond,
  Applicative,
  Chain,
  chainFirst2 as chainFirst,
  Monad,
  Foldable,
  FoldableWithIndex,
  Traversable,
  TraversableWithIndex,
  Alt,
  Comonad,
  Do,
  bindTo2 as bindTo,
  let_2 as let_,
  bind2 as bind,
  apS2 as apS,
  head2 as head,
  tail2 as tail,
  last,
  init,
  min2 as min,
  max2 as max,
  concatAll,
  matchLeft,
  matchRight,
  modifyHead,
  updateHead,
  modifyLast,
  updateLast,
  intercalate,
  chain,
  groupSort,
  filter,
  filterWithIndex,
  uncons,
  unsnoc,
  cons,
  snoc,
  insertAt,
  prependToAll,
  fold,
  readonlyNonEmptyArray,
  ReadonlyNonEmptyArray_exports
};
