import {
  concatAll,
  foldMap,
  foldMapWithIndex,
  getEq,
  getShow,
  head,
  intercalate,
  last,
  max,
  min,
  reduce,
  reduceRight,
  reduceRightWithIndex,
  reduceWithIndex
} from "./chunk-RGZW4IJC.mjs";
import {
  bindTo,
  flap,
  let_
} from "./chunk-WKK6AYQI.mjs";
import {
  getMonoid
} from "./chunk-KKQVF4Y5.mjs";
import {
  apFirst,
  apS,
  apSecond
} from "./chunk-ZMCLCMOU.mjs";
import {
  bind,
  chainFirst
} from "./chunk-MDNVYLGG.mjs";
import {
  emptyRecord,
  fromReadonlyNonEmptyArray,
  has,
  none,
  some
} from "./chunk-WPJQNT3M.mjs";
import {
  dual,
  identity,
  pipe
} from "./chunk-ZI34HWXZ.mjs";
import {
  __export
} from "./chunk-QK4PKNB3.mjs";

// src/NonEmptyArray.ts
var NonEmptyArray_exports = {};
__export(NonEmptyArray_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: () => concatAll2,
  concatW: () => concatW,
  cons: () => cons,
  copy: () => copy,
  duplicate: () => duplicate,
  extend: () => extend,
  extract: () => extract,
  filter: () => filter,
  filterWithIndex: () => filterWithIndex,
  flap: () => flap2,
  flatMap: () => flatMap,
  flatten: () => flatten,
  fold: () => fold,
  foldMap: () => foldMap2,
  foldMapWithIndex: () => foldMapWithIndex2,
  fromArray: () => fromArray,
  fromReadonlyNonEmptyArray: () => fromReadonlyNonEmptyArray2,
  getEq: () => getEq2,
  getSemigroup: () => getSemigroup,
  getShow: () => getShow2,
  getUnionSemigroup: () => getUnionSemigroup,
  group: () => group,
  groupBy: () => groupBy,
  groupSort: () => groupSort,
  head: () => head2,
  init: () => init,
  insertAt: () => insertAt,
  intercalate: () => intercalate2,
  intersperse: () => intersperse,
  isNonEmpty: () => isNonEmpty,
  isOutOfBound: () => isOutOfBound,
  last: () => last2,
  let: () => let_2,
  makeBy: () => makeBy,
  map: () => map,
  mapWithIndex: () => mapWithIndex,
  matchLeft: () => matchLeft,
  matchRight: () => matchRight,
  max: () => max2,
  min: () => min2,
  modifyAt: () => modifyAt,
  modifyHead: () => modifyHead,
  modifyLast: () => modifyLast,
  nonEmptyArray: () => nonEmptyArray,
  of: () => of,
  prepend: () => prepend,
  prependAll: () => prependAll,
  prependToAll: () => prependToAll,
  prependW: () => prependW,
  range: () => range,
  reduce: () => reduce2,
  reduceRight: () => reduceRight2,
  reduceRightWithIndex: () => reduceRightWithIndex2,
  reduceWithIndex: () => reduceWithIndex2,
  replicate: () => replicate,
  reverse: () => reverse,
  rotate: () => rotate,
  sequence: () => sequence,
  snoc: () => snoc,
  sort: () => sort,
  sortBy: () => sortBy,
  splitAt: () => splitAt,
  tail: () => tail,
  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 isNonEmpty = (as) => as.length > 0;
var isOutOfBound = (i, as) => i < 0 || i >= as.length;
var prependW = (head3) => (tail2) => [head3, ...tail2];
var prepend = prependW;
var appendW = (end) => (init2) => [...init2, end];
var append = appendW;
var unsafeInsertAt = (i, a, as) => {
  if (isNonEmpty(as)) {
    const xs = fromReadonlyNonEmptyArray2(as);
    xs.splice(i, 0, a);
    return xs;
  }
  return [a];
};
var unsafeUpdateAt = (i, a, as) => {
  const xs = fromReadonlyNonEmptyArray2(as);
  xs[i] = a;
  return xs;
};
var uniq = (E) => (as) => {
  if (as.length === 1) {
    return copy(as);
  }
  const out = [head2(as)];
  const rest = tail(as);
  for (const a of rest) {
    if (out.every((o) => !E.equals(o, a))) {
      out.push(a);
    }
  }
  return out;
};
var sortBy = (ords) => {
  if (isNonEmpty(ords)) {
    const M = getMonoid();
    return sort(ords.reduce(M.concat, M.empty));
  }
  return copy;
};
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 copy(as);
  }
  if (m < 0) {
    const [f, s] = splitAt(-m)(as);
    return pipe(s, concat(f));
  } else {
    return rotate(m - len)(as);
  }
};
var fromReadonlyNonEmptyArray2 = fromReadonlyNonEmptyArray;
var fromArray = (as) => isNonEmpty(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), tail(as)];
var unappend = (as) => [init(as), last2(as)];
function concatW(second) {
  return (first) => first.concat(second);
}
function concat(x, y) {
  return y ? x.concat(y) : (y2) => y2.concat(x);
}
var reverse = (as) => [last2(as), ...as.slice(0, -1).reverse()];
function group(E) {
  return (as) => {
    const len = as.length;
    if (len === 0) {
      return [];
    }
    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.slice().sort(O.compare);
var insertAt = (i, a) => (as) => i < 0 || i > as.length ? none : some(unsafeInsertAt(i, a, as));
var updateAt = (i, a) => modifyAt(i, () => a);
var modifyAt = (i, f) => (as) => isOutOfBound(i, as) ? none : some(unsafeUpdateAt(i, f(as[i]), as));
var copy = fromReadonlyNonEmptyArray2;
var of = (a) => [a];
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 = tail(as);
  return isNonEmpty(rest) ? pipe(rest, prependAll(middle), prepend(head2(as))) : copy(as);
};
var foldMapWithIndex2 = foldMapWithIndex;
var foldMap2 = foldMap;
var chainWithIndex = (f) => (as) => {
  const out = fromReadonlyNonEmptyArray2(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 (isNonEmpty(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 ? [copy(as), []] : [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, reduce2(b, f));
var _foldMap = (M) => {
  const foldMapM = foldMap2(M);
  return (fa, f) => pipe(fa, foldMapM(f));
};
var _reduceRight = (fa, b, f) => pipe(fa, reduceRight2(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, reduceWithIndex2(b, f));
var _foldMapWithIndex = (M) => {
  const foldMapWithIndexM = foldMapWithIndex2(M);
  return (fa, f) => pipe(fa, foldMapWithIndexM(f));
};
var _reduceRightWithIndex = (fa, b, f) => pipe(fa, reduceRightWithIndex2(b, f));
var _traverseWithIndex = (F) => {
  const traverseWithIndexF = traverseWithIndex(F);
  return (ta, f) => pipe(ta, traverseWithIndexF(f));
};
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 = tail(as);
  const out = [f(as)];
  while (isNonEmpty(next)) {
    out.push(f(next));
    next = tail(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 reduce2 = reduce;
var reduceWithIndex2 = reduceWithIndex;
var reduceRight2 = reduceRight;
var reduceRightWithIndex2 = reduceRightWithIndex;
var traverse = (F) => {
  const traverseWithIndexF = traverseWithIndex(F);
  return (f) => traverseWithIndexF((_, a) => f(a));
};
var sequence = (F) => traverseWithIndex(F)((_, a) => a);
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 = "NonEmptyArray";
var getShow2 = getShow;
var getSemigroup = () => ({
  concat
});
var getEq2 = getEq;
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 = head;
var tail = (as) => as.slice(1);
var last2 = last;
var init = (as) => as.slice(0, -1);
var min2 = min;
var max2 = max;
var concatAll2 = (S) => (as) => as.reduce(S.concat);
var matchLeft = (f) => (as) => f(head2(as), tail(as));
var matchRight = (f) => (as) => f(init(as), last2(as));
var modifyHead = (f) => (as) => [f(head2(as)), ...tail(as)];
var updateHead = (a) => modifyHead(() => a);
var modifyLast = (f) => (as) => pipe(init(as), append(f(last2(as))));
var updateLast = (a) => modifyLast(() => a);
var intercalate2 = intercalate;
var chain = flatMap;
function groupSort(O) {
  const sortO = sort(O);
  const groupO = group(O);
  return (as) => isNonEmpty(as) ? groupO(sortO(as)) : [];
}
function filter(predicate) {
  return filterWithIndex((_, a) => predicate(a));
}
var filterWithIndex = (predicate) => (as) => fromArray(as.filter((a, i) => predicate(i, a)));
var uncons = unprepend;
var unsnoc = unappend;
function cons(head3, tail2) {
  return tail2 === void 0 ? prepend(head3) : pipe(tail2, prepend(head3));
}
var snoc = (init2, end) => pipe(init2, append(end));
var prependToAll = prependAll;
var fold = concatAll;
var nonEmptyArray = {
  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 {
  isNonEmpty,
  isOutOfBound,
  prependW,
  prepend,
  appendW,
  append,
  unsafeInsertAt,
  unsafeUpdateAt,
  uniq,
  sortBy,
  union,
  rotate,
  fromReadonlyNonEmptyArray2 as fromReadonlyNonEmptyArray,
  fromArray,
  makeBy,
  replicate,
  range,
  unprepend,
  unappend,
  concatW,
  concat,
  reverse,
  group,
  groupBy,
  sort,
  insertAt,
  updateAt,
  modifyAt,
  copy,
  of,
  zipWith,
  zip,
  unzip,
  prependAll,
  intersperse,
  foldMapWithIndex2 as foldMapWithIndex,
  foldMap2 as foldMap,
  chainWithIndex,
  chop,
  splitAt,
  chunksOf,
  altW,
  alt,
  ap,
  flatMap,
  extend,
  duplicate,
  flatten,
  map,
  mapWithIndex,
  reduce2 as reduce,
  reduceWithIndex2 as reduceWithIndex,
  reduceRight2 as reduceRight,
  reduceRightWithIndex2 as reduceRightWithIndex,
  traverse,
  sequence,
  traverseWithIndex,
  extract,
  URI,
  getShow2 as getShow,
  getSemigroup,
  getEq2 as 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,
  tail,
  last2 as last,
  init,
  min2 as min,
  max2 as max,
  concatAll2 as concatAll,
  matchLeft,
  matchRight,
  modifyHead,
  updateHead,
  modifyLast,
  updateLast,
  intercalate2 as intercalate,
  chain,
  groupSort,
  filter,
  filterWithIndex,
  uncons,
  unsnoc,
  cons,
  snoc,
  prependToAll,
  fold,
  nonEmptyArray,
  NonEmptyArray_exports
};
