import React from "react";
import { getDefaultRegistry } from "@rjsf/core";
import { FormContextType, getTemplate, RJSFSchema, WidgetProps } from "@rjsf/utils";
import Typography from "@mui/material/Typography";

import ForeignKeyWidget from "./foreign-key";
import RichtextWidget from "./richtext";

export type CrudTextRJSFSchema = RJSFSchema & { props? : any };

export default function CrudTextWidget<T = any, S extends CrudTextRJSFSchema = CrudTextRJSFSchema, F extends FormContextType = any>(
  props: WidgetProps<T, S, F>
) {
  const { schema } = props;
  if (schema.hasOwnProperty("foreignKey")) {
    return <ForeignKeyWidget {...props} />;
  } else if (schema.hasOwnProperty("const")) {
    return <Typography >{schema.const as string}</Typography>;
  } else if (schema.props?.hasOwnProperty("richtext")) {
    return <RichtextWidget {...props} />;
  } else {
    const { widgets: { TextWidget } } = getDefaultRegistry<T,S,F>();
    return <TextWidget {...props} />;
  }
}
