import { useMemo } from "react";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";

const mapStateToProps = createStructuredSelector({
  bodyshop: selectBodyshop
});
const mapDispatchToProps = () => ({
  //setUserLanguage: language => dispatch(setUserLanguage(language))
});
export default connect(mapStateToProps, mapDispatchToProps)(ProductionListColumnPartsReceived);

export function ProductionListColumnPartsReceived({ bodyshop, record }) {
  const amount = useMemo(() => {
    const amount = record.joblines_status.reduce(
      (acc, val) => {
        acc.total += val.count;
        acc.received =
          val.status === bodyshop.md_order_statuses.default_received ? acc.received + val.count : acc.received;
        return acc;
      },
      { total: 0, received: 0 }
    );

    return {
      ...amount,
      percent: amount.total !== 0 ? ((amount.received / amount.total) * 100).toFixed(0) + "%" : "N/A"
    };
  }, [record, bodyshop.md_order_statuses]);

  return `${amount.percent} (${amount.received}/${amount.total})`;
}
