import { computed, ComputedRef, unref, Ref } from 'vue'
import { Edge } from '@vue-flow/core'

type EdgeType = 'default' | 'straight' | 'step' | 'simplebezier'

/**
 * 边样式计算Hook
 * 根据边类型、链路类型、标签等计算最终样式
 */
export function useEdgeStyles(edges: Ref<Edge[]> | Edge[], edgeType: Ref<EdgeType> | EdgeType): ComputedRef<Edge[]> {
  const styledEdges = computed(() => {
    const edgesValue = unref(edges)
    const edgeTypeValue = unref(edgeType)

    return edgesValue.map((edge) => {
      const isVirtual = edge.data?.type === 'virtual'
      const hasLabel = edge.data?.label && edge.data.label.trim() !== ''

      return {
        ...edge,
        type: edgeTypeValue,
        label: hasLabel ? edge.data?.label : undefined,
        animated: true,
        style: {
          stroke: isVirtual ? '#F57C00' : '#1976D2',
          strokeWidth: 2,
          strokeDasharray: isVirtual ? '5,5' : undefined,
        },
        labelStyle: hasLabel
          ? {
              fill: '#333',
              fontWeight: 600,
              fontSize: 12,
            }
          : undefined,
        labelBgStyle: hasLabel
          ? {
              fill: '#fff',
              fillOpacity: 0.9,
            }
          : undefined,
      }
    })
  })

  return styledEdges
}
