export function adaptTechnicianProfile(profile = {}) {
	const merchant = {
		...profile,
		id: profile.merchantId,
		merchantId: profile.merchantId,
		teName: profile.name || '',
		teSex: profile.sex,
		tePhone: profile.phone || '',
		teNickName: profile.nickName?.value || '',
		teNickNamePendingValue: profile.nickName?.pendingValue || '',
		teNickNameAuditStatus: profile.nickName?.auditStatus ?? null,
		teNickNameAuditStatusText: profile.nickName?.auditStatusText || '',
		teNickNameAuditRemark: profile.nickName?.auditRemark || '',
		teBrief: profile.brief?.value || '',
		teBriefPendingValue: profile.brief?.pendingValue || '',
		teBriefAuditStatus: profile.brief?.auditStatus ?? null,
		teBriefAuditStatusText: profile.brief?.auditStatusText || '',
		teBriefAuditRemark: profile.brief?.auditRemark || '',
		avatar: profile.avatar || '',
		cPortrait: profile.avatar || '',
		auditStatus: profile.auditStatus,
		serviceTag: profile.serviceTag,
	}

	return {
		merchant,
		merchantAuditFile: getOfficialFiles(profile.fileGroups),
		fileGroups: Array.isArray(profile.fileGroups) ? profile.fileGroups : [],
	}
}

export function getOfficialFiles(fileGroups) {
	if (!Array.isArray(fileGroups)) return []

	return fileGroups.reduce((list, group) => {
		const officialFiles = Array.isArray(group.officialFiles) ? group.officialFiles : []
		const files = officialFiles.map(file => ({
			...file,
			fileType: group.fileType,
			fileTypeName: group.fileTypeName,
		}))
		return list.concat(files)
	}, [])
}

export function getOpenidFromStorage(storage) {
	if (!storage || typeof storage.getItem !== 'function') return ''
	return storage.getItem('wx_copenid') || ''
}

export function normalizeStorageValue(value) {
	if (value === null || value === undefined) return ''
	return String(value)
}
