'use strict';

var require$$3 = require('node:util');
var require$$1$1 = require('node:child_process');
var require$$1 = require('node:url');

function chunkify(iterable, chunkSize) {
	if (typeof iterable[Symbol.iterator] !== 'function') {
		throw new TypeError('Expected an `Iterable` in the first argument');
	}

	if (!(Number.isSafeInteger(chunkSize) && chunkSize > 0)) {
		throw new TypeError(`Expected \`chunkSize\` to be an integer from 1 and up, got \`${chunkSize}\``);
	}

	return {
		* [Symbol.iterator]() {
			if (Array.isArray(iterable)) {
				for (let index = 0; index < iterable.length; index += chunkSize) {
					yield iterable.slice(index, index + chunkSize);
				}

				return;
			}

			let chunk = [];

			for (const value of iterable) {
				chunk.push(value);

				if (chunk.length === chunkSize) {
					yield chunk;
					chunk = [];
				}
			}

			if (chunk.length > 0) {
				yield chunk;
			}
		},
	};
}

const pExecFile = require$$3.promisify(require$$1$1.execFile);

async function chunkedExec(binary, paths, maxPaths) {
	for (const chunk of chunkify(paths, maxPaths)) {
		// eslint-disable-next-line no-await-in-loop
		await pExecFile(require$$1.fileURLToPath(binary).replace("app.asar", "app.asar.unpacked"), chunk);
	}
}

exports.chunkedExec = chunkedExec;
