import * as tslib_1 from "tslib";
import * as React from 'react';
import { autobind, css, divProperties, getInitials, getNativeProps, getRTL } from '../../Utilities';
import { Image, ImageFit, ImageLoadState } from '../../Image';
import { PersonaPresence } from './PersonaPresence';
import { PersonaPresence as PersonaPresenceEnum, PersonaInitialsColor, PersonaSize } from './Persona.types';
import { PERSONA_INITIALS_COLOR, PERSONA_SIZE } from './PersonaConsts';
import { Icon } from '../../Icon';
import * as stylesImport from './Persona.scss';
var styles = stylesImport;
var SIZE_TO_PIXELS = (_a = {},
    _a[PersonaSize.tiny] = 20,
    _a[PersonaSize.extraExtraSmall] = 24,
    _a[PersonaSize.extraSmall] = 28,
    _a[PersonaSize.small] = 40,
    _a[PersonaSize.regular] = 48,
    _a[PersonaSize.large] = 72,
    _a[PersonaSize.extraLarge] = 100,
    _a[PersonaSize.size24] = 24,
    _a[PersonaSize.size28] = 28,
    _a[PersonaSize.size10] = 20,
    _a[PersonaSize.size32] = 32,
    _a[PersonaSize.size40] = 40,
    _a[PersonaSize.size48] = 48,
    _a[PersonaSize.size72] = 72,
    _a[PersonaSize.size100] = 100,
    _a);
/**
 * These colors are considered reserved colors and can only be set with overrides:
 * - Red is a color that often has a special meaning.
 * - Transparent is not intended to be used with typical initials due to accessibility issues,
 *   its primary use is for Facepile overflow buttons.
 */
var COLOR_SWATCHES_LOOKUP = [
    PersonaInitialsColor.lightGreen,
    PersonaInitialsColor.lightBlue,
    PersonaInitialsColor.lightPink,
    PersonaInitialsColor.green,
    PersonaInitialsColor.darkGreen,
    PersonaInitialsColor.pink,
    PersonaInitialsColor.magenta,
    PersonaInitialsColor.purple,
    PersonaInitialsColor.black,
    PersonaInitialsColor.teal,
    PersonaInitialsColor.blue,
    PersonaInitialsColor.darkBlue,
    PersonaInitialsColor.orange,
    PersonaInitialsColor.darkRed
];
var COLOR_SWATCHES_NUM_ENTRIES = COLOR_SWATCHES_LOOKUP.length;
var PersonaCoin = /** @class */ (function (_super) {
    tslib_1.__extends(PersonaCoin, _super);
    function PersonaCoin(props) {
        var _this = _super.call(this, props) || this;
        _this.state = {
            isImageLoaded: false,
            isImageError: false
        };
        return _this;
    }
    PersonaCoin.prototype.render = function () {
        var _a = this.props, coinProps = _a.coinProps, coinSize = _a.coinSize, imageUrl = _a.imageUrl, imageAlt = _a.imageAlt, initialsColor = _a.initialsColor, primaryText = _a.primaryText, imageShouldFadeIn = _a.imageShouldFadeIn, _b = _a.onRenderInitials, onRenderInitials = _b === void 0 ? this._onRenderInitials : _b, imageShouldStartVisible = _a.imageShouldStartVisible;
        var size = this.props.size;
        var divProps = getNativeProps(this.props, divProperties);
        var coinSizeStyle = coinSize ? { width: coinSize, height: coinSize } : undefined;
        initialsColor = initialsColor !== undefined && initialsColor !== null ? initialsColor : this._getColorFromName(primaryText);
        return (React.createElement("div", tslib_1.__assign({}, divProps, { className: css('ms-Persona-coin', PERSONA_SIZE[size]) }),
            (size !== PersonaSize.size10 && size !== PersonaSize.tiny) ? (React.createElement("div", tslib_1.__assign({}, coinProps, { className: css('ms-Persona-imageArea', styles.imageArea), style: coinSizeStyle }),
                !this.state.isImageLoaded &&
                    (!imageUrl || this.state.isImageError) &&
                    (React.createElement("div", { className: css('ms-Persona-initials', styles.initials, PERSONA_INITIALS_COLOR[initialsColor]), style: coinSizeStyle, "aria-hidden": 'true' }, onRenderInitials(this.props, this._onRenderInitials))),
                React.createElement(Image, { className: css('ms-Persona-image', styles.image), imageFit: ImageFit.cover, src: imageUrl, width: coinSize || SIZE_TO_PIXELS[size], height: coinSize || SIZE_TO_PIXELS[size], alt: imageAlt, shouldFadeIn: imageShouldFadeIn, shouldStartVisible: imageShouldStartVisible, onLoadingStateChange: this._onPhotoLoadingStateChange }),
                React.createElement(PersonaPresence, tslib_1.__assign({}, this.props)))) :
                (this.props.presence ?
                    React.createElement(PersonaPresence, tslib_1.__assign({}, this.props)) :
                    React.createElement(Icon, { iconName: 'Contact', className: styles.size10NoPresenceIcon })),
            this.props.children));
    };
    PersonaCoin.prototype._getColorFromName = function (displayName) {
        var color = PersonaInitialsColor.blue;
        if (!displayName) {
            return color;
        }
        var hashCode = 0;
        for (var iLen = displayName.length - 1; iLen >= 0; iLen--) {
            var ch = displayName.charCodeAt(iLen);
            var shift = iLen % 8;
            // tslint:disable-next-line:no-bitwise
            hashCode ^= (ch << shift) + (ch >> (8 - shift));
        }
        color = COLOR_SWATCHES_LOOKUP[hashCode % COLOR_SWATCHES_NUM_ENTRIES];
        return color;
    };
    PersonaCoin.prototype._onRenderInitials = function (props) {
        var imageInitials = props.imageInitials, primaryText = props.primaryText;
        var isRTL = getRTL();
        imageInitials = imageInitials || getInitials(primaryText, isRTL);
        return (imageInitials !== '' ?
            React.createElement("span", null, imageInitials) :
            React.createElement(Icon, { iconName: 'Contact' }));
    };
    PersonaCoin.prototype._onPhotoLoadingStateChange = function (loadState) {
        this.setState({
            isImageLoaded: loadState === ImageLoadState.loaded,
            isImageError: loadState === ImageLoadState.error
        });
    };
    PersonaCoin.defaultProps = {
        primaryText: '',
        size: PersonaSize.size48,
        presence: PersonaPresenceEnum.none,
        imageAlt: ''
    };
    tslib_1.__decorate([
        autobind
    ], PersonaCoin.prototype, "_onRenderInitials", null);
    tslib_1.__decorate([
        autobind
    ], PersonaCoin.prototype, "_onPhotoLoadingStateChange", null);
    return PersonaCoin;
}(React.Component));
export { PersonaCoin };
var _a;
//# sourceMappingURL=PersonaCoin.js.map