/**
 * native chart：折线/面积通用路径生成器
 *
 * 输入一组 (x, y) 像素点 → 输出 SVG path d 字符串
 * 支持 linear / monotone / step 三种插值
 */

/** 直线连接 */
export function buildLinearPath(points) {
  if (!points.length) return '';
  let d = `M${points[0].x.toFixed(2)},${points[0].y.toFixed(2)}`;
  for (let i = 1; i < points.length; i++) {
    d += ` L${points[i].x.toFixed(2)},${points[i].y.toFixed(2)}`;
  }
  return d;
}

/**
 * 单调三次 Hermite（与 Recharts type="monotone" 视觉一致）
 * 参考：Steffen / Fritsch–Carlson 单调插值
 */
export function buildMonotonePath(points) {
  const n = points.length;
  if (n === 0) return '';
  if (n === 1) return `M${points[0].x.toFixed(2)},${points[0].y.toFixed(2)}`;

  const xs = points.map((p) => p.x);
  const ys = points.map((p) => p.y);

  // 段斜率
  const dx = [];
  const dy = [];
  const m = [];
  for (let i = 0; i < n - 1; i++) {
    dx[i] = xs[i + 1] - xs[i];
    dy[i] = ys[i + 1] - ys[i];
    m[i] = dx[i] === 0 ? 0 : dy[i] / dx[i];
  }
  // 端点切线
  const t = new Array(n).fill(0);
  t[0] = m[0];
  t[n - 1] = m[n - 2];
  for (let i = 1; i < n - 1; i++) {
    if (m[i - 1] * m[i] <= 0) {
      t[i] = 0;
    } else {
      t[i] = (m[i - 1] + m[i]) / 2;
    }
  }
  // 修正：保持单调
  for (let i = 0; i < n - 1; i++) {
    if (m[i] === 0) {
      t[i] = 0;
      t[i + 1] = 0;
    } else {
      const a = t[i] / m[i];
      const b = t[i + 1] / m[i];
      const h = a * a + b * b;
      if (h > 9) {
        const s = 3 / Math.sqrt(h);
        t[i] = s * a * m[i];
        t[i + 1] = s * b * m[i];
      }
    }
  }

  let d = `M${xs[0].toFixed(2)},${ys[0].toFixed(2)}`;
  for (let i = 0; i < n - 1; i++) {
    const cx1 = xs[i] + dx[i] / 3;
    const cy1 = ys[i] + (t[i] * dx[i]) / 3;
    const cx2 = xs[i + 1] - dx[i] / 3;
    const cy2 = ys[i + 1] - (t[i + 1] * dx[i]) / 3;
    d += ` C${cx1.toFixed(2)},${cy1.toFixed(2)} ${cx2.toFixed(2)},${cy2.toFixed(2)} ${xs[i + 1].toFixed(2)},${ys[i + 1].toFixed(2)}`;
  }
  return d;
}

/** 阶梯（Recharts type="step" 等价 stepBefore） */
export function buildStepPath(points) {
  if (!points.length) return '';
  let d = `M${points[0].x.toFixed(2)},${points[0].y.toFixed(2)}`;
  for (let i = 1; i < points.length; i++) {
    const cx = points[i].x.toFixed(2);
    const py = points[i - 1].y.toFixed(2);
    const cy = points[i].y.toFixed(2);
    d += ` L${cx},${py} L${cx},${cy}`;
  }
  return d;
}

export function buildLinePath(points, curve = 'linear') {
  if (curve === 'monotone') return buildMonotonePath(points);
  if (curve === 'step') return buildStepPath(points);
  return buildLinearPath(points);
}

/**
 * 在折线下方闭合成区域（用于 Area Chart）
 * baselineY 为基线像素位置（通常是 yScale(0) 或 plot 底部）
 */
export function closeAreaPath(linePath, points, baselineY) {
  if (!points.length || !linePath) return '';
  const last = points[points.length - 1];
  const first = points[0];
  return `${linePath} L${last.x.toFixed(2)},${baselineY.toFixed(2)} L${first.x.toFixed(2)},${baselineY.toFixed(2)} Z`;
}

/**
 * 圆角矩形 path（4 角独立半径，与 Recharts radius={[tl, tr, br, bl]} 顺序一致）
 *
 * @param {number} x 左上 x
 * @param {number} y 左上 y
 * @param {number} w 宽度
 * @param {number} h 高度
 * @param {number[]} r [tl, tr, br, bl]，0 表示直角
 */
export function buildRoundedRectPath(x, y, w, h, r) {
  const safeW = Math.max(0, w);
  const safeH = Math.max(0, h);
  if (safeW === 0 || safeH === 0) return '';
  const maxR = Math.min(safeW, safeH) / 2;
  const tl = Math.min(maxR, Math.max(0, r?.[0] || 0));
  const tr = Math.min(maxR, Math.max(0, r?.[1] || 0));
  const br = Math.min(maxR, Math.max(0, r?.[2] || 0));
  const bl = Math.min(maxR, Math.max(0, r?.[3] || 0));
  const x2 = x + safeW;
  const y2 = y + safeH;
  return [
    `M${x + tl},${y}`,
    `L${x2 - tr},${y}`,
    tr ? `Q${x2},${y} ${x2},${y + tr}` : '',
    `L${x2},${y2 - br}`,
    br ? `Q${x2},${y2} ${x2 - br},${y2}` : '',
    `L${x + bl},${y2}`,
    bl ? `Q${x},${y2} ${x},${y2 - bl}` : '',
    `L${x},${y + tl}`,
    tl ? `Q${x},${y} ${x + tl},${y}` : '',
    'Z',
  ].filter(Boolean).join(' ');
}
