/* eslint-disable @typescript-eslint/no-explicit-any */
import { FC, useCallback, useMemo } from "react";
import Button from "@mui/material/Button";
// import { CircularProgress } from "@mui/material";
import { ButtonWidgetProps, WrappedButtonProps } from "./interface";
import { buttonLayoutStyle } from "./style";
// import { green } from "@mui/material/colors";
import { ButtonIcon } from "../IconWidget/utils";
import { getStyleMap } from "@/utils/getStyleMap";
// import { useSelector } from "react-redux";
// import { getExecutionResultToGlobalCodeMirror } from "@/redux/currentApp/executionTree/executionSelector";
// import { getStringSnippets, isDynamicStringSnippet } from "@/redux/utils";
// import { evaluateDynamicString } from "@/utils/evaluateDynamicString";
// import { RootState } from "@/store";

export const WrappedButton: FC<WrappedButtonProps> = (props) => {
  const {
    text,
    context,
    variant,
    isClassTrue = true,
    trueClasses = [],
    falseClasses = [],
    tooltipText,
    disabled,
    loading,
    handleOnClick,
    startIconName,
    endIconName,
  } = props;
  const style = useMemo(() => {
    return getStyleMap(trueClasses, falseClasses, isClassTrue, context);
  }, [trueClasses, falseClasses, isClassTrue, context]);
  return (
    <Button
      variant={variant}
      fullWidth
      endIcon={endIconName ? ButtonIcon(endIconName, {}, {}) : false}
      startIcon={startIconName ? ButtonIcon(startIconName, {}, {}) : false}
      disabled={disabled || loading}
      title={tooltipText}
      key={props.displayName}
       css={style}
      onClick={handleOnClick}
    >
      {text}
    </Button>
  );
};

WrappedButton.displayName = "WrappedButton";
export const ButtonWidget: FC<ButtonWidgetProps> = (props: any) => {
  const handleOnClick = useCallback(() => {
    props.triggerEventHandler("click");
  }, [props]);
  return (
    <div css={buttonLayoutStyle} key={`${props.displayName}-wrapper`}>
      <WrappedButton {...props} handleOnClick={handleOnClick} />
    </div>
  );
};

ButtonWidget.displayName = "ButtonWidget";
