import { apiRequest, listAssets } from './api'; import type { AssetDetail, AssetListItem } from './api'; export interface InventoryMergeAlias { id: string; code: string; name: string; reason: string; mergedAt: string; depth: number; } export interface InventoryMergeStatus { requested: { id: string; code: string; name: string; typeCode: string; }; isMerged: boolean; canonical: { id: string; code: string; name: string; typeCode: string; }; chain: Array<{ id: string; sourceAssetId: string; canonicalAssetId: string; reason: string; mergedAt: string; mergedBy: string | null; source: string; }>; aliases: InventoryMergeAlias[]; } export interface InventoryMergeResult { merge: { id: string; sourceAssetId: string; canonicalAssetId: string; reason: string; mergedAt: string; }; source: { id: string; code: string; name: string }; canonical: { id: string; code: string; name: string }; sourceVersionNumber: number; reparentedChildIds: string[]; historyPolicy: 'HISTORICAL_REFERENCES_PRESERVED'; } export function getInventoryMergeStatus(assetId: string) { return apiRequest(`/assets/${assetId}/merge-status`); } export function mergeInventoryAsset( sourceAssetId: string, canonicalAssetId: string, reason: string, ) { return apiRequest(`/assets/${sourceAssetId}/merge`, { method: 'POST', body: JSON.stringify({ canonicalAssetId, reason }), }); } export async function searchInventoryMergeCandidates( source: AssetDetail, search = '', ): Promise { if (!source.parent?.id || !source.operationalArea?.id || !source.operatorCompany?.id) return []; const page = await listAssets({ page: 1, pageSize: 80, search: search.trim() || undefined, typeId: source.type.id, parentId: source.parent.id, operationalAreaId: source.operationalArea.id, operatorCompanyId: source.operatorCompany.id, }); return page.data.filter((candidate) => candidate.id !== source.id && candidate.informationStatus !== 'INACTIVE', ); }