import _moment from "moment";
import _setIntervalMixinJsx from "./set-interval-mixin.jsx";
import _propTypes from "prop-types";
import _createReactClass from "create-react-class";
import _react from "react";

var module = {
    exports: {}
};

var exports = module.exports;
/* The equivalent to jQuery.timeago for react.
 *
 * TimeAgo returns a span containing the amount of time (in English) that has
 * passed since `time`.
 *
 * Takes:
 *     time: an ISO 8601 timestamp
 *     refreshMillis: how often to update, in milliseconds
 *
 * Example:
 *
 *     return <a href={khanAcademy}><TimeAgo time={date} /></a>
 */

const React = _react;
const createReactClass = _createReactClass;
const PropTypes = _propTypes;
const SetIntervalMixin = _setIntervalMixinJsx;
const moment = _moment;

// TODO(joel) i18n
const TimeAgo = createReactClass({
    propTypes: {
        refreshMillis: PropTypes.number,
        time: PropTypes.any.isRequired,
    },
    mixins: [SetIntervalMixin],
    componentDidMount: function() {
        const interval = this.props.refreshMillis || 60000;
        // TODO(joel) why did I have to bind forceUpdate?
        this.setInterval(this.forceUpdate.bind(this), interval);
    },
    render: function() {
        return <span>{moment(this.props.time).fromNow()}</span>;
    },
});

module.exports = TimeAgo;
export default module.exports;
