"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var $ = require("jquery");
var React = require("react");
var id = 1000;
var genId = function () {
    id++;
    return '__scroll__' + id;
};
/**
 * ScrollList
*/
var default_1 = /** @class */ (function (_super) {
    __extends(default_1, _super);
    function default_1() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.id = genId();
        _this.interval = null;
        _this.canScroll = false;
        return _this;
    }
    default_1.prototype.marquee = function (root, step) {
        var container = $('#' + this.id).parent();
        root.animate({
            marginTop: '-=1'
        }, 0, function () {
            var s = Math.abs(parseInt($(this).css("margin-top")));
            if (s >= step) {
                $(this).find('li').slice(0, 1).appendTo($(this));
                $(this).css("margin-top", 0);
            }
        });
    };
    default_1.prototype.init = function (init) {
        var _this = this;
        if (init === void 0) { init = false; }
        var speed = this.props.speed || 50;
        var container = $('#' + this.id).parent();
        container.css({ overflow: 'hidden' });
        var root = $('#' + this.id);
        var height = root.height();
        root.css({ marginTop: 0 });
        var lis = root.children();
        if (lis.length <= 1) {
            return;
        }
        var liHeight = $(lis[0]).height();
        if (liHeight * lis.length > height) {
            this.canScroll = true;
            this.interval = window.setInterval(function () {
                _this.marquee(root, liHeight);
            }, speed);
        }
        else {
            if (this.props.alwaysScroll) {
                this.canScroll = true;
                this.interval = window.setInterval(function () {
                    _this.marquee(root, liHeight);
                }, speed);
            }
        }
        if (init) {
            root.hover(function () {
                if (_this.canScroll) {
                    window.clearInterval(_this.interval);
                }
            }, function () {
                if (_this.canScroll) {
                    _this.interval = window.setInterval(function () {
                        _this.marquee(root, liHeight);
                    }, speed);
                }
            });
        }
    };
    default_1.prototype.componentDidMount = function () {
        this.init(true);
    };
    default_1.prototype.componentDidUpdate = function (old) {
        if (this.props.list != old.list) {
            this.canScroll = false;
            window.clearInterval(this.interval);
            this.interval = null;
            this.init();
        }
    };
    default_1.prototype.render = function () {
        var _this = this;
        return React.createElement("ul", { id: this.id, className: this.props.className, style: { width: '100%', height: '100%' } }, this.props.list.map(function (item) { return _this.props.render(item); }));
    };
    return default_1;
}(React.PureComponent));
exports.default = default_1;
