| 1 | import type { SectorPerformance, SectorPerformanceStock } from "./portfolio-api"; |
| 2 | export type PerformanceRowTone = "positive" | "negative" | "neutral"; |
| 3 | export type MarketIntelligenceSelection = { |
| 4 | stock: SectorPerformanceStock; |
| 5 | region: SectorPerformance["region"]; |
| 6 | period: SectorPerformance["period"]; |
| 7 | }; |
| 8 | |
| 9 | export function marketIntelligenceSelection( |
| 10 | performance: SectorPerformance, |
| 11 | stock: SectorPerformanceStock, |
| 12 | ): MarketIntelligenceSelection { |
| 13 | return { stock, region: performance.region, period: performance.period }; |
| 14 | } |
| 15 | |
| 16 | export function regionalWatchlistName(region: SectorPerformance["region"]): string { |
| 17 | return ({ INDIA: "WATCHLIST-IND", EUROPE: "WATCHLIST-EU", USA: "WATCHLIST-USA" })[region]; |
| 18 | } |
| 19 | |
| 20 | export function performanceRowTone(performancePct: number | null | undefined): PerformanceRowTone { |
| 21 | if (typeof performancePct !== "number" || !Number.isFinite(performancePct) || performancePct === 0) { |
| 22 | return "neutral"; |
| 23 | } |
| 24 | return performancePct > 0 ? "positive" : "negative"; |
| 25 | } |
| 26 | |
| 27 | export function formatSignedPerformancePct(performancePct: number | null | undefined): string { |
| 28 | if (typeof performancePct !== "number" || !Number.isFinite(performancePct)) return "--"; |
| 29 | if (performancePct > 0) return `+${performancePct.toFixed(2)}%`; |
| 30 | return `${performancePct.toFixed(2)}%`; |
| 31 | } |
| 32 | |
| 33 | export function performanceDirectionLabel(stock: SectorPerformanceStock): string { |
| 34 | const tone = performanceRowTone(stock.performancePct); |
| 35 | return tone === "positive" ? "positive return" : tone === "negative" ? "negative return" : "neutral return"; |
| 36 | } |