import { yieldIfNeeded as g } from "../../utilities/yielder.js";
class y {
}
function u(i, f, h, t, n) {
  for (const s of i) {
    const o = s.nextTokens(h);
    if (o) {
      t.total += o.length;
      const c = f[f.length - 1];
      if (t.offset + o.length > c.length) {
        const r = c.length - t.offset;
        c.set(o.slice(0, r), t.offset);
        const l = new Uint16Array(Math.floor(n * 0.1) + 100);
        l.set(o.slice(r), 0), f.push(l), t.offset = o.length - r;
      } else
        c.set(o, t.offset), t.offset += o.length;
    }
  }
}
async function d(i, f, h) {
  const t = Math.min(
    (await Promise.all(i.map((e) => e.estimateTokens(f)))).reduce((e, a) => e + a, 0),
    f.vocabSize * 1e4
  ), n = [new Uint16Array(t)], s = {
    offset: 0,
    total: 0
  };
  let o = performance.now();
  for (; s.offset < t && (u(i, n, f, s, t), !i.every((e) => !e.hasMoreConversations())); )
    o = await g(o, h, s.total);
  if (n.length === 1)
    return n[0].subarray(0, s.offset);
  const c = n.reduce((e, a) => e + a.length, 0) - (n[n.length - 1].length - s.offset), r = new Uint16Array(c);
  let l = 0;
  for (let e = 0; e < n.length; e++) {
    const a = n[e];
    e === n.length - 1 ? (r.set(a.subarray(0, s.offset), l), l += s.offset) : (r.set(a, l), l += a.length);
  }
  return r;
}
export {
  y as Task,
  d as tokensFromTasks
};
