import axios from "axios";
import React, { useEffect } from "react";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { setPartnerVersion } from "../../redux/application/application.actions";
import { selectBodyshop } from "../../redux/user/user.selectors";
import { store } from "../../redux/store";

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

export function PartnerPingComponent({ bodyshop }) {
  useEffect(() => {
    // Create an scoped async function in the hook

    // Execute the created function directly
    checkPartnerStatus(bodyshop);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [bodyshop?.id]);

  return <></>;
}

export async function checkPartnerStatus(bodyshop) {
  if (!bodyshop) return;
  try {
    //if (process.env.NODE_ENV === "development") return;
    const PartnerResponse = await axios.post("http://localhost:1337/ping/");
    // const {
    //   appver, //qbpath
    // } = PartnerResponse.data;
    console.log(PartnerResponse.data);
    store.dispatch(setPartnerVersion(PartnerResponse.data));
    // if (
    //   checkAcctPath &&
    //   !qbpath &&
    //   !(
    //     bodyshop &&
    //     (bodyshop.cdk_dealerid ||
    //       bodyshop.pbs_serialnumber ||
    //       bodyshop.accountingconfig.qbo)
    //   )
    // ) {
    //   notification["error"]({
    //     title: "",
    //     message: i18n.t("general.messages.noacctfilepath"),
    //   });
    // }
  } catch (error) {
    console.log("Partner is not running.", error);
    // notification["error"]({
    //   title: "",
    //   message: i18n.t("general.messages.partnernotrunning"),
    // });
  }
}
