

import { MUTICHARTCOLORS } from "@/common/constant.js";

const getSankeyChart = (nodes, links) => {
  const formatAmountWan = (v) => {
    const n = Number(v)
    if (!Number.isFinite(n)) return '0.00万'
    return `${n.toFixed(2)}万`
  }

  const option = {
    tooltip: {
      trigger: 'item',
      backgroundColor: 'rgba(255, 255, 255, 0.95)',
      borderColor: 'rgba(234, 236, 238, 1)',
      borderWidth: 1,
      textStyle: {
        color: 'rgb(59, 65, 75)',
        fontFamily: 'Microsoft YaHei',
        fontSize: 14,
        lineHeight: 22
      },
      formatter: function (params) {
        // 仅对连线展示资金金额；节点保持默认名称提示
        if (params?.dataType === 'edge') {
          const amount = formatAmountWan(params?.data?.value)
          return `资金金额：${amount}`
        }
        return params?.name ?? ''
      }
    },
    series: {
      type: 'sankey',
      layout: 'none',
      left: '2%',
      right: '15%',
      top: '5%',
      bottom: '5%',
      color: MUTICHARTCOLORS,
      lineStyle: {
        color: 'rgb(230, 231, 232)',
        opacity: 1
      },
      emphasis: {
        focus: 'adjacency'
      },
      nodeWidth: 40,
      label: {
        show: true,
        formatter: function (params) {
          return `${params.name}`;
        },
        position: 'right',
        textStyle: {
          fontSize: '14px',
          color: '#555'
        }
      },
      data: nodes,
      links: links
    }
  };

  return option
}

export default getSankeyChart