import React from 'react';
import normalize from 'value-or-function';
import CartTotal from '../../components/cart-total/cart-total.jsx';

const propTypes = {
    cartApi: React.PropTypes.object.isRequired,
    languageApi: React.PropTypes.object.isRequired,
    show: React.PropTypes.oneOfType([
        React.PropTypes.bool,
        React.PropTypes.func
    ])
};

export default class CartContainer extends React.Component {

    static get defaultProps(){
        return {
            cartApi: {},
            languageApi: { state: { cart: {} } },
            show: context => context.props.cartApi.state.items.length > 0
        };
    }

    render() {
        const { totalPrice, totalPriceWithoutTax } = this.props.cartApi.state;
        const language = this.props.languageApi.state.cartTotal;
        return normalize.boolean(this.props.show, this) && <CartTotal
            language={language}
            totalPrice={totalPrice.formatted}
            totalPriceWithoutTax={totalPriceWithoutTax.formatted} />;
    }
}

CartContainer.propTypes = propTypes;