import i18n from '@/i18n'

const NODE_GROUP_KEYS: Record<string, 'start' | 'logic' | 'data' | 'tool' | 'other' | 'custom'> = {
	start: 'start',
	end: 'other',
	'http-request': 'data',
	'if-else': 'logic',
	database: 'data',
	'database-query': 'data',
	code: 'data',
	'module-invoke': 'tool',
	'basic-dataset': 'data',
	'view-data': 'data',
	iteration: 'logic',
	loop: 'logic',
	'list-operator': 'data',
	'question-classifier': 'logic',
	'loop-end': 'logic',
	'trigger-schedule': 'start',
	'trigger-webhook': 'start',
	'mail-sender': 'tool',
	'sms-sender': 'tool',
	'workflow-approval': 'logic',
	'knowledge-retrieval': 'data',
	'loop-start': 'logic',
	'iteration-start': 'logic',
	stickyNote: 'other',
	custom: 'custom'
}

const NODE_META_KEYS: Record<string, string> = {
	'database-query': 'database'
}

const getNodeMetaKey = (nodeType: string) => NODE_META_KEYS[nodeType] || nodeType

const getNodeMetaText = (nodeType: string, field: 'displayName' | 'description', fallback = '') => {
	const value = i18n.tm(`nodes.meta.${getNodeMetaKey(nodeType)}.${field}`)
	return typeof value === 'string' && value ? value : fallback
}

export const getNodeDisplayName = (nodeType: string) =>
	getNodeMetaText(nodeType, 'displayName', nodeType)

export const getNodeDescription = (nodeType: string) =>
	getNodeMetaText(nodeType, 'description', '')

export const getNodeGroup = (nodeType: string) => {
	const groupKey = NODE_GROUP_KEYS[nodeType] || 'other'
	return i18n.t(`nodes.groups.${groupKey}`)
}

export const getNodeOutputText = (namespace: string, key: string) =>
	i18n.t(`nodes.outputs.${namespace}.${key}`)

export const getConditionPortLabel = (index: number) =>
	`${i18n.t('nodes.outputs.condition.casePrefix')}${index + 1}`

export const getClassPortLabel = (index: number) =>
	`${i18n.t('nodes.outputs.question-classifier.classPrefix')}${index + 1}`
