import Result, { type ResultProps } from './result';
import * as common from './common';

type ComponentNames = keyof typeof common;

const componentsKeyByStatus = Object.keys(common).reduce<Record<string, React.ComponentType<ResultProps>>>(
  (acc, cur) => ({ ...acc, [common[cur as ComponentNames].status || cur]: common[cur as ComponentNames] as any }),
  {}
);

export interface ResultWrapperProps extends ResultProps {
  status?: string | number;
}

function ResultWrapper({ status = '', ...rest }: ResultWrapperProps) {
  if (status) {
    if (componentsKeyByStatus[status]) {
      const Component = componentsKeyByStatus[status];
      return <Component {...rest} />;
    }
    throw new Error(
      `Please provide a valid status for Result.status. Valid values are: ${Object.keys(componentsKeyByStatus).join(
        ', '
      )}`
    );
  }
  return <Result {...rest} />;
}

export default ResultWrapper;
