import "../../chunks/chunk.QU3DSPNU.js";
var McpErrorCode = /* @__PURE__ */ ((McpErrorCode2) => {
  McpErrorCode2["INVALID_INPUT"] = "INVALID_INPUT";
  McpErrorCode2["NOT_FOUND"] = "NOT_FOUND";
  McpErrorCode2["DATA_LOAD_FAILED"] = "DATA_LOAD_FAILED";
  McpErrorCode2["INVALID_URI"] = "INVALID_URI";
  McpErrorCode2["FILE_ERROR"] = "FILE_ERROR";
  McpErrorCode2["INVALID_REGEX"] = "INVALID_REGEX";
  McpErrorCode2["SEARCH_FAILED"] = "SEARCH_FAILED";
  McpErrorCode2["INTERNAL_ERROR"] = "INTERNAL_ERROR";
  return McpErrorCode2;
})(McpErrorCode || {});
class McpError extends Error {
  /**
   * Create a new MCP error
   * @param message - Human-readable error message
   * @param code - Error code for programmatic handling
   * @param context - Additional context information about the error
   */
  constructor(message, code, context) {
    super(message);
    this.code = code;
    this.context = context;
    this.name = "McpError";
    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, McpError);
    }
  }
  /**
   * Convert error to JSON representation
   */
  toJSON() {
    return {
      name: this.name,
      message: this.message,
      code: this.code,
      context: this.context,
      stack: this.stack
    };
  }
}
class ValidationError extends McpError {
  constructor(message, field, context) {
    super(message, "INVALID_INPUT" /* INVALID_INPUT */, {
      ...context,
      field
    });
    this.field = field;
    this.name = "ValidationError";
  }
}
class NotFoundError extends McpError {
  constructor(message, resourceType, resourceId, context) {
    super(message, "NOT_FOUND" /* NOT_FOUND */, {
      ...context,
      resourceType,
      resourceId
    });
    this.resourceType = resourceType;
    this.resourceId = resourceId;
    this.name = "NotFoundError";
  }
}
class DataLoadError extends McpError {
  constructor(message, filePath, context) {
    super(message, "DATA_LOAD_FAILED" /* DATA_LOAD_FAILED */, {
      ...context,
      filePath
    });
    this.filePath = filePath;
    this.name = "DataLoadError";
  }
}
class RegexError extends McpError {
  constructor(message, pattern, context) {
    super(message, "INVALID_REGEX" /* INVALID_REGEX */, {
      ...context,
      pattern
    });
    this.pattern = pattern;
    this.name = "RegexError";
  }
}
function formatErrorResponse(error) {
  let message;
  let code;
  let context;
  if (error instanceof McpError) {
    message = error.message;
    code = error.code;
    context = error.context;
  } else if (error instanceof Error) {
    message = error.message;
    code = "INTERNAL_ERROR" /* INTERNAL_ERROR */;
  } else {
    message = String(error);
    code = "INTERNAL_ERROR" /* INTERNAL_ERROR */;
  }
  let errorText = `Error: ${message}`;
  if (code) {
    errorText = `Error [${code}]: ${message}`;
  }
  if (context && Object.keys(context).length > 0) {
    errorText += `

Context: ${JSON.stringify(context, null, 2)}`;
  }
  return {
    content: [
      {
        type: "text",
        text: errorText
      }
    ],
    isError: true
  };
}
function logError(error, operation) {
  const logData = {
    timestamp: (/* @__PURE__ */ new Date()).toISOString(),
    operation
  };
  if (error instanceof McpError) {
    logData.errorCode = error.code;
    logData.message = error.message;
    logData.context = error.context;
  } else if (error instanceof Error) {
    logData.errorType = error.name;
    logData.message = error.message;
    logData.stack = error.stack;
  } else {
    logData.error = String(error);
  }
  console.error("[MCP Error]", JSON.stringify(logData));
}
export {
  DataLoadError,
  McpError,
  McpErrorCode,
  NotFoundError,
  RegexError,
  ValidationError,
  formatErrorResponse,
  logError
};
