/* eslint-disable @typescript-eslint/no-explicit-any */
import { SYSTEM_DEFAULT } from "../../../../constant";
import ResetView from "./ResetView";

export type ResetProps = {
  visible: boolean;
  disabled: boolean;
  uiElementGroupData: Record<string, any>;
  config: { uiElementGroupId: string };
  onModelUpdate: (
    callBack: ((args: any) => void) | null,
    fieldName: string,
    value: any
  ) => void;
  getUniqueViewId?: (callBack: (response: string) => void) => void;
};

const Reset = (props: ResetProps) => {
  const onReset = () => {
    if (props.uiElementGroupData.userView.selectedView?.id === SYSTEM_DEFAULT) {
      let uniqueViewId = props.config.uiElementGroupId;
      if (props.getUniqueViewId) {
        props.getUniqueViewId((id: string) => {
          uniqueViewId = id + "-" + props.config.uiElementGroupId;
        });
      }
      const defaultScreenViewId = `${uniqueViewId}_${SYSTEM_DEFAULT}`;
      sessionStorage.removeItem(defaultScreenViewId);
    }

    const updatedUiElementGroupData = {
      ...props.uiElementGroupData,
      reset: (props.uiElementGroupData.reset ?? 0) + 1,
    };

    props.onModelUpdate(
      null,
      props.config.uiElementGroupId,
      updatedUiElementGroupData
    );
  };
  return props.visible ? (
    <ResetView disabled={props.disabled} onReset={onReset} />
  ) : (
    <></>
  );
};
export default Reset;
