/**
 * 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, {
  FlowSurfaceAggregateError: () => FlowSurfaceAggregateError,
  FlowSurfaceBadRequestError: () => FlowSurfaceBadRequestError,
  FlowSurfaceConflictError: () => FlowSurfaceConflictError,
  FlowSurfaceError: () => FlowSurfaceError,
  FlowSurfaceForbiddenError: () => FlowSurfaceForbiddenError,
  FlowSurfaceInternalError: () => FlowSurfaceInternalError,
  isFlowSurfaceAggregateError: () => isFlowSurfaceAggregateError,
  isFlowSurfaceBadRequestError: () => isFlowSurfaceBadRequestError,
  isFlowSurfaceConflictError: () => isFlowSurfaceConflictError,
  isFlowSurfaceError: () => isFlowSurfaceError,
  isFlowSurfaceForbiddenError: () => isFlowSurfaceForbiddenError,
  isFlowSurfaceInternalError: () => isFlowSurfaceInternalError,
  normalizeFlowSurfaceError: () => normalizeFlowSurfaceError,
  throwAggregateBadRequest: () => throwAggregateBadRequest,
  throwBadRequest: () => throwBadRequest,
  throwConflict: () => throwConflict,
  throwForbidden: () => throwForbidden,
  throwInternalError: () => throwInternalError,
  toFlowSurfaceErrorItem: () => toFlowSurfaceErrorItem
});
module.exports = __toCommonJS(errors_exports);
class FlowSurfaceError extends Error {
  constructor(message, status, type, code, options = {}) {
    super(message);
    this.status = status;
    this.type = type;
    this.code = code;
    this.options = options;
    this.name = "FlowSurfaceError";
  }
  toResponseBody() {
    return {
      errors: [toFlowSurfaceErrorItem(this)]
    };
  }
}
class FlowSurfaceBadRequestError extends FlowSurfaceError {
  constructor(message, code, options = {}) {
    super(message, 400, "bad_request", code || "FLOW_SURFACE_BAD_REQUEST", options);
    this.name = "FlowSurfaceBadRequestError";
  }
}
class FlowSurfaceForbiddenError extends FlowSurfaceError {
  constructor(message, code = "FLOW_SURFACE_FORBIDDEN") {
    super(message, 403, "forbidden", code);
    this.name = "FlowSurfaceForbiddenError";
  }
}
class FlowSurfaceConflictError extends FlowSurfaceError {
  constructor(message, code = "FLOW_SURFACE_CONFLICT") {
    super(message, 409, "conflict", code);
    this.name = "FlowSurfaceConflictError";
  }
}
class FlowSurfaceInternalError extends FlowSurfaceError {
  constructor(message, code = "FLOW_SURFACE_INTERNAL_ERROR") {
    super(message, 500, "internal_error", code);
    this.name = "FlowSurfaceInternalError";
  }
}
class FlowSurfaceAggregateError extends FlowSurfaceError {
  errors;
  constructor(errors, message) {
    const normalizedErrors = errors.map(
      (error, index) => normalizeFlowSurfaceErrorItemInput({
        ...error,
        index: index + 1
      })
    );
    super(
      message || buildAggregateBadRequestMessage(normalizedErrors.length),
      400,
      "bad_request",
      "FLOW_SURFACE_AUTHORING_VALIDATION_FAILED"
    );
    this.name = "FlowSurfaceAggregateError";
    this.errors = normalizedErrors;
  }
  toResponseBody() {
    return {
      message: this.message,
      errorCount: this.errors.length,
      details: buildAggregateBadRequestDetails(this.errors),
      errors: this.errors
    };
  }
}
function isFlowSurfaceError(error) {
  return error instanceof FlowSurfaceError;
}
function isFlowSurfaceBadRequestError(error) {
  return error instanceof FlowSurfaceBadRequestError;
}
function isFlowSurfaceAggregateError(error) {
  return error instanceof FlowSurfaceAggregateError;
}
function isFlowSurfaceForbiddenError(error) {
  return error instanceof FlowSurfaceForbiddenError;
}
function isFlowSurfaceConflictError(error) {
  return error instanceof FlowSurfaceConflictError;
}
function isFlowSurfaceInternalError(error) {
  return error instanceof FlowSurfaceInternalError;
}
function toFlowSurfaceErrorItem(error) {
  return buildDefinedErrorItem({
    message: error.message,
    type: error.type,
    code: error.code,
    status: error.status,
    path: error.options.path,
    ruleId: error.options.ruleId,
    details: error.options.details
  });
}
function normalizeFlowSurfaceError(error) {
  if (error instanceof FlowSurfaceError) {
    return error;
  }
  const message = error instanceof Error ? error.message : String(error);
  return new FlowSurfaceInternalError(message);
}
function throwBadRequest(message, codeOrOptions, options = {}) {
  if (typeof codeOrOptions === "object" && codeOrOptions) {
    throw new FlowSurfaceBadRequestError(message, void 0, codeOrOptions);
  }
  const code = typeof codeOrOptions === "string" ? codeOrOptions : void 0;
  throw new FlowSurfaceBadRequestError(message, code, options);
}
function throwAggregateBadRequest(errors) {
  throw new FlowSurfaceAggregateError(errors);
}
function throwForbidden(message, code) {
  throw new FlowSurfaceForbiddenError(message, code);
}
function throwConflict(message, code) {
  throw new FlowSurfaceConflictError(message, code);
}
function throwInternalError(message, code) {
  throw new FlowSurfaceInternalError(message, code);
}
function buildDefinedErrorItem(input) {
  const output = {
    message: input.message,
    type: input.type,
    code: input.code,
    status: input.status
  };
  if (typeof input.index !== "undefined") {
    output.index = input.index;
  }
  if (typeof input.path !== "undefined") {
    output.path = input.path;
  }
  if (typeof input.ruleId !== "undefined") {
    output.ruleId = input.ruleId;
  }
  if (typeof input.details !== "undefined") {
    output.details = input.details;
  }
  return output;
}
function normalizeFlowSurfaceErrorItemInput(input) {
  return buildDefinedErrorItem({
    message: input.message,
    index: input.index,
    type: input.type || "bad_request",
    code: input.code || "FLOW_SURFACE_AUTHORING_VALIDATION_ERROR",
    status: input.status || 400,
    path: input.path,
    ruleId: input.ruleId,
    details: input.details
  });
}
function buildAggregateBadRequestMessage(errorCount) {
  return `flowSurfaces authoring validation failed with ${errorCount} error(s); fix all errors before retrying the same write`;
}
function buildAggregateBadRequestDetails(errors) {
  const requiredBlockTypes = Array.from(
    new Set(
      errors.map((error) => {
        var _a;
        return (_a = error.details) == null ? void 0 : _a.requiredBlockType;
      }).filter(
        (requiredBlockType) => typeof requiredBlockType === "string" && !!requiredBlockType
      )
    )
  );
  return {
    errorCount: errors.length,
    mustFixAllErrorsBeforeRetry: true,
    retryPolicy: "fix_all_errors_before_retry_same_write",
    sameWriteRetryRequired: true,
    agentInstruction: "Read the complete errors[] array. Fix every listed error in one payload revision before retrying the same write. Do not fix only the first error and immediately retry. Do not drop, defer, or replace required chart, jsBlock, or JS/RunJS work to bypass validation.",
    ...requiredBlockTypes.length ? {
      requiredBlockPolicy: {
        requiredBlockTypes,
        fixStrategy: "repair_same_block_type",
        doNotReplaceOrDrop: true
      }
    } : {}
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  FlowSurfaceAggregateError,
  FlowSurfaceBadRequestError,
  FlowSurfaceConflictError,
  FlowSurfaceError,
  FlowSurfaceForbiddenError,
  FlowSurfaceInternalError,
  isFlowSurfaceAggregateError,
  isFlowSurfaceBadRequestError,
  isFlowSurfaceConflictError,
  isFlowSurfaceError,
  isFlowSurfaceForbiddenError,
  isFlowSurfaceInternalError,
  normalizeFlowSurfaceError,
  throwAggregateBadRequest,
  throwBadRequest,
  throwConflict,
  throwForbidden,
  throwInternalError,
  toFlowSurfaceErrorItem
});
