/** 统一实体详情只读 API。路径规划与响应正规化集中在此,组件/store 不认识后端信封。 */ import { requestCockpitJson } from './cockpitApi.js' import { ENTITY_DETAIL_KIND, entityIdentity, normalizeEntityDetail } from '../domain/entityDetail.js' export const ENTITY_DETAIL_PATHS = Object.freeze({ [ENTITY_DETAIL_KIND.FACILITY]: (id, target) => target.category ? `/infrastructure/entities/${encodeURIComponent(id)}/detail?category=${encodeURIComponent(target.category)}` : '', [ENTITY_DETAIL_KIND.EMERGENCY]: (id, target) => target.category ? `/infrastructure/entities/${encodeURIComponent(id)}/detail?category=${encodeURIComponent(target.category)}` : '', [ENTITY_DETAIL_KIND.EVENT]: (id, target) => target.family ? `/events/${encodeURIComponent(id)}/detail?family=${encodeURIComponent(target.family)}` : '', [ENTITY_DETAIL_KIND.WORK_ORDER]: (id) => `/work-orders/${encodeURIComponent(id)}/detail`, }) export function entityDetailPath(identityInput) { const identity = entityIdentity(identityInput) const build = ENTITY_DETAIL_PATHS[identity.kind] return identity.valid && build ? build(identity.id, identityInput || {}) : '' } export async function fetchEntityDetail(identityInput, options = {}) { const identity = entityIdentity(identityInput) const path = options.path || entityDetailPath(identity) if (!identity.valid || !path) return { ok: false, identity, reason: 'invalid-identity', mode: '', data: null } const result = await requestCockpitJson(path, { config: options.config, fetchImpl: options.fetchImpl }) if (!result.ok) return { ...result, identity, data: null } const detail = normalizeEntityDetail(identity, result.data, options.prefill) return detail ? { ok: true, identity, mode: result.mode || '', path: result.path || path, data: detail } : { ok: false, identity, mode: result.mode || '', path: result.path || path, reason: 'bad-payload', data: null } }