import React from 'react';
import CouponCode from './code.jsx';
import LoadingCode from './loading-code.jsx';

const propTypes = {
    appliedCouponCodes: React.PropTypes.array.isRequired,
    loadingCount: React.PropTypes.number
};

export default class CouponTable extends React.Component {

    static get defaultProps(){
        return {
            loadingCount: 0
        };
    }

    renderCodes(){
        var codes = this.props.appliedCouponCodes;
        return codes.map((code, index) => (
            <CouponCode
                {...code}
                key={`code-${index}`} />
        ));
    }

    renderLoadingCodes(){
        var count = this.props.loadingCount;
        var iterator = Array.from(new Array(count));
        return iterator.map((undefined, index) => (
            <LoadingCode
                key={`loading-code-${index}`} />
        ));
    }

    render() {
        var rows = [
            ...this.renderCodes(),
            ...this.renderLoadingCodes()
        ];
        return rows.length ? (
            <table className="coupon__codes">
                <tbody>{rows}</tbody>
            </table>
        ) : null;
    }
}

CouponTable.propTypes = propTypes;