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

const ProductFruitsWrapper = React.memo(({ currentUser, bodyshop, workspaceCode }) => {
  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 (
    workspaceCode &&
    currentUser?.authorized === true &&
    currentUser?.email && (
      <ProductFruits
        lifeCycle="unmount"
        workspaceCode={workspaceCode}
        debug
        language="en"
        user={{
          email: currentUser.email,
          username: currentUser.email,
          props: featureProps
        }}
      />
    )
  );
});

export default ProductFruitsWrapper;

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