import { useEffect } from "react";
import { useTranslation } from "react-i18next";
import RbacWrapper from "../../components/rbac-wrapper/rbac-wrapper.component";
import ShopVendorPageComponent from "./shop-vendor.page.component";

import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";
import { setBreadcrumbs, setSelectedHeader } from "../../redux/application/application.actions";
import InstanceRenderManager from "../../utils/instanceRenderMgr";

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

export function ShopVendorPageContainer({ bodyshop, setBreadcrumbs, setSelectedHeader }) {
  const { t } = useTranslation();
  useEffect(() => {
    document.title = t("titles.shop_vendors", {
      app: InstanceRenderManager({
        imex: "$t(titles.imexonline)",
        rome: "$t(titles.romeonline)"
      })
    });
    setSelectedHeader("shop-vendors");
    setBreadcrumbs([
      {
        link: "/manage/shop",
        label: t("titles.bc.shop", { shopname: bodyshop.shopname })
      },
      { link: "/manage/shop/vendors", label: t("titles.bc.shop-vendors") }
    ]);
  }, [t, bodyshop.shopname, setBreadcrumbs, setSelectedHeader]);

  return (
    <RbacWrapper action="shop:vendors">
      <ShopVendorPageComponent />
    </RbacWrapper>
  );
}

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