'use strict';

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

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

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 Authorized = function (_Component) {
	(0, _inherits3.default)(Authorized, _Component);

	function Authorized(props, context) {
		(0, _classCallCheck3.default)(this, Authorized);

		var _this = (0, _possibleConstructorReturn3.default)(this, (Authorized.__proto__ || Object.getPrototypeOf(Authorized)).call(this, props, context));

		_this.setValue = function ($props) {
			var _$props$auth = $props.auth,
			    auth = _$props$auth === undefined ? [] : _$props$auth;
			var show = _this.state.show;

			var status = true;
			var funcs = [];

			for (var i = 0; i < auth.length; i++) {
				// if (typeof auth[i] === 'function') {
				// 	funcs = [...funcs, auth[i]];
				// 	continue;
				// }
				if (!auth[i]) {
					status = false;
					break;
				}
			}
			// if (status) {
			// 	for (let i = 0; i < funcs.length; i++) {
			// 		status = funcs[i]();
			// 		if (!status) {
			// 			status = false;
			// 			break;
			// 		}
			// 	}
			// }
			if (show !== status) {
				_this.setState({
					show: status
				});
			}
		};

		_this.state = {
			show: false
		};
		return _this;
	}

	(0, _createClass3.default)(Authorized, [{
		key: 'componentWillMount',
		value: function componentWillMount() {
			this.setValue(this.props);
		}
	}, {
		key: 'componentWillReceiveProps',
		value: function componentWillReceiveProps(nextProps) {
			var _this2 = this;

			var isChange = nextProps.auth.every(function (item, index) {
				if (item === _this2.props.auth[index]) {
					return false;
				} else {
					return true;
				}
			});
			if (isChange) {
				this.setValue(nextProps);
			}
		}
	}, {
		key: 'render',
		value: function render() {
			var show = this.state.show;

			if (!show) return null;

			var _props = this.props,
			    auth = _props.auth,
			    Tag = _props.tag,
			    rest = (0, _objectWithoutProperties3.default)(_props, ['auth', 'tag']);

			return (0, _react.createElement)(Tag, (0, _extends3.default)({}, rest, { show: typeof Tag === 'string' ? 'true' : true }));
		}
	}]);
	return Authorized;
}(_react.Component);

Authorized.propTypes = {
	/**
  * 默认标签 div
  */
	tag: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.func]),
	// auth: PropTypes.arrayOf(PropTypes.oneOfType([
	// 	PropTypes.bool,
	// 	PropTypes.func
	// ]))
	auth: _propTypes2.default.arrayOf(_propTypes2.default.bool)
};
Authorized.defaultProps = {
	tag: 'div'
};
exports.default = Authorized;