import { useEffect } from "react";
import VehiclesPageComponent from "./vehicles.page.component";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import InstanceRenderManager from "../../utils/instanceRenderMgr";

import { setBreadcrumbs, setSelectedHeader } from "../../redux/application/application.actions";
import { createStructuredSelector } from "reselect";
import { selectIsPartsEntry } from "../../redux/application/application.selectors.js";
import getPartsBasePath from "../../utils/getPartsBasePath.js";

const mapStateToProps = createStructuredSelector({
  isPartsEntry: selectIsPartsEntry
});

const mapDispatchToProps = (dispatch) => ({
  setBreadcrumbs: (breadcrumbs) => dispatch(setBreadcrumbs(breadcrumbs)),
  setSelectedHeader: (key) => dispatch(setSelectedHeader(key))
});

export function VehiclesPageContainer({ setBreadcrumbs, setSelectedHeader, isPartsEntry }) {
  const { t } = useTranslation();
  const basePath = getPartsBasePath(isPartsEntry);

  useEffect(() => {
    document.title = t("titles.vehicles", {
      app: InstanceRenderManager({
        imex: "$t(titles.imexonline)",
        rome: "$t(titles.romeonline)"
      })
    });
    setSelectedHeader("vehicles");

    if (isPartsEntry) {
      setBreadcrumbs([
        { link: "/parts", label: t("titles.bc.parts") },
        { link: `${basePath}/vehicles`, label: t("titles.bc.vehicles") }
      ]);
    } else {
      setBreadcrumbs([{ link: `${basePath}/vehicles`, label: t("titles.bc.vehicles") }]);
    }
  }, [t, setBreadcrumbs, setSelectedHeader, basePath, isPartsEntry]);

  return <VehiclesPageComponent />;
}

export default connect(mapStateToProps, mapDispatchToProps)(VehiclesPageContainer);
