import type { RatingProps, Theme } from '@mui/material';
import type { Components, Palette, PaletteColor } from '@mui/material/styles';

import { largeSize, mediumSize, smallSize, xSmallSize } from '@/mui/components/constants';

const fontSizes = {
  xSmall: xSmallSize,
  small: smallSize,
  medium: mediumSize,
  large: largeSize,
};

export default function MuiRating(theme: Theme): Components['MuiRating'] {
  const variants = Object.keys(theme.palette).filter(
    (key) => !!(theme.palette[key as keyof Palette] as PaletteColor).contrastText,
  );

  return {
    defaultProps: {
      color: 'warning',
    },
    styleOverrides: {
      root: {
        fontSize: 'inherit',
      },
    },
    variants: [
      ...variants.map((key) => ({
        props: { color: key },
        style: {
          color:
            key === 'neutral' ? theme.palette.shades[900] : (theme.palette[key as keyof Palette] as PaletteColor)?.main,
        },
      })),
      ...Object.entries(fontSizes).map(([key, fontSize]) => ({
        props: { size: key as RatingProps['size'] },
        style: {
          fontSize,
        },
      })),
    ],
  };
}
