"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var React = require("react");
var Utilities_1 = require("../../Utilities");
var Rating_Props_1 = require("./Rating.Props");
var Rating_scss_1 = require("./Rating.scss");
var Rating = (function (_super) {
    __extends(Rating, _super);
    function Rating(props) {
        var _this = _super.call(this, props) || this;
        _this.state = {
            rating: _this._getInitialValue(props),
            focusedRating: null
        };
        _this._id = Utilities_1.getId('Rating');
        _this._labelId = Utilities_1.getId('RatingLabel');
        return _this;
    }
    Rating.prototype.componentWillReceiveProps = function (nextProps) {
        if (typeof nextProps.rating !== 'undefined' && nextProps.rating !== this.state.rating) {
            this.setState({
                rating: this._getClampedRating(nextProps.rating)
            });
        }
    };
    Rating.prototype.render = function () {
        var stars = [];
        for (var i = this.props.min; i <= this.props.max; ++i) {
            stars.push(this._renderStar(i));
        }
        return React.createElement("div", { className: Utilities_1.css('ms-Rating', this.props.className, (_a = {},
                _a['ms-Rating--large ' + Rating_scss_1.default.rootIsLarge] = this.props.size === Rating_Props_1.RatingSize.Large,
                _a)), role: 'application' },
            React.createElement("div", { className: Utilities_1.css('ms-Rating-container', Rating_scss_1.default.container), role: 'radiogroup', "aria-labelledby": this.props.ariaLabelId }, stars));
        var _a;
    };
    Rating.prototype._renderStar = function (rating) {
        var inputId = this._id + "-" + rating;
        return React.createElement("div", { className: Utilities_1.css('ms-Rating-star', Rating_scss_1.default.star, (_a = {},
                _a['is-selected ' + Rating_scss_1.default.starIsSelected] = rating <= this.state.rating,
                _a['is-inFocus ' + Rating_scss_1.default.starIsInFocus] = rating === this.state.focusedRating,
                _a['is-disabled ' + Rating_scss_1.default.starIsDisabled] = this.props.disabled,
                _a)), key: rating },
            React.createElement("input", { className: Utilities_1.css('ms-Rating-input', Rating_scss_1.default.input), type: 'radio', name: this._id, id: inputId, value: rating, "aria-labelledby": this._labelId + "-" + rating, disabled: this.props.disabled, checked: rating === this.state.rating, onChange: this._onChange.bind(this, rating), onFocus: this._onFocus.bind(this, rating), onBlur: this._onBlur.bind(this, rating) }),
            React.createElement("label", { className: Utilities_1.css('ms-Rating-label', Rating_scss_1.default.label), htmlFor: inputId },
                this._getLabel(rating),
                this._getIcon()));
        var _a;
    };
    Rating.prototype._onFocus = function (value, ev) {
        this.setState({
            focusedRating: value
        });
    };
    Rating.prototype._onBlur = function (option, ev) {
        this.setState({
            focusedRating: null
        });
    };
    Rating.prototype._onChange = function (rating, evt) {
        this.setState({
            rating: rating
        });
        var onChanged = this.props.onChanged;
        if (onChanged) {
            onChanged(rating);
        }
    };
    Rating.prototype._getLabel = function (rating) {
        var text = this.props.ariaLabelIcon || 'Star';
        return (React.createElement("span", { id: this._labelId + "-" + rating, className: Utilities_1.css('ms-Rating-labelText', Rating_scss_1.default.labelText) }, rating + " " + text));
    };
    Rating.prototype._getIcon = function () {
        return React.createElement("i", { className: Utilities_1.css('ms-Icon', this.props.icon || 'ms-Icon--FavoriteStarFill') });
    };
    Rating.prototype._getInitialValue = function (props) {
        if (typeof props.rating === 'undefined') {
            return props.min;
        }
        if (props.rating === null) {
            return null;
        }
        return this._getClampedRating(props.rating);
    };
    Rating.prototype._getClampedRating = function (rating) {
        rating = Math.floor(rating);
        return Math.min(Math.max(rating, this.props.min), this.props.max);
    };
    return Rating;
}(Utilities_1.BaseComponent));
Rating.defaultProps = {
    min: 1,
    max: 5
};
exports.Rating = Rating;

//# sourceMappingURL=Rating.js.map
