import { useMemo } from 'react';
import SmartBulletChart from '../../charts/SmartBulletChart';
import { usePreviewSurface } from '../../_dashboard_runtime/preview-surface-stub.js';

function buildVariantMock(variant) {
  switch (variant) {
    case 'basic':
      return [
        { title: '满意度', ranges: [100], measures: [80], targets: [85] },
      ];
    case 'transposed':
      return [
        { title: '满意度', ranges: [100], measures: [80], targets: [85] },
      ];
    case 'grouped':
    default:
      return [
        { title: '5', ranges: [100], measures: [40], targets: [85] },
        { title: '4', ranges: [100], measures: [80], targets: [40] },
        { title: '3', ranges: [100], measures: [20], targets: [22] },
        { title: '0-2', ranges: [100], measures: [30], targets: [10] },
      ];
  }
}

/**
 * Dashboard 子弹图预览桥接层：扁平 Props → SmartBulletChart `spec`。
 */
export default function BulletChartPreview({
  variant = 'grouped',
  data = [],
  showLegend = true,
  legendPosition = 'top',
  height = 340,
  style,
}) {
  const previewSurface = usePreviewSurface();

  const spec = useMemo(() => {
    const sourceData = Array.isArray(data) && data.length > 0 ? data : buildVariantMock(variant);
    return {
      data: sourceData,
      config: {
        variant,
        legendPosition,
        showLegend: !!showLegend,
        height: Number(height) || 340,
        mainColorToken: 'data-0',
        targetColorToken: 'data-4',
        resolveRoot: previewSurface || undefined,
      },
    };
  }, [
    variant,
    data,
    showLegend,
    legendPosition,
    height,
    previewSurface,
  ]);

  return (
    <div style={{ width: '100%', minWidth: 0, ...(style || {}) }}>
      <SmartBulletChart spec={spec} />
    </div>
  );
}
