import { memo } from "react";
import PropTypes from "prop-types";
import { ProductFruits } from "react-product-fruits";
import dayjs from "dayjs";

const ProductFruitsWrapper = memo(({ currentUser, bodyshop, workspaceCode, isPartsEntry }) => {
  const featureProps = bodyshop?.features
    ? Object.entries(bodyshop.features).reduce((acc, [key, value]) => {
        acc[key] = value === true || (typeof value === "string" && dayjs(value).isAfter(dayjs()));
        return acc;
      }, {})
    : {};

  return (
    !isPartsEntry &&
    workspaceCode &&
    currentUser?.authorized === true &&
    currentUser?.email && (
      <ProductFruits
        lifeCycle="unmount"
        workspaceCode={workspaceCode}
        debug
        language="en"
        user={{
          email: currentUser.email,
          username: currentUser.email,
          props: featureProps
        }}
      />
    )
  );
});

ProductFruitsWrapper.displayName = "ProductFruitsWrapper";

export default ProductFruitsWrapper;

ProductFruitsWrapper.propTypes = {
  currentUser: PropTypes.shape({
    authorized: PropTypes.bool,
    email: PropTypes.string
  }),
  workspaceCode: PropTypes.string,
  bodyshop: PropTypes.object
};
