import { notification } from "antd";
import axios from "axios";
import React, { useEffect } from "react";
import { useTranslation } from "react-i18next";

import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { setPartnerVersion } from "../../redux/application/application.actions";

const mapStateToProps = createStructuredSelector({
  //currentUser: selectCurrentUser
});
const mapDispatchToProps = (dispatch) => ({
  //setUserLanguage: language => dispatch(setUserLanguage(language))
  setPartnerVersion: (version) => dispatch(setPartnerVersion(version)),
});
export default connect(
  mapStateToProps,
  mapDispatchToProps
)(PartnerPingComponent);

export function PartnerPingComponent({ setPartnerVersion }) {
  const { t } = useTranslation();

  useEffect(() => {
    // Create an scoped async function in the hook
    async function checkPartnerStatus() {
      try {
        if (process.env.NODE_ENV === "development") return;
        const PartnerResponse = await axios.post("http://localhost:1337/ping/");
        const { appver, qbpath } = PartnerResponse.data;
        setPartnerVersion(appver);
        console.log({ appver, qbpath });
        if (!qbpath) {
          notification["error"]({
            title: "",
            message: t("general.messages.noacctfilepath"),
          });
        }
      } catch (error) {
        notification["error"]({
          title: "",
          message: t("general.messages.partnernotrunning"),
        });
      }
    }
    // Execute the created function directly
    checkPartnerStatus();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  return <></>;
}
