import assert from 'node:assert/strict'
import {
  getPathConflictDisplayEnabled,
  isNewPathConflictPayload,
  normalizePathConflictMessage,
} from '../src/utils/pathConflictDisplay.mjs'

assert.equal(getPathConflictDisplayEnabled({}), true)
assert.equal(getPathConflictDisplayEnabled({ pathConflictDisplayEnabled: false }), false)

const status = normalizePathConflictMessage({
  type: 'path_conflict_status',
  payload: {
    conflictPoint: { latitude: 36.1, longitude: 120.2 },
    aircraftName: 'A01',
    vehicleName: 'V01',
    aircraftDistanceToConflictMeters: 120,
    vehicleDistanceToConflictMeters: 80,
    aircraftAlertThresholdMeters: 200,
    vehicleAlertThresholdMeters: 100,
    vehicleMovingTowardConflictPoint: true,
    directionLockStatus: 'DIRECTION_LOCKED',
    calculationStatus: 'MONITORING',
    directionLockReason: '',
    object1ForwardDistanceMeters: 118,
    object2ForwardDistanceMeters: 78,
  },
})

assert.equal(status.kind, 'status')
assert.equal(status.title, '计算中')
assert.deepEqual(status.conflictPoint, { latitude: 36.1, longitude: 120.2 })
assert.equal(status.key, 'A01::V01')
assert.equal(status.directionLockText, '方向已锁定')
assert.equal(status.directionLockFailed, false)
assert.equal(status.directionLockReason, '')
assert.equal(status.object1ForwardDistance, 118)
assert.equal(status.object2ForwardDistance, 78)

const directionInvalid = normalizePathConflictMessage({
  type: 'path_conflict_status',
  payload: {
    aircraftName: 'A01',
    vehicleName: 'V01',
    directionLockStatus: 'DIRECTION_INVALID',
    calculationStatus: 'DIRECTION_LOCK_FAILED',
    directionLockReason: '冲突点在两侧',
  },
})

assert.equal(directionInvalid.directionLockText, '方向无法锁定')
assert.equal(directionInvalid.directionLockFailed, true)
assert.equal(directionInvalid.directionLockReason, '冲突点在两侧')

const resume = normalizePathConflictMessage({
  type: 'path_conflict_alert',
  payload: {
    messageType: 'PATH_CONFLICT_RESUME',
    aircraftName: 'A01',
    vehicleName: 'V01',
  },
})

assert.equal(resume.kind, 'resume')
assert.equal(resume.key, 'A01::V01')
assert.equal(resume.aircraftName, 'A01')
assert.equal(resume.vehicleName, 'V01')

const alert = normalizePathConflictMessage({
  type: 'path_conflict_alert',
  payload: {
    alertLevel: 'EMERGENCY',
    conflictPoint: { latitude: 36.1, longitude: 120.2 },
    aircraftName: 'A01',
    vehicleName: 'V01',
  },
})

assert.equal(alert.kind, 'alert')
assert.equal(alert.level, 'EMERGENCY')
assert.equal(alert.title, '紧急告警')

assert.equal(isNewPathConflictPayload({
  object1: { objectName: 'V01' },
  object2: { objectName: 'V02' },
  alertType: 'CONFLICT_WARNING',
}), false)

assert.equal(isNewPathConflictPayload({
  aircraftName: 'A01',
  vehicleName: 'V01',
  conflictPoint: { latitude: 36.1, longitude: 120.2 },
}), true)
