import type { WidgetBase } from "@racletteCore/store/types"

/**
 * Validates that a widget has required fields
 */
export const validateWidget = (widget: Partial<WidgetBase>) => {
  if (!widget.pluginKey) {
    throw new Error(
      `Widget '${widget.name}' has no pluginKey - this is required`,
    )
  }

  if (!widget.name) {
    throw new Error(`Widget has no name - this is required`)
  }
}

/**
 * Creates an enhanced error with better context and preserved stack trace
 */
export const createEnhancedError = (
  error: unknown,
  widget: Partial<WidgetBase>,
) => {
  const message = error instanceof Error ? error.message : "Unknown error"

  const enhancedError = new Error(
    `Failed to load widget ${widget.pluginKey}/${widget.name}: ${message}`,
  )

  if (error instanceof Error && error.stack) {
    enhancedError.stack = error.stack
  }

  return enhancedError
}
