import PropTypes from 'prop-types';

import {CommonList, CommonListProps} from './common/CommonList';

export type PureListProps<R> = {
    rows: R[];
} & CommonListProps;

export abstract class PureList<P, R> extends CommonList<PureListProps<R> & P, any> {
    static propTypes = {
        ...CommonList.propTypes,
        rows: PropTypes.array.isRequired
    };

    constructor(props) {
        super(props);

        this.renderItem = this.renderItem.bind(this);
    }

    hasItems() {
        return this.props.rows.length > 0;
    }

    isEmptyList() {
        return this.props.rows.length === 0;
    }

    isEmptyListMessageVisible() {
        return !this.hasItems();
    }

    abstract renderItem(item: R, index: number, array: R[]): any;

    renderItems() {
        return this.props.rows.map(this.renderItem);
    }
}
