import { getStatusColor, getPriorityColor } from './orderFormatters';

export const calculateTableHeight = (
  rowCount: number,
  pageSize: number,
  options?: {
    minHeight?: number;
    rowHeight?: number;
    headerHeight?: number;
    footerHeight?: number;
    aggregationRowHeight?: number;
  }
): number => {
  const {
    minHeight = 400,
    rowHeight = 36,
    headerHeight = 40,
    footerHeight = 48,
    aggregationRowHeight = 40,
  } = options || {};

  const visibleRows = Math.min(rowCount, pageSize);
  const aggregationHeight = 0;
  const calculatedHeight =
    headerHeight + visibleRows * rowHeight + aggregationHeight + footerHeight;
  const minVisibleRows = Math.max(visibleRows, 3);
  const minCalculatedHeight =
    headerHeight +
    minVisibleRows * rowHeight +
    aggregationHeight +
    footerHeight;

  return Math.max(calculatedHeight, minCalculatedHeight, minHeight);
};

type ChipColor = 'success' | 'error' | 'warning' | 'default';

const COLOR_MAP: Record<string, ChipColor> = {
  success: 'success',
  error: 'error',
  warning: 'warning',
};

export const getStatusChipProps = (status: string) => {
  const color = getStatusColor(status);
  return {
    color: COLOR_MAP[color] || ('default' as const),
    label: status,
  };
};

export const getPriorityChipProps = (priority: unknown) => {
  if (!priority) {
    return null;
  }

  let priorityStr = '';

  if (typeof priority === 'object' && priority !== null) {
    const obj = priority as Record<string, unknown>;
    const nameValue =
      typeof obj.name === 'string' || typeof obj.name === 'number'
        ? obj.name
        : '';
    const labelValue =
      typeof obj.label === 'string' || typeof obj.label === 'number'
        ? obj.label
        : '';
    priorityStr = String(nameValue || labelValue || '').trim();
  } else if (typeof priority === 'string' || typeof priority === 'number') {
    priorityStr = String(priority).trim();
  }

  if (!priorityStr || priorityStr.toLowerCase() === 'não definido') {
    return null;
  }

  const color = getPriorityColor(priority);
  return {
    color: COLOR_MAP[color] || ('default' as const),
    label: priorityStr,
  };
};
