Spaces:
Sleeping
Sleeping
File size: 7,162 Bytes
ce8f04a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | /**
* Pure helpers for strategy cascade UI (no React / no network).
* Canonical path ids match backend/core/strategy/recommend.py.
*/
/** Tax sub-path ids from backend core/strategy/tax_paths.py */
export type TaxChecklistId = 'psi' | 'ulga_br' | 'ip_box' | string;
export type StrategyPathView = {
id: string;
label: string;
score: number;
reasons: string[];
primary: boolean;
de_minimis_blocked: boolean;
/** Tax path (`tax_psi_ulgi`) only — primary checklist ref (usually psi) */
checklist_id?: string | null;
/** Tax path only — ordered ids: psi, ulga_br, ip_box */
checklist_ids?: TaxChecklistId[];
/** PSI guardrail — never suggest submit to PARP */
never_say_submit_to_parp?: boolean;
};
export type StrategyResultView = {
paths: StrategyPathView[];
primaryPathId: string | null;
recommendedPathIds: string[];
deMinimisExhausted: boolean;
mspStatus: string | null;
deMinimisUsedEur: number | null;
notes: string[];
cascadeOrder: string[];
};
export const CANONICAL_CASCADE_ORDER = [
'tax_psi_ulgi',
'regional_fst',
'national_feng',
'debt_loan_guarantee',
'direct_eu_prep',
] as const;
export type CanonicalPathId = (typeof CANONICAL_CASCADE_ORDER)[number];
/** Normalize API strategy payload into a stable view model. Always ≥4 paths when source has them. */
export function normalizeStrategyResult(
raw: Record<string, unknown> | null | undefined,
): StrategyResultView {
const empty: StrategyResultView = {
paths: [],
primaryPathId: null,
recommendedPathIds: [],
deMinimisExhausted: false,
mspStatus: null,
deMinimisUsedEur: null,
notes: [],
cascadeOrder: [...CANONICAL_CASCADE_ORDER],
};
if (!raw || typeof raw !== 'object') return empty;
const pathsIn = Array.isArray(raw.paths) ? raw.paths : [];
const paths: StrategyPathView[] = pathsIn
.filter((p): p is Record<string, unknown> => !!p && typeof p === 'object')
.map((p) => {
const checklistIds = Array.isArray(p.checklist_ids)
? p.checklist_ids.map((x) => String(x)).filter(Boolean)
: undefined;
const view: StrategyPathView = {
id: String(p.id || ''),
label: String(p.label || p.id || 'Ścieżka'),
score: Number(p.score) || 0,
reasons: Array.isArray(p.reasons)
? p.reasons.map((r) => String(r)).filter(Boolean)
: [],
primary: Boolean(p.primary),
de_minimis_blocked: Boolean(p.de_minimis_blocked),
};
if (checklistIds && checklistIds.length > 0) {
view.checklist_ids = checklistIds;
}
if (p.checklist_id != null && p.checklist_id !== '') {
view.checklist_id = String(p.checklist_id);
} else if (checklistIds && checklistIds.length > 0) {
view.checklist_id = checklistIds[0];
}
if (p.never_say_submit_to_parp != null) {
view.never_say_submit_to_parp = Boolean(p.never_say_submit_to_parp);
}
return view;
})
.filter((p) => p.id);
// Sort by score desc; keep blocked paths visible (not filtered out)
paths.sort((a, b) => b.score - a.score);
const primaryFromFlag = paths.find((p) => p.primary)?.id ?? null;
const primaryPathId =
(typeof raw.primary_path_id === 'string' && raw.primary_path_id) || primaryFromFlag;
// Ensure exactly one primary flag matches primary_path_id
if (primaryPathId) {
for (const p of paths) {
p.primary = p.id === primaryPathId && !p.de_minimis_blocked;
}
// if primary was a blocked path, fall back to highest non-blocked
if (!paths.some((p) => p.primary)) {
const fallback = paths.find((p) => !p.de_minimis_blocked);
if (fallback) fallback.primary = true;
}
}
const rec = Array.isArray(raw.recommended_paths) ? raw.recommended_paths : [];
const recommendedPathIds = rec
.map((p) => (p && typeof p === 'object' ? String((p as { id?: string }).id || '') : ''))
.filter(Boolean);
const elig =
raw.eligibility_summary && typeof raw.eligibility_summary === 'object'
? (raw.eligibility_summary as Record<string, unknown>)
: {};
const cascadeOrder = Array.isArray(raw.cascade_order)
? raw.cascade_order.map((x) => String(x))
: [...CANONICAL_CASCADE_ORDER];
const notes = Array.isArray(raw.notes) ? raw.notes.map((n) => String(n)) : [];
return {
paths,
primaryPathId: paths.find((p) => p.primary)?.id ?? primaryPathId,
recommendedPathIds:
recommendedPathIds.length > 0
? recommendedPathIds
: paths.filter((p) => !p.de_minimis_blocked).slice(0, 4).map((p) => p.id),
deMinimisExhausted: Boolean(elig.de_minimis_exhausted),
mspStatus: elig.msp != null ? String(elig.msp) : null,
deMinimisUsedEur:
typeof elig.de_minimis_used_eur === 'number' ? elig.de_minimis_used_eur : null,
notes,
cascadeOrder,
};
}
/** Paths the UI should show as cards — all scored paths (including blocked). */
export function pathsForCards(view: StrategyResultView): StrategyPathView[] {
return view.paths.slice();
}
export function isBlockedPath(path: StrategyPathView): boolean {
return Boolean(path.de_minimis_blocked);
}
export function formatScore(score: number): string {
if (!Number.isFinite(score)) return '—';
return Math.round(score).toString();
}
/** Human-readable short labels for tax checklist chips (UI only). */
export const TAX_CHECKLIST_LABELS: Record<string, string> = {
psi: 'PSI',
ulga_br: 'Ulga B+R',
ip_box: 'IP Box',
};
export function formatTaxChecklistId(id: string): string {
return TAX_CHECKLIST_LABELS[id] || id;
}
/** True when path carries tax checklist_ids from attach_tax_checklists. */
export function hasTaxChecklists(path: StrategyPathView): boolean {
return Array.isArray(path.checklist_ids) && path.checklist_ids.length > 0;
}
/**
* Decide which path should become the active selection after a recommend load.
* - If the user already picked a still-valid non-blocked path, keep it (no auto change).
* - Otherwise prefer primary (non-blocked), else highest non-blocked score.
* Returns the path to select, or null when nothing to change/notify.
*/
export function resolveAutoSelectedPath(
view: StrategyResultView,
currentSelected: string | null | undefined,
userHasPicked: boolean,
): StrategyPathView | null {
if (userHasPicked && currentSelected) {
const still = view.paths.find(
(p) => p.id === currentSelected && !isBlockedPath(p),
);
if (still) return null;
}
if (currentSelected) {
const still = view.paths.find(
(p) => p.id === currentSelected && !isBlockedPath(p),
);
if (still && !userHasPicked) {
// Keep current (e.g. parent already holds primary) — no re-notify needed
// only if it is already the primary/chosen; still return null to avoid loops
return null;
}
}
const primary =
view.paths.find((p) => p.primary && !isBlockedPath(p)) ||
view.paths.find((p) => p.id === view.primaryPathId && !isBlockedPath(p)) ||
view.paths.find((p) => !isBlockedPath(p));
if (!primary) return null;
if (primary.id === currentSelected) return null;
return primary;
}
|