import '../i18n/domains/settings.gen' import { I18nKey, t } from '../i18n' import { vmRefKey } from './vmRef' import type { Config, HostReport, Locale, Task, VMListItem } from '../types' export interface HypervisorSettingsView { supported: boolean gateEnabled: boolean expanded: boolean badgeClass: 'badge-ok' | 'badge-warn' | 'badge-accent' badgeLabel: string subLabel: string actionLabel: string actionVariant: 'primary' | 'secondary' targetEnable: boolean isUndo: boolean disableBlockedByVMs: boolean showHVCINotice: boolean } export interface HostWHPXSummary { short: string hint: string tone: 'ok' | 'warn' canEnable: boolean } const BLOCKING_VM_STATES = new Set(['running', 'starting', 'stopping', 'suspending', 'resuming']) export function hasHypervisorBlockingWork( vms: VMListItem[] | null | undefined, transitions: Record | undefined, tasks?: Task[], ): boolean { for (const vm of vms ?? []) { const st = transitions?.[vmRefKey(vm)] ?? transitions?.[vm.name] ?? vm.state if (BLOCKING_VM_STATES.has(st)) return true } if (transitions) { for (const tr of Object.values(transitions)) { if (tr && BLOCKING_VM_STATES.has(tr)) return true } } for (const task of tasks ?? []) { if (task.status === 'running' && task.kind === 'vm-create') return true } return false } export function buildHypervisorSettingsView( locale: Locale, host: Pick, config: Pick, hasBlockingWork: boolean, ): HypervisorSettingsView { const tr = (k: I18nKey) => t(locale, k) const supported = host.whpx !== 'unsupported' const gateEnabled = supported && Boolean(config.enable_hypervisor_switch) const pending = host.pending_hypervisor_boot === 'auto' || host.pending_hypervisor_boot === 'off' ? host.pending_hypervisor_boot : '' const isUndo = pending !== '' const effectiveOn = pending === 'auto' ? true : pending === 'off' ? false : host.whpx === 'ready' const targetEnable = !effectiveOn let badgeClass: HypervisorSettingsView['badgeClass'] = 'badge-warn' let badgeKey: I18nKey = 'whpxNoHvShort' let subKey: I18nKey = 'hypervisorSubNoHv' if (pending === 'off') { badgeClass = 'badge-warn' badgeKey = 'hypervisorBadgePendingOff' subKey = 'hypervisorSubPendingOff' } else if (pending === 'auto') { badgeClass = 'badge-accent' badgeKey = 'hypervisorBadgePendingOn' subKey = 'hypervisorSubPendingOn' } else if (host.whpx === 'ready') { badgeClass = 'badge-ok' badgeKey = 'hypervisorBadgeActive' subKey = 'hypervisorSubActive' } else if (host.whpx === 'hypervisor_off') { badgeClass = 'badge-warn' badgeKey = 'hypervisorBadgeOff' subKey = 'hypervisorSubOff' } else if (host.whpx === 'missing') { badgeClass = 'badge-warn' badgeKey = 'whpxMissingShort' subKey = 'hypervisorSubMissing' } const actionKey: I18nKey = isUndo ? 'hypervisorActionUndo' : effectiveOn ? 'hypervisorActionDisable' : 'hypervisorActionEnable' const actionVariant: 'primary' | 'secondary' = !isUndo && !effectiveOn ? 'primary' : 'secondary' const disableBlockedByVMs = !targetEnable && hasBlockingWork const showHVCINotice = Boolean(host.hvci_enabled || host.vbs_running) && (pending === 'off' || host.whpx === 'ready') return { supported, gateEnabled, expanded: gateEnabled, badgeClass, badgeLabel: tr(badgeKey), subLabel: tr(subKey), actionLabel: tr(actionKey), actionVariant, targetEnable, isUndo, disableBlockedByVMs, showHVCINotice, } } export function describeHostWHPX( locale: Locale, host: Pick, ): HostWHPXSummary { const tr = (k: I18nKey) => t(locale, k) if (host.pending_hypervisor_boot === 'auto') { return { short: tr('whpxPendingOnShort'), hint: tr('whpxPendingHint'), tone: 'warn', canEnable: false, } } if (host.pending_hypervisor_boot === 'off') { return { short: tr('whpxPendingOffShort'), hint: tr(host.whpx === 'ready' ? 'whpxPendingOffHint' : 'whpxPendingHint'), tone: 'warn', canEnable: false, } } switch (host.whpx) { case 'ready': return { short: tr('whpxReady'), hint: '', tone: 'ok', canEnable: false } case 'hypervisor_off': return { short: tr('whpxHvOffShort'), hint: tr('whpxHvOff'), tone: 'warn', canEnable: true } case 'missing': return { short: tr('whpxMissingShort'), hint: tr('whpxMissing'), tone: 'warn', canEnable: true } case 'no_hypervisor': return { short: tr('whpxNoHvShort'), hint: tr('whpxNoHv'), tone: 'warn', canEnable: false } default: return { short: tr('whpxUnsupportedShort'), hint: tr('whpxUnsupported'), tone: 'warn', canEnable: false } } }