import {setupPopover} from './popover'; const roiCache: Record> = {}; async function getROI(cx: string) { const response = await fetch(`/roi_${cx.toLowerCase()}.json`); const lastModified = new Date(response.headers.get('last-modified')!); const profits = await response.json(); return {lastModified, profits}; } type MetricType = 'vwap' | 'bid' | 'ask'; const lowVolume = document.querySelector('input#low-volume') as HTMLInputElement; const cxSelect = document.querySelector('select#cx') as HTMLSelectElement; const expertise = { AGRICULTURE: 'agri', CHEMISTRY: 'chem', CONSTRUCTION: 'const', ELECTRONICS: 'elec', FOOD_INDUSTRIES: 'food ind', FUEL_REFINING: 'fuel', MANUFACTURING: 'mfg', METALLURGY: 'metal', RESOURCE_EXTRACTION: 'res ext', } as const; const expertiseSelect = document.querySelector('select#expertise') as HTMLSelectElement; for (const key of Object.keys(expertise)) { const option = document.createElement('option'); option.value = key; option.textContent = key.replace('_', ' ').toLowerCase(); expertiseSelect.appendChild(option); } const buildingSelect = document.querySelector('select#building') as HTMLSelectElement; const formatSigFig = new Intl.NumberFormat(undefined, { notation: 'compact', maximumSignificantDigits: 3, }).format; if (localStorage.getItem('roi-cx')) cxSelect.value = localStorage.getItem('roi-cx')!; if (localStorage.getItem('roi-expertise')) expertiseSelect.value = localStorage.getItem('roi-expertise')!; if (localStorage.getItem('roi-low-volume')) lowVolume.checked = localStorage.getItem('roi-low-volume') === 'true'; let savedBuilding = localStorage.getItem('roi-building') || ''; let currentSortKey: keyof ProfitWithMetrics | 'outputs' = (localStorage.getItem('roi-sort-key') as any) || 'break_even'; let currentSortAsc: boolean = localStorage.getItem('roi-sort-asc') !== 'false'; let headersInitialized = false; let metricControlsInitialized = false; let capexMetric: MetricType = (localStorage.getItem('roi-capex-metric') as MetricType) || 'vwap'; let opexMetric: MetricType = (localStorage.getItem('roi-opex-metric') as MetricType) || 'vwap'; let revenueMetric: MetricType = (localStorage.getItem('roi-revenue-metric') as MetricType) || 'vwap'; let includeShips: boolean = localStorage.getItem('roi-include-ships') === 'true'; let workingCapitalDays: number = parseInt(localStorage.getItem('roi-working-capital') || '3', 10); let targetPermit: number = parseInt(localStorage.getItem('roi-target-permit') || '2', 10); async function render() { const tbody = document.querySelector('tbody')!; tbody.innerHTML = ''; const cx = cxSelect.value; if (!roiCache[cx]) roiCache[cx] = getROI(cx); const {lastModified, profits} = await roiCache[cx]; if (!metricControlsInitialized) { const controls = document.createElement('div'); controls.style.marginBottom = '15px'; // EXTREME DETAIL: Re-arranged the `