import { useState } from 'react';
import { useAppState, useAppDispatch } from '../context/AppContext.jsx';
import Header from '../components/Header.jsx';
import { DEFAULT_CONFIG } from '../utils/constants.js';
import { buildAndDownload } from '../utils/excelExport.js';
import { showInfoToast, showWarnToast } from '../components/Toast.jsx';
function getByPath(obj, path) {
if (!path) return obj;
return path.split('.').reduce((o, k) => (o && o[k] !== undefined ? o[k] : undefined), obj);
}
function setByPath(obj, path, value) {
if (!path) return value;
const keys = path.split('.');
const root = JSON.parse(JSON.stringify(obj));
let cur = root;
for (let i = 0; i < keys.length - 1; i++) { cur = cur[keys[i]]; }
cur[keys[keys.length - 1]] = value;
return root;
}
function ConfigNode({ obj, path, depth }) {
const dispatch = useAppDispatch();
const state = useAppState();
const [collapsed, setCollapsed] = useState(depth > 1);
function update(p, val) {
const newConfig = setByPath(state.config, p, val);
dispatch({ type: 'SET_CONFIG', payload: newConfig });
}
if (Array.isArray(obj)) {
return (
JSON.stringify(v)).join(', ')}
title="Edit comma-separated values"
onBlur={e => {
try {
const raw = e.target.value;
const parsed = raw.split(',').map(s => {
const t = s.trim();
try { return JSON.parse(t); } catch { return t; }
}).filter(v => v !== '');
update(path, parsed);
} catch (err) { showWarnToast('Invalid array format'); }
}}
/>
array[{obj.length}]
);
}
if (obj !== null && typeof obj === 'object') {
const keys = Object.keys(obj);
return (
{depth > 0 && (
setCollapsed(!collapsed)}
>
{collapsed ? '▶' : '▼'}
{'{' + keys.length + ' keys}'}
)}
{!collapsed && (
0 ? 'cfg-children' : ''}>
{keys.map(key => {
const childPath = path ? `${path}.${key}` : key;
const childVal = obj[key];
return (
);
})}
)}
);
}
// Primitive leaf
const isNum = typeof obj === 'number';
const isBool = typeof obj === 'boolean';
if (isBool) {
return (
);
}
return (
{
const raw = e.target.value;
update(path, isNum ? (isNaN(Number(raw)) ? raw : Number(raw)) : raw);
}}
/>
);
}
export default function Step7Config({ onBack, onNav }) {
const state = useAppState();
const dispatch = useAppDispatch();
const { config } = state;
function resetConfig() {
if (!window.confirm('Reset all config values to defaults?')) return;
dispatch({ type: 'SET_CONFIG', payload: JSON.parse(JSON.stringify(DEFAULT_CONFIG)) });
}
function download() {
const ok = buildAndDownload(state);
if (ok) showInfoToast('Download complete — Dabur_Model_Output.xlsx');
}
return (
Model Configuration
Edit values inline · Click ▶ to expand objects
);
}