import type { Asset } from '../model/Asset'; import { type AssetType, isPlaceableType } from '../model/AssetTypes'; import { findAsset, type Project } from '../model/Project'; export const NO_ASSET_LABEL = 'None'; /** * The asset's name, "None" for the empty id, `?` when it no longer resolves – a dangling * reference shows its id rather than passing for an empty field. */ export function assetRefLabel(project: Project | null, id: string): string { if (id === '') return NO_ASSET_LABEL; return (project ? findAsset(project, id) : undefined)?.name ?? `${id}?`; } /** * The assets an `asset` property offers, in menu order; without `assetTypes` every placeable one. * Sorted by name so the order does not depend on creation time. */ export function assetRefChoices(project: Project, assetTypes?: readonly AssetType[]): Asset[] { const allowed = (asset: Asset): boolean => assetTypes ? assetTypes.includes(asset.type) : isPlaceableType(asset.type); return project.assets .filter(allowed) .sort((a, b) => a.name.localeCompare(b.name) || a.type.localeCompare(b.type)); } export function assetChoiceLabel(asset: Asset): string { return `${asset.name} (${asset.type})`; }