import { Box, Divider, Typography } from '@mui/material';
import { memo, useContext } from 'react';
import { DataContext } from '../context/Context';
import { inputProps } from '../interface/inputfieldProps';
import { getFieldName } from '../permissions/getFieldName';
import { useJsonForms } from '@jsonforms/react';
import ComponentWrapper from '../common/ComponentWrapper';
import { getComponentProps } from '../common/getComponentProps';
import { ButtonIcon } from '../common/ButtonIcon';
import React from 'react';

const Label = memo(function ({
 uischema,
 path,
 data,
 schema,
 rootSchema,
}: inputProps) {
 const uischemaData = uischema?.config?.main;
 const { serviceProvider, pageName, permissions, theme } =
  useContext(DataContext);
 const ctx = useJsonForms();
 const fieldName = getFieldName(path);
 const callServiceProvider = (event: any) => {
  serviceProvider(ctx, uischemaData, {
   event,
   path,
   ...uischemaData.additionalData,
  });
 };
 const dynamicStyle = React.useMemo(() => {
  return serviceProvider(
   ctx,
   {
    onClick: uischema.config?.main?.styleFunction || 'getStyle',
   },
   {
    event: { _reactName: 'onClick' },
    path,
   },
  );
 }, [data, path]);
 const componentStyle = { ...uischema.config?.style, ...dynamicStyle };
 return (
  <ComponentWrapper
   {...getComponentProps(
    `${pageName}:${fieldName}`,
    permissions,
    schema,
    rootSchema,
   )}
  >
   <Typography
    key={path}
    sx={{
     ...theme?.BoxStyle,
     ...componentStyle,
    }}
    variant={uischemaData?.variant}
    onClick={(e) => callServiceProvider(e)}
    paragraph={uischemaData?.paragraph}
   >
    {uischemaData?.iconName ? (
     ButtonIcon(uischemaData?.iconName, uischema.config)
    ) : uischemaData?.isInputLabel ? (
     <label
      htmlFor={uischemaData?.htmlFor}
      style={{ ...uischema?.config?.style }}
     >
      {data ? data : uischemaData?.heading}
     </label>
    ) : (
     (data ?? uischemaData?.heading)
    )}
   </Typography>
  </ComponentWrapper>
 );
});

export default Label;
