/** @typedef {'horizontal'|'vertical'} ChartLayout */
/** @typedef {'top'|'bottom'} LegendPosition */
/** @typedef {'square'|'rounded'} ChartCornerMode */

/**
 * @typedef {Object} SmartBarSeries
 * @property {string} dataKey
 * @property {string} [name]
 * @property {string} [colorToken]
 */

/**
 * @typedef {Object} SmartBarConfig
 * @property {ChartLayout} [layout]
 * @property {boolean} [stack]
 * @property {boolean} [showLegend]
 * @property {boolean} [showXAxis]
 * @property {boolean} [showYAxis]
 * @property {boolean} [showLabel]
 * @property {ChartCornerMode} [cornerMode]
 * @property {LegendPosition} [legendPosition]
 * @property {number} [height]
 * @property {{ top?: number, right?: number, bottom?: number, left?: number }} [margin]
 * @property {number} [yAxisWidth] — 纵轴（横向柱图为类目轴）占位宽度（px）
 * @property {{ top?: number, bottom?: number }} [yAxisCategoryPadding] — 覆盖默认类目轴上下内缩
 * @property {number} [categoryLabelMaxLength] — 横向柱图 Y 轴类目截断长度，默认 8
 * @property {number} [plotAreaMinWidth] — 横向柱图绘图区最小宽度（px），用于对齐设计稿柱区宽度
 * @property {Element} [resolveRoot]
 */

/** 堆叠段之间的目标像素缝（非 stroke）— 全局统一 2px（柱图 / 组合图共用） */
export const STACK_SEGMENT_GAP_PX = 2;

/**
 * @param {SmartBarConfig | undefined} c
 */
export function mergeSmartBarChartConfig(c) {
  const defaults = {
    layout: 'vertical',
    stack: false,
    showLegend: true,
    showXAxis: true,
    showYAxis: true,
    showLabel: false,
    cornerMode: 'rounded',
    legendPosition: 'bottom',
    height: 340,
    margin: { top: 20, right: 20, left: 10, bottom: 10 },
    yAxisWidth: undefined,
    yAxisCategoryPadding: undefined,
    categoryLabelMaxLength: 8,
    plotAreaMinWidth: undefined,
  };
  if (!c || typeof c !== 'object') return defaults;
  const cornerMode = c.cornerMode === 'square' ? 'square' : 'rounded';
  const yAxisCategoryPadding =
    c.yAxisCategoryPadding && typeof c.yAxisCategoryPadding === 'object'
      ? { ...c.yAxisCategoryPadding }
      : undefined;
  return {
    ...defaults,
    ...c,
    cornerMode,
    height: Number(c.height ?? defaults.height) || defaults.height,
    margin: { ...defaults.margin, ...(c.margin || {}) },
    yAxisCategoryPadding,
    categoryLabelMaxLength: Number.isFinite(Number(c.categoryLabelMaxLength))
      ? Math.max(1, Number(c.categoryLabelMaxLength))
      : defaults.categoryLabelMaxLength,
    plotAreaMinWidth: Number.isFinite(Number(c.plotAreaMinWidth))
      ? Math.max(0, Number(c.plotAreaMinWidth))
      : undefined,
    yAxisWidth: Number.isFinite(Number(c.yAxisWidth)) ? Math.max(24, Number(c.yAxisWidth)) : undefined,
  };
}
