import React from "react";
import * as MuiIcons from "./list";
import { Icon as MuiIcon, IconProps } from "@mui/material";

interface Props extends IconProps {
  code?: string | null;
  fallback?: React.ReactElement;
}

const DynamicIcon: React.FC<Props> = ({ code, fallback, ...res }) => {
  if (code && code in MuiIcons) {
    const Icon = MuiIcons[code as keyof typeof MuiIcons] as typeof MuiIcon;
    return <Icon {...res} />;
  } else {
    return <>{fallback ?? ""}</>;
  }
};

export default DynamicIcon;
