'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactNative = require('react-native');

var _createReactClass = require('create-react-class');

var _createReactClass2 = _interopRequireDefault(_createReactClass);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

function until(test, iterator, callback) {
    if (!test()) {
        iterator(function (err) {
            if (err) {
                return callback(err);
            }
            until(test, iterator, callback);
        });
    } else {
        callback();
    }
}
var Marquee = (0, _createReactClass2['default'])({
    displayName: 'Marquee',
    getDefaultProps: function getDefaultProps() {
        return {
            text: '',
            loop: false,
            leading: 500,
            trailing: 800,
            fps: 40
        };
    },
    getInitialState: function getInitialState() {
        this.texts = {};
        return {
            left: new _reactNative.Animated.Value(0)
        };
    },
    onLayout: function onLayout(e, i) {
        var _this = this;

        this.texts[i] = e.nativeEvent.layout.width;
        var textList = this.props.text.split('');
        var textkeys = Object.keys(this.texts);
        if (textkeys.length === textList.length) {
            this.twidth = textkeys.reduce(function (prev, curKey) {
                return prev + _this.texts[curKey];
            }, 0);
            this.texts = {};
            if (this.twidth > this.width) {
                until(function () {
                    return _this.width > 0;
                }, function (cb) {
                    return setTimeout(cb, 10);
                }, function () {
                    return _this.startMove();
                });
            }
        }
    },
    onLayoutContainer: function onLayoutContainer(e) {
        if (!this.width) {
            this.width = e.nativeEvent.layout.width;
            this.setState({
                left: new _reactNative.Animated.Value(0)
            });
        }
    },
    startMove: function startMove() {
        var _this2 = this;

        var SPPED = 1 / this.props.fps * 1000;
        var width = this.width,
            twidth = this.twidth,
            props = this.props;

        _reactNative.Animated.timing(this.state.left, {
            toValue: -twidth + width,
            duration: twidth * SPPED,
            easing: _reactNative.Easing.linear,
            delay: props.leading
        }).start(function () {
            if (_this2.props.loop) {
                _this2.moveToHeader();
            }
        });
    },
    moveToHeader: function moveToHeader() {
        var _this3 = this;

        _reactNative.Animated.timing(this.state.left, {
            toValue: 0,
            duration: 0,
            delay: this.props.trailing
        }).start(function () {
            _this3.startMove();
        });
    },
    render: function render() {
        var _this4 = this;

        var _props = this.props,
            style = _props.style,
            text = _props.text;

        var textChildren = text.split('').map(function (t, i) {
            return _react2['default'].createElement(
                _reactNative.Text,
                { key: i, onLayout: function onLayout(e) {
                        return _this4.onLayout(e, i);
                    }, style: style },
                t
            );
        });
        return _react2['default'].createElement(
            _reactNative.View,
            { style: { flex: 1, flexDirection: 'row', alignItems: 'center' }, onLayout: this.onLayoutContainer },
            _react2['default'].createElement(
                _reactNative.Animated.View,
                { style: { flexDirection: 'row', left: this.state.left } },
                textChildren
            )
        );
    }
});
exports['default'] = Marquee;
module.exports = exports['default'];