import React from 'react'

interface LoadingSpinnerProps {
  size?: 'small' | 'medium' | 'large'
  label?: string
  /** 是否全屏覆盖 */
  overlay?: boolean
}

const sizeMap = {
  small: 16,
  medium: 24,
  large: 36
}

/**
 * UX-02: 通用加载指示器组件
 */
export const LoadingSpinner = React.memo(function LoadingSpinner({
  size = 'medium',
  label,
  overlay = false
}: LoadingSpinnerProps) {
  const px = sizeMap[size]

  const spinner = (
    <div
      className={`loading-spinner loading-spinner-${size}`}
      role="status"
      aria-label={label || '加载中'}
    >
      <svg
        className="loading-spinner-svg"
        width={px}
        height={px}
        viewBox="0 0 24 24"
        fill="none"
        xmlns="http://www.w3.org/2000/svg"
        aria-hidden="true"
      >
        <circle
          cx="12"
          cy="12"
          r="10"
          stroke="currentColor"
          strokeWidth="3"
          strokeLinecap="round"
          opacity="0.2"
        />
        <path
          d="M12 2a10 10 0 0 1 10 10"
          stroke="currentColor"
          strokeWidth="3"
          strokeLinecap="round"
        />
      </svg>
      {label && <span className="loading-spinner-label">{label}</span>}
    </div>
  )

  if (!overlay) return spinner

  return (
    <div className="loading-overlay" aria-busy="true">
      {spinner}
    </div>
  )
})

LoadingSpinner.displayName = 'LoadingSpinner'
