import type { CanvasExecutionStatus, IWorkflowNode } from '../Interface'

export const getNodeExecutionStatus = (node?: IWorkflowNode | null): CanvasExecutionStatus => {
	return node?.data?.executionStatus ?? node?.executionStatus ?? 'idle'
}

export const getEdgeExecutionStatus = (
	sourceNode?: IWorkflowNode | null,
	targetNode?: IWorkflowNode | null
): CanvasExecutionStatus => {
	const sourceStatus = getNodeExecutionStatus(sourceNode)
	const targetStatus = getNodeExecutionStatus(targetNode)
	const bothExecuted = sourceStatus !== 'idle' && targetStatus !== 'idle'

	if (!bothExecuted) {
		return 'idle'
	}

	if (sourceStatus === 'running' || targetStatus === 'running') {
		return 'running'
	}

	if (sourceStatus === 'suspended' || targetStatus === 'suspended') {
		return 'suspended'
	}

	if (sourceStatus === 'warning' || targetStatus === 'warning') {
		return 'warning'
	}

	if (sourceStatus === 'success' && targetStatus === 'success') {
		return 'success'
	}

	return 'idle'
}

export const getExecutionStatusColor = (status: CanvasExecutionStatus): string => {
	if (status === 'running') {
		return '#3b82f6'
	}

	if (status === 'success') {
		return '#22c55e'
	}

	if (status === 'warning') {
		return '#f59e0b'
	}

	if (status === 'suspended') {
		return '#64748b'
	}

	return '#b1b1b7'
}
