import { hasRemoteConfig } from '@/utils/runtime-config.js' import { normalizeGenealogyPathId } from './genealogy-contract.js' import { normalizeLineagePersonDetail, normalizeLineagePersonIdentity, normalizeLineagePersonOptions, normalizeLineagePersonPage } 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 { createRequestError, requestStrict } from './request-client.js' const requireRemoteLineage = (message, code) => { if (hasRemoteConfig()) return throw createRequestError(message, code) } export const lineageApi = { async getTree(genealogyId, requestOptions = {}) { requireRemoteLineage( '世系树需要真实读取服务,当前本地预览不会伪造人物节点', 'REMOTE_READ_REQUIRED' ) 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 = {}) { requireRemoteLineage( '人物详情需要真实读取服务,当前本地预览不会伪造成员资料', 'REMOTE_READ_REQUIRED' ) 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 = {}) { requireRemoteLineage( '成员目录需要真实读取服务,当前本地预览不会伪造目录数据', 'REMOTE_READ_REQUIRED' ) 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 = {}) { requireRemoteLineage( '人物创建接口在本地预览模式不可用,当前内容不会保存', 'WRITE_UNAVAILABLE' ) 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 = {}) { requireRemoteLineage( '人物关系写入接口在本地预览模式不可用,当前内容不会保存', 'WRITE_UNAVAILABLE' ) 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 = {}) { requireRemoteLineage( '人物编辑接口在本地预览模式不可用,当前内容不会保存', 'WRITE_UNAVAILABLE' ) 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 deletePerson(genealogyId, personId, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const normalizedPersonId = normalizeResourcePathId(personId, '人物标识') requireRemoteLineage( '世系人物写入需要真实服务,当前本地预览不会伪造结果', 'REMOTE_WRITE_REQUIRED' ) 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 = {}) { requireRemoteLineage( '排行调整接口在本地预览模式不可用,当前内容不会保存', 'WRITE_UNAVAILABLE' ) 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) requireRemoteLineage( '世系人物选项需要真实读取服务,当前本地预览不会伪造候选项', 'REMOTE_READ_REQUIRED' ) const options = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/lineage/persons/options`, method: 'GET' }, { requestController: requestOptions.requestController ?? null }) return normalizeLineagePersonOptions(options, normalizedGenealogyId) } }