import assert from 'node:assert/strict'
import { normalizePage, normalizeMonitoringRecord, normalizeMonitoringTypeName, normalizeGeoJson, normalizeMapPoints } from '../src/utils/waterSupplyModel.js'

const page = normalizePage({ data: { records: [{ id: 1 }], total: 4, current: 2, size: 10 } })
assert.equal(page.records.length, 1)
assert.equal(page.total, 4)
assert.equal(page.current, 2)

const record = normalizeMonitoringRecord({ equipmentId: 'eq-1', deviceName: '一号泵', currentValue: '2.5' }, 'pressure')
assert.equal(record.id, 'eq-1')
assert.equal(record.value, 2.5)
assert.equal(record.unit, 'MPa')

const flowRecord = normalizeMonitoringRecord({ equipmentId: 'flow-1', instantFlow: '18.6' }, 'flow')
assert.equal(flowRecord.value, 18.6)

const pressureRecord = normalizeMonitoringRecord({ equipmentId: 'pressure-1', pressureValue: 0.42 }, 'pressure')
assert.equal(pressureRecord.value, 0.42)

const typedRecord = normalizeMonitoringRecord({ equipmentId: 'flow-2', typeName: '流量监测(测试)', instantFlow: 8.2 }, 'flow')
assert.equal(typedRecord.equipmentTypeName, '流量监测(测试)')

const fallbackTypeRecord = normalizeMonitoringRecord({ equipmentId: 'flow-3', equipmentTypeName: '  ', instantFlow: 1.2 }, 'flow', 'Flow Monitor')
assert.equal(fallbackTypeRecord.equipmentTypeName, 'Flow Monitor')
assert.equal(normalizeMonitoringTypeName({ typeName: 'Pressure Monitor' }, 'Flow Monitor'), 'Pressure Monitor')

assert.equal(normalizeGeoJson({ data: { type: 'FeatureCollection', features: [] } }).type, 'FeatureCollection')
assert.equal(normalizeGeoJson({ data: [] }).features.length, 0)

const mapPoints = normalizeMapPoints([
  { id: 1, longitude: 110.1, latitude: 28.1 },
  { id: 2, longitude: 110.3, latitude: 28.4 }
])
assert.deepEqual(mapPoints.map(item => [item.mapX, item.mapY]), [[6, 90], [94, 8]])
assert.deepEqual(normalizeMapPoints([{ id: 1, longitude: 110.2, latitude: 28.2 }]).map(item => [item.mapX, item.mapY]), [[50, 50]])
assert.deepEqual(normalizeMapPoints([{ id: 1, longitude: null, latitude: undefined }]), [])
console.log('waterSupplyModel tests passed')
