/* ============================================================
   Gear —  Helpers
   Extracted from components.jsx via split.py.
   Each component reaches React hooks via window.React.
   ============================================================ */
const { useState, useEffect, useRef, useMemo, useCallback, Fragment } = React;


/* Helper used inside render functions — kept outside the component to avoid re-creating it every render. */
window.formatStat = function formatStat(v, fallback = '—') {
    if (v === null || v === undefined || v === '') return fallback;
    return String(v);
}

/* ------------------------------------------------------------
   CareerGear — shared utilities for gear tabs in career mode.
   
   Each tab uses the same pattern: user picks template → we
   stage pending gear entry → commit happens via pending bar.
   This helper centralizes the nuyen affordability check and
   the pending-gear row lookup so we don't copy/paste the
   same code in seven tabs.
   ------------------------------------------------------------ */
window.CareerGear = {
    /* Compute nuyen cost for a given template + extras.
       Handles the cost/cost/costPerRating variations across gear types. */
    computeNuyenCost(template, extras = {}) {
        if (!template) return 0;
        if (typeof template.cost === 'number') {
            /* Rated items (cyberware, bioware) scale cost by rating */
            const r = extras.rating || 1;
            return template.cost * r;
        }
        return 0;
    },

    /* Build an essence cost for cyberware/bioware including grade multiplier. */
    computeEssenceCost(template, extras = {}) {
        if (!template || typeof template.essence !== 'number') return 0;
        const A = window.SR5_AUGS;
        const grade = A?.findGrade(extras.grade || 'standard');
        const r = extras.rating || 1;
        const mul = grade?.essMul ?? 1;
        return Math.round(template.essence * r * mul * 1000) / 1000;
    },

    /* Apply grade cost multiplier to nuyen for ware purchases. */
    computeWareNuyenCost(template, extras = {}) {
        if (!template || typeof template.cost !== 'number') return 0;
        const A = window.SR5_AUGS;
        const grade = A?.findGrade(extras.grade || 'standard');
        const r = extras.rating || 1;
        const mul = grade?.costMul ?? 1;
        return Math.round(template.cost * r * mul);
    },

    /* Check if a nuyen amount fits in the remaining budget including already-staged. */
    canAfford(character, nuyenCost) {
        const C = window.SR5_CALC;
        const pending = C.pendingNuyenDelta(character);
        const avail = C.nuyenAvailable(character);
        return pending + nuyenCost <= avail;
    },

    /* Get pending gear rows for a specific kind (or multiple kinds). */
    pendingOfKind(character, kinds) {
        const C = window.SR5_CALC;
        const all = C.pendingChanges(character).pendingGear || [];
        const set = Array.isArray(kinds) ? new Set(kinds) : new Set([kinds]);
        return all.filter(g => set.has(g.kind));
    },
};
