import React, { PureComponent } from 'react';
import { keysOf } from '../common';
import classNames from 'classnames';
import { isColorDark, hexToRgb } from '../../services/color';
import { VISUALIZATION_COLORS, toInitials } from '../../services';
const sizeToClassNameMap = {
    none: null,
    s: 'softAvatar--s',
    m: 'softAvatar--m',
    l: 'softAvatar--l',
    xl: 'softAvatar--xl',
};
export const SIZES = keysOf(sizeToClassNameMap);
const typeToClassNameMap = {
    space: 'softAvatar--space',
    user: 'softAvatar--user',
};
export const TYPES = keysOf(typeToClassNameMap);
export class SoftAvatar extends PureComponent {
    render() {
        let { className, color, imageUrl, initials, initialsLength, name, size, type, ...rest } = this.props;
        const classes = classNames('softAvatar', sizeToClassNameMap[size], typeToClassNameMap[type], className);
        checkValidColor(color);
        checkValidInitials(initials);
        let optionalInitial;
        if (name && !imageUrl) {
            // Create the initials
            const calculatedInitials = toInitials(name, initialsLength, initials);
            optionalInitial = React.createElement("span", { "aria-hidden": "true" }, calculatedInitials);
        }
        const assignedColor = color ||
            VISUALIZATION_COLORS[Math.floor(name.length % VISUALIZATION_COLORS.length)];
        // @ts-ignore
        const textColor = isColorDark(...hexToRgb(assignedColor))
            ? '#FFFFFF'
            : '#000000';
        const avatarStyle = {
            backgroundImage: imageUrl ? `url(${imageUrl})` : 'none',
            backgroundColor: assignedColor,
            color: textColor,
        };
        return (React.createElement("div", Object.assign({ className: classes, style: avatarStyle, "aria-label": name, title: name }, rest), optionalInitial));
    }
}
SoftAvatar.defaultProps = { size: 'm', type: 'user' };
// TODO: Migrate to a service
function checkValidColor(color) {
    const validHex = color && /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(color);
    if (color && !validHex) {
        throw new Error(`SoftAvatar needs to pass a valid color. This can either be a three ` +
            `or six character hex value`);
    }
}
function checkValidInitials(initials) {
    // Must be a string of 1 or 2 characters
    if (initials && initials.length > 2) {
        // tslint:disable-next-line:no-console
        console.warn(`SoftAvatar only accepts a max of 2 characters for the initials as a string. It is displaying only the first 2 characters.`);
    }
}
//# sourceMappingURL=SoftAvatar.js.map