import { useState } from 'react' import { Box, Button, Card, Slider, Typography } from '@mui/material' import { RotateCcw } from 'lucide-react' import { WEIGHTING_KEYS, WEIGHTING_LABELS } from '../../domain/needBuilder' import type { WeightingKey } from '../../domain/needBuilder' import { weightingService } from '../../services/weightingService' interface Props { weights: Record onChange: (weights: Record) => void assetType?: string } const IMPORTANCE_LABELS = ['', 'Unwichtig', 'Wenig wichtig', 'Wichtig', 'Sehr wichtig', 'Entscheidend'] function toRaw(w: Record): Record { const max = Math.max(...WEIGHTING_KEYS.map(k => w[k] ?? 0)) if (max === 0) return Object.fromEntries(WEIGHTING_KEYS.map(k => [k, 3])) as Record return Object.fromEntries( WEIGHTING_KEYS.map(k => [k, Math.max(1, Math.round(((w[k] ?? 0) / max) * 5))]) ) as Record } function rawToWeights(raw: Record): Record { const total = WEIGHTING_KEYS.reduce((s, k) => s + (raw[k] ?? 1), 0) return Object.fromEntries( WEIGHTING_KEYS.map(k => [k, (raw[k] ?? 1) / total]) ) as Record } export function WeightingEditor({ weights, onChange, assetType }: Props) { const [raw, setRaw] = useState>(() => toRaw(weights)) function handleSlider(key: WeightingKey, value: number) { const updated = { ...raw, [key]: value } setRaw(updated) onChange(rawToWeights(updated)) } function handleReset() { const defaults = weightingService.getDefaultWeights(assetType) setRaw(toRaw(defaults)) onChange(defaults) } return ( Wichtigkeit der Kriterien Schieber nach rechts = wichtiger. Gewichtung wird automatisch berechnet. {WEIGHTING_KEYS.map(key => { const importance = raw[key] ?? 3 return ( {WEIGHTING_LABELS[key]} {IMPORTANCE_LABELS[importance]} handleSlider(key, v as number)} size="small" sx={{ color: '#1e3a5f' }} /> ) })} ) }