import { useMemo } from 'react';
import SmartBarChart from '../../charts/SmartBarChart';
import {
  chartMockCategoryDateLabel,
  getDefaultDataColorToken,
} from '../../charts/chart-common';

const DEFAULT_DATA = [
  { name: chartMockCategoryDateLabel(0), series1: 22, series2: 18 },
  { name: chartMockCategoryDateLabel(1), series1: 13, series2: 21 },
  { name: chartMockCategoryDateLabel(2), series1: 25, series2: 29 },
  { name: chartMockCategoryDateLabel(3), series1: 29, series2: 24 },
  { name: chartMockCategoryDateLabel(4), series1: 38, series2: 31 },
];

const DEFAULT_LEGEND = [
  { key: 'series1', label: 'Series 1' },
  { key: 'series2', label: 'Series 2' },
];

/**
 * Dashboard BarChart 桥接组件 — 接收扁平 props，内部转 SmartBarChart 的 spec。
 * 所有几何 / 配色 / Tooltip 全部走 native SmartBarChart，保持视觉与 BarChartPreview 一致。
 */
export default function BarChart({
  data = DEFAULT_DATA,
  legendItems = DEFAULT_LEGEND,
  layout = 'vertical',
  stack = false,
  showLegend = true,
  legendPosition = 'bottom',
  showLabel = true,
  showXAxis = true,
  showYAxis = true,
  height = 300,
}) {
  const spec = useMemo(() => {
    const series = (Array.isArray(legendItems) ? legendItems : []).map((item, idx) => ({
      dataKey: item.key,
      name: item.label ?? item.key,
      colorToken: item.colorToken ?? getDefaultDataColorToken(idx),
    }));
    return {
      data,
      series,
      config: {
        layout,
        stack: !!stack,
        showLegend: !!showLegend,
        showXAxis: !!showXAxis,
        showYAxis: !!showYAxis,
        showLabel: !!showLabel,
        cornerMode: 'rounded',
        legendPosition,
        height: Number(height) || 300,
      },
    };
  }, [data, legendItems, layout, stack, showLegend, legendPosition, showLabel, showXAxis, showYAxis, height]);

  return (
    <div style={{ width: '100%', height }}>
      <SmartBarChart spec={spec} />
    </div>
  );
}
