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 aggregator_exports = {};
__export(aggregator_exports, {
  Aggregator: () => Aggregator
});
module.exports = __toCommonJS(aggregator_exports);
var import_core = require("./core");
var import_lazy = require("./lazy");
var import_util = require("./util");
class Aggregator {
  #pipeline;
  #options;
  /**
   * Creates an instance of the Aggregator class.
   *
   * @param pipeline - An array of objects representing the aggregation pipeline stages.
   * @param options - Optional configuration settings for the aggregator.
   */
  constructor(pipeline, options) {
    this.#pipeline = pipeline;
    this.#options = (0, import_core.initOptions)(options);
  }
  /**
   * Processes a collection through an aggregation pipeline and returns an iterator
   * for the transformed results.
   *
   * @param collection - The input collection to process. This can be any source
   *   that implements the `Source` interface.
   * @param options - Optional configuration for processing. If not provided, the
   *   default options of the aggregator instance will be used.
   * @returns An iterator that yields the results of the aggregation pipeline.
   *
   * @throws Will throw an error if:
   * - A pipeline stage contains more than one operator.
   * - The `$documents` operator is not the first stage in the pipeline.
   * - An unregistered pipeline operator is encountered.
   */
  stream(collection, options) {
    let iter = (0, import_lazy.Lazy)(collection);
    const opts = options ?? this.#options;
    const mode = opts.processingMode;
    if (mode & import_core.ProcessingMode.CLONE_INPUT) iter.map(import_util.cloneDeep);
    iter = this.#pipeline.map((stage, i) => {
      const keys = Object.keys(stage);
      (0, import_util.assert)(
        keys.length === 1,
        `aggregation stage must have single operator, got ${keys.toString()}.`
      );
      const name = keys[0];
      (0, import_util.assert)(
        name !== "$documents" || i == 0,
        "$documents must be first stage in pipeline."
      );
      const op = (0, import_core.getOperator)(import_core.OpType.PIPELINE, name, opts);
      (0, import_util.assert)(!!op, `unregistered pipeline operator ${name}.`);
      return [op, stage[name]];
    }).reduce((acc, [op, expr]) => op(acc, expr, opts), iter);
    if (mode & import_core.ProcessingMode.CLONE_OUTPUT) iter.map(import_util.cloneDeep);
    return iter;
  }
  /**
   * Executes the aggregation pipeline on the provided collection and returns the resulting array.
   *
   * @template T - The type of the objects in the resulting array.
   * @param collection - The input data source to run the aggregation on.
   * @param options - Optional settings to customize the aggregation behavior.
   * @returns An array of objects of type `T` resulting from the aggregation.
   */
  run(collection, options) {
    return this.stream(collection, options).value();
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  Aggregator
});
