import {getPath} from '@/lib/resourcePath' import type {CloudResource} from '@/types/resource' import type {ServiceSchema} from '@/types/schema' /** * "This also deletes 2 users and 1 app client." for the delete confirmation, * summed over the selected resources. A count is a number at the path, or the * length of a list there; anything else, and zero counts, are left out. */ export function describeDeleteImpact(impact: ServiceSchema['deleteImpact'], resources: CloudResource[]): string | null { if (!impact?.length) return null const parts = impact.flatMap(({path, singular, plural}) => { const total = resources.reduce((sum, resource) => { const value = getPath(resource, path) if (Array.isArray(value)) return sum + value.length return typeof value === 'number' && Number.isFinite(value) ? sum + value : sum }, 0) return total > 0 ? [`${total} ${total === 1 ? singular : plural}`] : [] }) if (parts.length === 0) return null const list = parts.length === 1 ? parts[0] : `${parts.slice(0, -1).join(', ')} and ${parts[parts.length - 1]}` return `This also deletes ${list}.` }