import { normalizeGenealogyPathId } from './genealogy-contract.js' import { normalizeLineagePersonDetail, normalizeLineagePersonIdentity, normalizeLineagePersonOptions, normalizeLineagePersonPage, normalizeLineageSensitiveProfile } from './lineage-person-contract.js' import { normalizeLineageTree } from './lineage-tree-contract.js' import { lineageRelationPath, normalizeLineageWritePayload } from './lineage-write-contract.js' import { normalizeResourcePathId } from './request-normalizers.js' import { requestStrict } from './request-client.js' export const lineageApi = { async getTree(genealogyId, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const tree = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/lineage/tree`, method: 'GET' }, { requestController: requestOptions.requestController ?? null }) return normalizeLineageTree(tree) }, async getPerson(genealogyId, personId, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const normalizedPersonId = normalizeLineagePersonIdentity(personId, '人物标识') const person = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/lineage/persons/${normalizedPersonId}`, method: 'GET' }, { requestController: requestOptions.requestController ?? null }) return normalizeLineagePersonDetail(person, normalizedGenealogyId, normalizedPersonId) }, async getPersonPage(genealogyId, { pageNum = 1, pageSize = 10, keyword = '' } = {}, requestOptions = {}) { if (!Number.isSafeInteger(pageNum) || pageNum < 1) throw new TypeError('成员目录页码无效') if (!Number.isSafeInteger(pageSize) || pageSize < 1) throw new TypeError('成员目录页大小无效') if (typeof keyword !== 'string') throw new TypeError('成员目录关键词无效') const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const page = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/lineage/persons/page`, method: 'GET', data: { pageNum, pageSize, ...(keyword.trim() ? { keyword: keyword.trim() } : {}) } }, { requireData: false, requestController: requestOptions.requestController ?? null }) return normalizeLineagePersonPage(page, normalizedGenealogyId) }, async createPerson(genealogyId, payload, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) return requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/lineage/persons`, method: 'POST', data: normalizeLineageWritePayload(payload) }, { requestController: requestOptions.requestController ?? null }) }, async createRelatedPerson(genealogyId, personId, relationType, payload, requestOptions = {}) { const relationPath = lineageRelationPath[relationType] if (!relationPath) throw new TypeError('人物关系类型不属于当前合同') const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const normalizedPersonId = normalizeLineagePersonIdentity(personId, '人物标识') return requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/lineage/persons/${normalizedPersonId}/${relationPath}`, method: 'POST', data: normalizeLineageWritePayload(payload) }, { requestController: requestOptions.requestController ?? null }) }, async updatePerson(genealogyId, personId, payload, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const normalizedPersonId = normalizeLineagePersonIdentity(personId, '人物标识') return requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/lineage/persons/${normalizedPersonId}`, method: 'PUT', data: normalizeLineageWritePayload(payload) }, { requestController: requestOptions.requestController ?? null }) }, async getSensitiveProfile(genealogyId, personId, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const normalizedPersonId = normalizeLineagePersonIdentity(personId, '人物标识') const profile = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/lineage/persons/${normalizedPersonId}/sensitive-profile`, method: 'GET' }, { requestController: requestOptions.requestController ?? null }) return normalizeLineageSensitiveProfile(profile, normalizedPersonId) }, async saveSensitiveProfile(genealogyId, personId, hereditaryMedicalHistory, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const normalizedPersonId = normalizeLineagePersonIdentity(personId, '人物标识') const normalizedHistory = typeof hereditaryMedicalHistory === 'string' ? hereditaryMedicalHistory.trim() : '' if (!normalizedHistory) throw new TypeError('遗传病史不能为空') if (normalizedHistory.length > 500) throw new TypeError('遗传病史长度超出当前页面合同') const profile = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/lineage/persons/${normalizedPersonId}/sensitive-profile`, method: 'PUT', data: { hereditaryMedicalHistory: normalizedHistory } }, { requestController: requestOptions.requestController ?? null }) return normalizeLineageSensitiveProfile(profile, normalizedPersonId) }, async clearSensitiveProfile(genealogyId, personId, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const normalizedPersonId = normalizeLineagePersonIdentity(personId, '人物标识') const profile = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/lineage/persons/${normalizedPersonId}/sensitive-profile`, method: 'DELETE' }, { requestController: requestOptions.requestController ?? null }) return normalizeLineageSensitiveProfile(profile, normalizedPersonId) }, async deletePerson(genealogyId, personId, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const normalizedPersonId = normalizeResourcePathId(personId, '人物标识') await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/lineage/persons/${normalizedPersonId}`, method: 'DELETE' }, { requireData: false, requestController: requestOptions.requestController ?? null }) return null }, async updatePersonSortOrder(genealogyId, personId, sortOrder, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const normalizedPersonId = normalizeLineagePersonIdentity(personId, '人物标识') const normalizedSortOrder = Number(sortOrder) if (!Number.isSafeInteger(normalizedSortOrder)) throw new TypeError('排序值必须是整数') const person = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/lineage/persons/${normalizedPersonId}/sort-order`, method: 'PUT', data: { sortOrder: normalizedSortOrder } }, { requestController: requestOptions.requestController ?? null }) return normalizeLineagePersonDetail(person, normalizedGenealogyId, normalizedPersonId) }, async getLineagePersonOptions(genealogyId, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const options = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/lineage/persons/options`, method: 'GET' }, { requestController: requestOptions.requestController ?? null }) return normalizeLineagePersonOptions(options, normalizedGenealogyId) } }