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

const getTreeMapChart = (treemapData) => {
  const list = Array.isArray(treemapData) ? treemapData : []
  const dataWithColors = list.map((node, idx) => ({
    ...node,
    itemStyle: {
      ...(node?.itemStyle || {}),
      color: node?.itemStyle?.color || MUTICHARTCOLORS[idx % MUTICHARTCOLORS.length]
    }
  }))

  const option = {
    tooltip: {
      trigger: 'item',
      formatter: '{b}: {c}人'
    },
    grid: {
      top: '0%',
      left: '0%',
      right: '0%',
      bottom: '0%',
      containLabel: true
    },
    series: [
      {
        type: 'treemap',
        data: dataWithColors,
        roam: false,
        nodeClick: false,
        breadcrumb: {
          show: false
        },
        label: {
          show: true,
          formatter: '{b}',
          fontSize: 10
        },
        upperLabel: {
          show: true,
          height: 22
        },
        itemStyle: {
          borderColor: '#fff',
          borderWidth: 2,
          gapWidth: 2
        }
      }
    ]
  }
  return option
}

export default getTreeMapChart