/**
 * This file is part of the NocoBase (R) project.
 * Copyright (c) 2020-2024 NocoBase Co., Ltd.
 * Authors: NocoBase Team.
 *
 * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
 * For more information, please refer to: https://www.nocobase.com/agreement.
 */

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 source_budget_exports = {};
__export(source_budget_exports, {
  collectRunJsSourceLimitErrors: () => collectRunJsSourceLimitErrors,
  createRunJsSourceBudget: () => createRunJsSourceBudget
});
module.exports = __toCommonJS(source_budget_exports);
var import_constants = require("./constants");
var import_errors = require("./errors");
function createRunJsSourceBudget() {
  return {
    count: 0,
    totalLength: 0
  };
}
function collectRunJsSourceLimitErrors(input, budget) {
  const source = String(input.code || "");
  const sourceLength = source.length;
  const modelUse = normalizeText(input.modelUse);
  const surface = normalizeText(input.surface);
  const errors = [];
  if (budget) {
    budget.count += 1;
    budget.totalLength += sourceLength;
  }
  const pushLimitError = (ruleId, message, details) => {
    errors.push(
      (0, import_errors.buildRunJsAuthoringError)({
        path: input.path,
        repairClass: "source-limit-stop",
        ruleId,
        message,
        modelUse,
        surface,
        index: 0,
        source,
        details
      })
    );
  };
  const sourceTooLarge = sourceLength > import_constants.MAX_RUNJS_SOURCE_LENGTH;
  if (sourceTooLarge && (!budget || budget.count <= import_constants.MAX_RUNJS_SOURCES_PER_REQUEST)) {
    pushLimitError(
      "runjs-source-too-large",
      `flowSurfaces authoring ${input.path} RunJS source is too large to validate safely`,
      {
        maxSourceLength: import_constants.MAX_RUNJS_SOURCE_LENGTH,
        sourceLength
      }
    );
  }
  let requestLimitExceeded = false;
  if (budget && budget.count > import_constants.MAX_RUNJS_SOURCES_PER_REQUEST) {
    requestLimitExceeded = true;
    if (!budget.countLimitReported) {
      budget.countLimitReported = true;
      pushLimitError(
        "runjs-too-many-sources",
        `flowSurfaces authoring request contains too many RunJS sources to validate safely`,
        {
          maxSources: import_constants.MAX_RUNJS_SOURCES_PER_REQUEST,
          sourceCount: budget.count
        }
      );
    }
  }
  if (budget && budget.totalLength > import_constants.MAX_RUNJS_TOTAL_SOURCE_LENGTH) {
    requestLimitExceeded = true;
    if (!budget.totalLimitReported) {
      budget.totalLimitReported = true;
      pushLimitError(
        "runjs-total-source-too-large",
        `flowSurfaces authoring request contains too much RunJS source to validate safely`,
        {
          maxTotalSourceLength: import_constants.MAX_RUNJS_TOTAL_SOURCE_LENGTH,
          totalSourceLength: budget.totalLength
        }
      );
    }
  }
  return { errors, skipInspection: sourceTooLarge || requestLimitExceeded };
}
function normalizeText(value) {
  return typeof value === "string" ? value.trim() : "";
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  collectRunJsSourceLimitErrors,
  createRunJsSourceBudget
});
