import { adaptTechnicianProfile, getOpenidFromStorage, normalizeStorageValue } from './technicianProfile'

describe('technicianProfile', () => {
	test('adapts profile response data to legacy merchant shape', () => {
		const result = adaptTechnicianProfile({
			merchantId: 12,
			name: '靳世辉',
			sex: 1,
			phone: '19834500372',
			address: null,
			areaCode: null,
			avatar: '',
			provinceCode: '110000',
			provinceName: '北京市',
			cityCode: '110000',
			cityName: '北京市',
			operationCenterId: 8801,
			operationCenterName: '华北区运营中心',
			auditStatus: 2,
			serviceTag: '1',
			nickName: {
				value: '小靳',
				pendingValue: '测试',
				auditStatus: 0,
				auditStatusText: '审核中',
				auditRemark: null,
				editable: false,
			},
			brief: {
				value: '这是我的简介',
				pendingValue: null,
				auditStatus: null,
				auditStatusText: null,
				auditRemark: null,
				editable: true,
			},
			fileGroups: [
				{
					fileType: '1',
					fileTypeName: '形象照',
					officialFiles: [
						{
							id: 302,
							fileName: '形象照',
							fileUrl: 'https://example.com/a.jpeg',
							fileSize: 9.12,
							contentType: 'image/jpeg',
							applyBatchNo: null,
							auditStatus: 1,
							auditRemark: null,
						},
					],
					pendingFiles: [
						{
							id: 319,
							fileName: '待审形象照',
							fileUrl: 'https://example.com/pending.jpeg',
							fileSize: 54.45,
							contentType: 'jpeg',
							applyBatchNo: 'batch',
							auditStatus: 0,
							auditRemark: null,
						},
					],
				},
				{
					fileType: '2',
					fileTypeName: '生活照',
					officialFiles: [
						{
							id: 308,
							fileName: '生活照1',
							fileUrl: 'https://example.com/life.png',
							fileSize: 28.17,
							contentType: 'image/png',
							applyBatchNo: null,
							auditStatus: 1,
							auditRemark: null,
						},
					],
					pendingFiles: [],
				},
			],
		})

		expect(result.merchant).toMatchObject({
			id: 12,
			merchantId: 12,
			teName: '靳世辉',
			teSex: 1,
			tePhone: '19834500372',
			teNickName: '小靳',
			teBrief: '这是我的简介',
			provinceCode: '110000',
			provinceName: '北京市',
			cityCode: '110000',
			cityName: '北京市',
			operationCenterId: 8801,
			operationCenterName: '华北区运营中心',
			auditStatus: 2,
			serviceTag: '1',
			teNickNameAuditStatus: 0,
			teBriefAuditStatus: null,
		})
		expect(result.merchantAuditFile).toEqual([
			{
				id: 302,
				fileName: '形象照',
				fileUrl: 'https://example.com/a.jpeg',
				fileSize: 9.12,
				contentType: 'image/jpeg',
				applyBatchNo: null,
				auditStatus: 1,
				auditRemark: null,
				fileType: '1',
				fileTypeName: '形象照',
			},
			{
				id: 308,
				fileName: '生活照1',
				fileUrl: 'https://example.com/life.png',
				fileSize: 28.17,
				contentType: 'image/png',
				applyBatchNo: null,
				auditStatus: 1,
				auditRemark: null,
				fileType: '2',
				fileTypeName: '生活照',
			},
		])
	})

	test('reads openid from wx_copenid storage key', () => {
		const storage = {
			getItem(key) {
				return key === 'wx_copenid' ? 'openid-value' : ''
			},
		}

		expect(getOpenidFromStorage(storage)).toBe('openid-value')
	})

	test('normalizes numeric storage values to plain strings', () => {
		expect(normalizeStorageValue(12)).toBe('12')
		expect(normalizeStorageValue(1)).toBe('1')
		expect(normalizeStorageValue('2')).toBe('2')
		expect(normalizeStorageValue(null)).toBe('')
		expect(normalizeStorageValue(undefined)).toBe('')
	})
})
