'use strict';

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

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

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

var Pipe = function (_Component) {
	(0, _inherits3.default)(Pipe, _Component);

	function Pipe(props, context) {
		(0, _classCallCheck3.default)(this, Pipe);
		return (0, _possibleConstructorReturn3.default)(this, (Pipe.__proto__ || Object.getPrototypeOf(Pipe)).call(this, props, context));
	}

	(0, _createClass3.default)(Pipe, [{
		key: 'renderResult',
		value: function renderResult() {
			var _props = this.props,
			    mode = _props.mode,
			    value = _props.children,
			    onConvert = _props.onConvert;
			var _props2 = this.props,
			    extra = _props2.extra,
			    start = _props2.start,
			    length = _props2.length;

			if (onConvert) return onConvert(value);
			switch (mode) {
				case 'ellipsis':
					var isEnough = value.length > length;
					return isEnough ? '' + value.substring(start, start + length) + extra : value;
				case 'auto': // 自适应 待开发
				// 1. 2 4 6 8 .. 一次改变
				// 2. 根据高度自适应
				default:
					return value;
			}
		}
	}, {
		key: 'render',
		value: function render() {
			var _props3 = this.props,
			    Tag = _props3.tag,
			    rest = (0, _objectWithoutProperties3.default)(_props3, ['tag']);

			return _react2.default.createElement(
				Tag,
				rest,
				this.renderResult()
			);
		}
	}]);
	return Pipe;
}(_react.Component);

Pipe.propTypes = {
	/**
  * 默认标签 span
  */
	tag: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.func]),
	/**
  * 子元素必须是string
  */
	children: _propTypes2.default.string,
	/**
  * 自定义转换器
  */
	onConvert: _propTypes2.default.func,
	/**
  * 模式
  * ellipsis | auto | none
  */
	mode: _propTypes2.default.string,

	/**
  * ellipsis 模式
  */
	start: _propTypes2.default.number,
	length: _propTypes2.default.number,
	extra: _propTypes2.default.string
};
Pipe.defaultProps = {
	tag: 'span',
	mode: 'ellipsis',
	start: 0,
	length: 5,
	extra: '...'
};
exports.default = Pipe;