// 节点端口配置
export const PORT_GROUPS = ['top', 'right', 'bottom', 'left'] as const;

// 转换后端端口配置为X6格式
export const convertPortConfig = (ports: any = {}) => {
    const { groups = {} } = ports;
    
    // 转换groups配置
    const processedGroups: any = {};
    Object.entries(groups).forEach(([key, group]: [string, any]) => {
        processedGroups[key] = {
            ...group,
            attrs: {
                circle: {
                    ...group.attrs?.circle,
                    magnet: true,
                }
            },
            position: group.position
        };
    });

    // 如果没有types，就用groups的key作为默认的items
    const items = Object.keys(groups).map(group => ({
        group: group
    }));

    return {
        groups: processedGroups,
        items
    };
};

// 默认样式配置
export const DEFAULT_STYLES = {
    node: {
        stroke: '#5F95FF',
        strokeWidth: 2,
        fill: '#FFF',
    },
    label: {
        fontSize: 12,
        fill: '#000000',
    },
    edge: {
        stroke: '#1890ff',
        strokeWidth: 2,
        targetMarker: {
            name: 'classic',
            size: 8,
        },
    },
} as const;

// 默认节点大小
export const NODE_SIZES = {
    circle: {
        width: 60,
        height: 60,
    },
    rectangle: {
        width: 100,
        height: 50,
    },
    diamond: {
        width: 60,
        height: 60,
    },
} as const;

// 网格配置
export const GRID_CONFIG = {
    size: 10,
    visible: true,
    type: 'dot',
    args: {
        color: '#a0a0a0',
        thickness: 1,
    },
} as const;

// 连接配置
export const CONNECTING_CONFIG = {
    snap: true,
    allowBlank: false,
    allowLoop: false,
    highlight: true,
    connector: 'rounded',
    connectionPoint: 'boundary',
    router: {
        name: 'manhattan'
    },
    validateConnection({
        sourceView,
        targetView,
        sourceMagnet,
        targetMagnet
    }: any) {
        if (!sourceMagnet || !targetMagnet) {
            return false;
        }
        return true;
    }
} as const;

// 高亮配置
export const HIGHLIGHTING_CONFIG = {
    magnetAvailable: {
        name: 'stroke',
        args: {
            padding: 4,
            attrs: {
                strokeWidth: 4,
                stroke: '#52c41a',
            },
        },
    },
} as const;

// 节点注册配置
export const NODE_REGISTRY_CONFIG = {
    circle: {
        inherit: 'circle',
        width: NODE_SIZES.circle.width,
        height: NODE_SIZES.circle.height,
        attrs: {
            body: DEFAULT_STYLES.node,
            label: DEFAULT_STYLES.label,
        },
    },
    rectangle: {
        inherit: 'rect',
        width: NODE_SIZES.rectangle.width,
        height: NODE_SIZES.rectangle.height,
        attrs: {
            body: DEFAULT_STYLES.node,
            label: DEFAULT_STYLES.label,
        },
    },
    diamond: {
        inherit: 'polygon',
        width: NODE_SIZES.diamond.width,
        height: NODE_SIZES.diamond.height,
        attrs: {
            body: {
                ...DEFAULT_STYLES.node,
                refPoints: '0,10 10,0 20,10 10,20',
            },
            label: DEFAULT_STYLES.label,
        },
    },
} as const;
