import classNames from "classnames";
import React, { useCallback, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Panel } from "@enact/sandstone/Panels";
import { Cancelable } from "@enact/ui/Cancelable";
import css from "./TPanel.module.less";
import SmoodShowingAnimation from "../SmoodShowingAnimation/SmoodShowingAnimation";
import { popPanel } from "../../features/panels/panelsSlice";

const CancelablePanel = Cancelable(
  { modal: true, onCancel: "handleCancel" },
  Panel
);

const TPanel = ({
  className,
  children,
  handleCancel,
  isTabActivated = true,
  ...rest
}) => {
  const dispatch = useDispatch();
  delete rest.panelInfo;

  const onCancel = useCallback(
    (e) => {
      if (isTabActivated) {
        return;
      }
      if (handleCancel) {
        handleCancel(e);
      } else {
        dispatch(popPanel());
        e.stopPropagation();
      }
    },
    [dispatch, handleCancel, isTabActivated]
  );

  console.log(isTabActivated);

  return (
    <CancelablePanel
      {...rest}
      handleCancel={onCancel}
      className={classNames(
        css.tpanelmain,
        isTabActivated && css.isTabActivated,
        className
      )}
    >
      {children}
    </CancelablePanel>
  );
};

export default SmoodShowingAnimation(TPanel);
