import * as tracing_1 from "../../../../Tracing"
const fileName_1 = "(@effect-ts/system): _src/Collections/Immutable/Chunk/api/split.ts"
import * as Chunk from "../core"
import { concreteId } from "../definition"
import { forEach_ } from "./forEach"
/**
 * Splits this chunk into `n` equally sized chunks.
 */

export function split_(self, n) {
  const length = self.length
  const k = Math.floor(n)
  const quotient = Math.floor(length / k)
  const remainder = length % k
  let chunks = Chunk.empty()
  let i = 0
  let chunk = Chunk.empty()
  forEach_(self, (a) => {
    chunk = Chunk.append_(chunk, a)

    if (
      (i <= remainder && chunk.length > quotient) ||
      (i > remainder && chunk.length >= quotient)
    ) {
      chunks = Chunk.append_(chunks, chunk)
      chunk = Chunk.empty()
    }

    i++
  })

  if (chunk.length > 0) {
    chunks = Chunk.append_(chunks, chunk)
  }

  return chunks
}
/**
 * Splits this chunk into `n` equally sized chunks.
 *
 * @dataFirst split_
 */

export function split(n) {
  return (self) => split_(self, n)
}
//# sourceMappingURL=split.js.map
