import React from 'react';
import PropTypes from 'prop-types';

import {highlight} from '../../list/utils';
import {Highlighter, HIGHLIGHT_TYPE} from '../../Highlighter';

export class CommonListHighlight extends React.Component {
    static HIGHLIGHT_TYPE_BACKGROUND = HIGHLIGHT_TYPE.BACKGROUND;
    static HIGHLIGHT_TYPE_BOLD = HIGHLIGHT_TYPE.BOLD;

    static propTypes = {
        children: PropTypes.node,
        title: PropTypes.string,
        textHighlightPattern: PropTypes.object,
        textFilterPattern: PropTypes.object,
        listHighlightType: PropTypes.oneOf([HIGHLIGHT_TYPE.BACKGROUND, HIGHLIGHT_TYPE.BOLD]),
        containsRichText: PropTypes.bool
    };

    static defaultProps = {
        children: null,
        title: null,
        textHighlightPattern: null,
        textFilterPattern: null,
        listHighlightType: HIGHLIGHT_TYPE.BOLD,
        containsRichText: false
    };

    render() {
        const {
            listHighlightType,
            containsRichText,
            title,
            textHighlightPattern,
            textFilterPattern
        } = this.props;

        if (containsRichText || listHighlightType === HIGHLIGHT_TYPE.BACKGROUND) {
            return (
                <Highlighter
                    title={title}
                    listHighlightType={listHighlightType}
                    highlightRegexp={textHighlightPattern}
                    filterRegexp={textFilterPattern}>
                    {this.props.children}
                </Highlighter>
            );
        }

        return highlight(textFilterPattern, this.props.children, title);
    }
}
