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

import {getHighlightRegExp, searchStringToRegExp} from '../../utils/regexp';

import {
    CommonListHighlight,
    CommonListHighlightDefaultProps,
    CommonListHighlightProps
} from './common/CommonListHighlight';

type PureListHighlightProps = {
    highlightedText?: string;
} & Partial<PureListHighlightDefaultProps> &
    CommonListHighlightProps;

type PureListHighlightDefaultProps = {
    highlightedText: string;
} & CommonListHighlightDefaultProps;

// CommonListHighlight
export class PureListHighlight extends React.Component<PureListHighlightProps> {
    static propTypes = {
        ...CommonListHighlight.propTypes,
        highlightedText: PropTypes.string
    };

    static defaultProps = {
        ...CommonListHighlight.defaultProps,
        highlightedText: ''
    };

    static HIGHLIGHT_TYPE_BACKGROUND = CommonListHighlight.HIGHLIGHT_TYPE_BACKGROUND;
    static HIGHLIGHT_TYPE_BOLD = CommonListHighlight.HIGHLIGHT_TYPE_BOLD;

    getTextHighlightPattern() {
        const {textHighlightPattern, highlightedText} = this.props;

        return textHighlightPattern || getHighlightRegExp(highlightedText);
    }

    getTextFilterPattern() {
        const {textFilterPattern, highlightedText} = this.props;

        return textFilterPattern || searchStringToRegExp(highlightedText);
    }

    render() {
        return (
            <CommonListHighlight
                {...this.props}
                textHighlightPattern={this.getTextHighlightPattern() as any}
                textFilterPattern={this.getTextFilterPattern() as any}
            />
        );
    }
}
