/**
 * 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 inspect_exports = {};
__export(inspect_exports, {
  inspectRunJsAuthoringCode: () => inspectRunJsAuthoringCode,
  inspectRunJsAuthoringCodeForWrite: () => inspectRunJsAuthoringCodeForWrite
});
module.exports = __toCommonJS(inspect_exports);
var import_constants = require("./runtime/constants");
var import_surface = require("./runtime/surface");
var import_parser = require("./ast/parser");
var import_errors = require("./runtime/errors");
var import_source_budget = require("./runtime/source-budget");
var import_scan = require("./scan");
var import_validators = require("./validators");
function inspectRunJsAuthoringCodeForWrite(input, budget, context = {}) {
  const limitResult = (0, import_source_budget.collectRunJsSourceLimitErrors)(input, budget);
  if (limitResult.skipInspection || limitResult.errors.length) {
    return limitResult.errors;
  }
  return inspectRunJsAuthoringCode(input, context);
}
function inspectRunJsAuthoringCode(input, context = {}) {
  const errors = [];
  const source = String(input.code || "");
  const modelUse = (0, import_surface.normalizeText)(input.modelUse);
  const surface = (0, import_surface.normalizeText)(input.surface);
  const surfaceStyle = (0, import_surface.resolveSurfaceStyle)(input);
  const sourceLimitResult = (0, import_source_budget.collectRunJsSourceLimitErrors)(input);
  if (sourceLimitResult.skipInspection || sourceLimitResult.errors.length) {
    return sourceLimitResult.errors;
  }
  if (surface && !import_constants.SURFACE_STYLE_BY_ID[surface]) {
    errors.push(
      (0, import_errors.buildRunJsAuthoringError)({
        path: input.path,
        repairClass: "unknown-surface-stop",
        message: `flowSurfaces authoring ${input.path} references unknown RunJS surface '${surface}'`,
        modelUse,
        surface,
        index: 0,
        source
      })
    );
    return errors;
  }
  if (modelUse && !import_constants.KNOWN_MODEL_USES.has(modelUse)) {
    errors.push(
      (0, import_errors.buildRunJsAuthoringError)({
        path: input.path,
        repairClass: "unknown-model-stop",
        message: `flowSurfaces authoring ${input.path} references unknown JS model '${modelUse}'`,
        modelUse,
        surface,
        index: 0,
        source
      })
    );
    return errors;
  }
  if (surface && modelUse && import_constants.SURFACE_ALLOWED_MODEL_USES[surface] && !import_constants.SURFACE_ALLOWED_MODEL_USES[surface].has(modelUse)) {
    errors.push(
      (0, import_errors.buildRunJsAuthoringError)({
        path: input.path,
        repairClass: "unknown-model-stop",
        message: `flowSurfaces authoring ${input.path} model '${modelUse}' is not supported by RunJS surface '${surface}'`,
        modelUse,
        surface,
        index: 0,
        source
      })
    );
    return errors;
  }
  if (!surfaceStyle) {
    errors.push(
      (0, import_errors.buildRunJsAuthoringError)({
        path: input.path,
        repairClass: modelUse ? "unknown-model-stop" : "unknown-surface-stop",
        message: `flowSurfaces authoring ${input.path} cannot resolve a RunJS validation surface`,
        modelUse,
        surface,
        index: 0,
        source
      })
    );
    return errors;
  }
  const parseResult = (0, import_parser.parseRunJsAuthoringAst)(source);
  if (parseResult.error) {
    return [
      (0, import_errors.buildRunJsAuthoringError)({
        path: input.path,
        repairClass: "syntax-stop",
        ruleId: "runjs-syntax-invalid",
        message: `flowSurfaces authoring ${input.path} has invalid JavaScript syntax: ${parseResult.error.message}`,
        modelUse,
        surface,
        index: parseResult.error.index,
        source,
        details: {
          syntaxMessage: parseResult.error.message
        }
      })
    ];
  }
  const scan = (0, import_scan.scanJavaScriptSource)(source, parseResult.ast, context, modelUse);
  errors.push(
    ...(0, import_validators.collectRunJsInspectionErrors)({
      context,
      input,
      modelUse,
      scan,
      source,
      surface,
      surfaceStyle
    })
  );
  return (0, import_errors.dedupeErrors)(errors).slice(0, import_constants.MAX_RUNJS_ERRORS_PER_SOURCE);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  inspectRunJsAuthoringCode,
  inspectRunJsAuthoringCodeForWrite
});
