import React from 'react';
import PropTypes from 'prop-types';
import Pricing from '../../../../atoms/pricing/Pricing';
import { stripSlash } from '../../../../tools/stringTools';
import { getFormattedFullPriceElements } from '../../../../tools/components/pricing';

const VariantPrice = ({ plans }) => {
    const [{ costs: { unitPrice = [{}], flatPrice } = {} }] = (Array.isArray(plans) && plans.length > 0) ? plans : [{}];
    const formattedFullPriceElements = getFormattedFullPriceElements(unitPrice, flatPrice);

    if (!formattedFullPriceElements || !formattedFullPriceElements.length) {
        return '';
    }

    const [price, unit, frequency, feeType = ''] = formattedFullPriceElements;
    const isConsistentDisplay = formattedFullPriceElements.length === 4;

    return (
        <Pricing
            isMultipleLine
            price={price}
            unit={unit && stripSlash(unit)}
            frequency={frequency && stripSlash(frequency)}
            feeType={feeType && stripSlash(feeType)}
            isConsistentDisplay={isConsistentDisplay}
        />
    );
};

VariantPrice.propTypes = {
    plans: PropTypes.arrayOf(PropTypes.shape({
        costs: PropTypes.shape({
            unitPrice: PropTypes.arrayOf(PropTypes.shape({
                usageRange: PropTypes.arrayOf(PropTypes.shape({
                    formattedFullPriceElements: PropTypes.array
                }))
            }))
        })
    }))
};

VariantPrice.defaultProps = {
    plans: [{
        costs: {
            unitPrice: [{
                usageRange: [{
                    formattedFullPriceElements: []
                }]
            }]
        }
    }]
};

export default VariantPrice;
