import React from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';
import classnames from 'classnames';

export class HeaderIcon extends React.Component<any> {
    static propTypes = {
        imageClass: PropTypes.string,
        children(props, propName, componentName): any {
            let children = props[propName];
            let count = React.Children.count(children);

            if (count > 0 && (count !== 1 || children.type.name !== 'Icon')) {
                return new Error(
                    `Only a Icon component is allowed in the body of the ${componentName}.`
                );
            }
        }
    };

    static defaultProps = {
        imageClass: '',
        children: null
    };

    render() {
        let className = classnames('header-icon', {
            [this.props.imageClass]: !_.isEmpty(this.props.imageClass)
        });

        return (
            <div data-test-automation-id="headerIcon" className={className}>
                {this.props.children}
            </div>
        );
    }
}
