/**
 * 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 errors_exports = {};
__export(errors_exports, {
  buildRunJsAuthoringError: () => buildRunJsAuthoringError,
  dedupeErrors: () => dedupeErrors
});
module.exports = __toCommonJS(errors_exports);
var import_rules = require("../rules");
var import_constants = require("./constants");
function buildRunJsAuthoringError(input) {
  const rule = (0, import_rules.getRunJsAuthoringRule)(input.repairClass);
  const loc = getLineColumn(input.source, input.index);
  const agentInstruction = getRunJsAuthoringAgentInstruction(input.repairClass);
  return {
    path: input.path,
    ruleId: input.ruleId || rule.defaultRuleId,
    message: buildRunJsAuthoringErrorMessage(input.message, agentInstruction, rule.suggestedAction),
    details: buildDefinedDetails({
      repairClass: input.repairClass,
      suggestedAction: rule.suggestedAction,
      skipForbidden: true,
      mustRetry: true,
      agentInstruction,
      line: loc.line,
      column: loc.column,
      ...input.details
    })
  };
}
function getRunJsAuthoringAgentInstruction(repairClass) {
  return import_constants.CONTEXT_FIRST_REPAIR_CLASSES.has(repairClass) ? import_constants.RUNJS_CONTEXT_AND_RETRY_INSTRUCTION : import_constants.RUNJS_FIX_AND_RETRY_INSTRUCTION;
}
function buildRunJsAuthoringErrorMessage(message, agentInstruction, suggestedAction) {
  return [message, agentInstruction, suggestedAction ? `Suggested fix: ${suggestedAction}` : ""].filter(Boolean).join(" ");
}
function buildDefinedDetails(details) {
  return Object.fromEntries(
    Object.entries(details).filter(([, value]) => typeof value !== "undefined" && value !== "")
  );
}
function getLineColumn(source, index) {
  const safeIndex = Math.max(0, Math.min(index, source.length));
  const prefix = source.slice(0, safeIndex);
  const lines = prefix.split(/\r\n|\r|\n/);
  return {
    line: lines.length,
    column: lines[lines.length - 1].length + 1
  };
}
function dedupeErrors(errors) {
  const seen = /* @__PURE__ */ new Set();
  return errors.filter((error) => {
    var _a, _b;
    const key = `${error.path}:${error.ruleId}:${(_a = error.details) == null ? void 0 : _a.line}:${(_b = error.details) == null ? void 0 : _b.column}`;
    if (seen.has(key)) {
      return false;
    }
    seen.add(key);
    return true;
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildRunJsAuthoringError,
  dedupeErrors
});
