"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var iterate_exports = {};
__export(iterate_exports, {
  pEach: () => pEach,
  pEachChunk: () => pEachChunk,
  pFilter: () => pFilter,
  pFilterChunk: () => pFilterChunk
});
module.exports = __toCommonJS(iterate_exports);
var import_chunk = require("../arrays/chunk");
var import_promise = require("../primordials/promise");
var import_options = require("./options");
var import_retry = require("./retry");
// @__NO_SIDE_EFFECTS__
async function pEach(array, callbackFn, options) {
  const iterOpts = (0, import_options.normalizeIterationOptions)(options);
  const { concurrency, retries, signal } = iterOpts;
  const chunks = (0, import_chunk.arrayChunk)(array, concurrency);
  for (const chunk of chunks) {
    if (signal?.aborted) {
      return;
    }
    await (0, import_promise.PromiseAllSettled)(
      chunk.map(
        (item) => (0, import_retry.pRetry)((...args) => callbackFn(args[0]), {
          ...retries,
          args: [item],
          signal
        })
      )
    );
  }
}
// @__NO_SIDE_EFFECTS__
async function pEachChunk(array, callbackFn, options) {
  const { chunkSize = 100, ...retryOpts } = options || {};
  const chunks = (0, import_chunk.arrayChunk)(array, chunkSize);
  const normalizedRetryOpts = (0, import_options.normalizeRetryOptions)(retryOpts);
  const { signal } = normalizedRetryOpts;
  for (const chunk of chunks) {
    if (signal?.aborted) {
      return;
    }
    await (0, import_retry.pRetry)((...args) => callbackFn(args[0]), {
      ...normalizedRetryOpts,
      args: [chunk]
    });
  }
}
// @__NO_SIDE_EFFECTS__
async function pFilter(array, callbackFn, options) {
  const iterOpts = (0, import_options.normalizeIterationOptions)(options);
  return (await /* @__PURE__ */ pFilterChunk(
    (0, import_chunk.arrayChunk)(array, iterOpts.concurrency),
    callbackFn,
    iterOpts.retries
  )).flat();
}
// @__NO_SIDE_EFFECTS__
async function pFilterChunk(chunks, callbackFn, options) {
  const retryOpts = (0, import_options.normalizeRetryOptions)(options);
  const { signal } = retryOpts;
  const { length } = chunks;
  const filteredChunks = Array(length);
  for (let i = 0; i < length; i += 1) {
    if (signal?.aborted) {
      filteredChunks[i] = [];
    } else {
      const chunk = chunks[i];
      const settled = await (0, import_promise.PromiseAllSettled)(
        chunk.map(
          (value) => (0, import_retry.pRetry)((...args) => callbackFn(args[0]), {
            ...retryOpts,
            args: [value]
          })
        )
      );
      const predicateResults = settled.map(
        (r) => r.status === "fulfilled" ? r.value : false
      );
      filteredChunks[i] = chunk.filter((_v, i2) => predicateResults[i2]);
    }
  }
  return filteredChunks;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  pEach,
  pEachChunk,
  pFilter,
  pFilterChunk
});
