import React from 'react';
import {DiskStateProgressBar} from '../../../components/DiskStateProgressBar/DiskStateProgressBar';
import {HoverPopup} from '../../../components/HoverPopup/HoverPopup';
import {InternalLink} from '../../../components/InternalLink';
import {PDiskLogSize} from '../../../components/PDiskInfo/PDiskLogSize';
import {VDiskPopup} from '../../../components/VDiskPopup/VDiskPopup';
import type {YDBDefinitionListItem} from '../../../components/YDBDefinitionList/YDBDefinitionList';
import {YDBDefinitionList} from '../../../components/YDBDefinitionList/YDBDefinitionList';
import {useVDiskPagePath} from '../../../routes';
import type {
EmptySlotData,
LogSlotData,
PDiskData,
SlotItem,
SlotItemType,
} from '../../../store/reducers/pdisk/types';
import type {NodeMetadata} from '../../../types/store/nodesList';
import {valueIsDefined} from '../../../utils';
import {formatBytes} from '../../../utils/bytesParsers';
import {cn} from '../../../utils/cn';
import {formatStorageValuesToGb} from '../../../utils/dataFormatters/dataFormatters';
import {DISK_COLOR_STATE_TO_NUMERIC_SEVERITY} from '../../../utils/disks/constants';
import {getDiskBarTone} from '../../../utils/disks/getDiskBarTone';
import {pDiskPageKeyset} from '../i18n';
import {isEmptySlot, isLogSlot, isVDiskSlot} from './utils';
import './PDiskSpaceDistribution.scss';
const b = cn('ydb-pdisk-space-distribution');
const BASE_SLOT_HEIGHT = 40;
// Upper bound for how much a single slot can be scaled relative to the smallest one.
// Protects layout from degenerate cases where a VDisk reports an implausibly small Total
// (e.g. a mostly empty disk where Total falls back to AllocatedSize), which would otherwise
// blow up other slots' heights to thousands of pixels.
const MAX_SLOT_HEIGHT_MULTIPLIER = 10;
interface PDiskSpaceDistributionProps {
data: PDiskData;
}
export function PDiskSpaceDistribution({data}: PDiskSpaceDistributionProps) {
const getVDiskPagePath = useVDiskPagePath();
const {SlotItems} = data;
const {PDiskId, NodeId} = data;
// Find the minimum Total among non-log slots to use as the base unit for height scaling.
// Slots with missing/zero Total are skipped so that they don't collapse the base unit to 1
// and inflate every other slot's computed height to an unrenderable value.
const minNonLogTotal = React.useMemo(() => {
if (!SlotItems?.length) {
return 1;
}
let minTotal = Infinity;
for (const item of SlotItems) {
if (item.SlotType === 'log') {
continue;
}
const value = Number(item.Total);
if (!value || value <= 0) {
continue;
}
if (value < minTotal) {
minTotal = value;
}
}
return minTotal === Infinity ? 1 : minTotal;
}, [SlotItems]);
const renderSlots = () => {
return SlotItems?.map((item, index) => {
return (