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


/* ------------------------------------------------------------
   CustomEquipmentModal — lets the user add a freeform item
   with any name, rating, avail, and cost. Useful for homebrew,
   splatbook items, or anything not in the Core catalog.
   ------------------------------------------------------------ */
window.CustomEquipmentModal = function CustomEquipmentModal({ kind, title, categories, onAdd, onClose }) {
    const [name, setName] = useState('');
    const [category, setCategory] = useState(categories[0]?.key || 'custom');
    const [rating, setRating] = useState('');
    const [avail, setAvail] = useState('');
    const [nuyen, setNuyen] = useState(0);
    const [description, setDescription] = useState('');

    const canSubmit = name.trim().length > 0;

    const footer = (
        <>
            <div className="footer-info">enter the stats you know — blank fields become "—"</div>
            <div className="footer-actions">
                <button className="btn btn-ghost" onClick={onClose}>Cancel</button>
                <button className="btn btn-primary" disabled={!canSubmit}
                        onClick={() => onAdd({
                            name: name.trim(),
                            category,
                            rating: rating ? parseInt(rating, 10) : null,
                            avail: avail.trim() || '—',
                            nuyen: parseInt(nuyen, 10) || 0,
                            description: description.trim(),
                        })}>Add</button>
            </div>
        </>
    );

    return (
        <Modal title={`Custom ${title} Item`} onClose={onClose} footer={footer}>
            <div style={{ padding: '20px 24px', width: '100%', overflowY: 'auto' }}>
                <FormRow label="Name">
                    <TextInput value={name} onChange={setName} placeholder="e.g. Custom Jammer" maxLength={80} />
                </FormRow>
                <FormRow label="Category">
                    <select className="input" value={category} onChange={e => setCategory(e.target.value)}>
                        {categories.map(c => (
                            <option key={c.key} value={c.key}>{c.label}</option>
                        ))}
                    </select>
                </FormRow>
                <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr 1fr', gap: 12 }}>
                    <FormRow label="Rating" hint="(optional)">
                        <TextInput value={rating} onChange={setRating} placeholder="e.g. 3" maxLength={4} />
                    </FormRow>
                    <FormRow label="Avail">
                        <TextInput value={avail} onChange={setAvail} placeholder="e.g. 8R" maxLength={10} />
                    </FormRow>
                    <FormRow label="Cost (¥)">
                        <input className="input" type="number" min="0" value={nuyen}
                               onChange={e => setNuyen(e.target.value)} />
                    </FormRow>
                </div>
                <FormRow label="Description" hint="(optional)">
                    <TextArea value={description} onChange={setDescription} rows={3}
                              placeholder="What does this item do?" />
                </FormRow>
            </div>
        </Modal>
    );
};
