import '../nui-icon/nui-icon.js'; import { html, nothing, type TemplateResult } from 'lit'; import type { NuiType } from '../../types/nui-type.js'; import type { MeterItem } from './types.js'; export interface NuiMeterGroupViewState { value: MeterItem[]; min: number; max: number; orientation: 'horizontal' | 'vertical'; labelPosition: 'start' | 'end' | 'none'; labelOrientation: 'horizontal' | 'vertical'; unstyled: boolean; nuiType: NuiType; meterGroupClass: string; } function getMeterGroupClass(state: NuiMeterGroupViewState): string { return ['nui-meter-group', state.meterGroupClass].filter(Boolean).join(' '); } export function renderMeterGroup( state: NuiMeterGroupViewState, ): TemplateResult { const min = state.min; const max = state.max; const range = max - min > 0 ? max - min : 100; const items = state.value || []; // Calculate percentages const calculatedItems = items.map((item) => { const percent = Math.max(0, (item.value / range) * 100); return { ...item, percent, }; }); const renderMeters = () => html`
${calculatedItems.map((item) => { const style = state.orientation === 'vertical' ? `height: ${item.percent}%; background-color: ${item.color || 'var(--nui-primary-color)'};` : `width: ${item.percent}%; background-color: ${item.color || 'var(--nui-primary-color)'};`; return html`
`; })}
`; const renderLabels = () => { if (state.labelPosition === 'none' || calculatedItems.length === 0) { return nothing; } return html`
    ${calculatedItems.map( (item) => html`
  1. ${ item.icon ? html` ` : html` ` } ${item.label} (${item.value}%)
  2. `, )}
`; }; return html`
${state.labelPosition === 'start' ? renderLabels() : nothing} ${renderMeters()} ${state.labelPosition === 'end' ? renderLabels() : nothing}
`; }