'use strict';

exports.__esModule = true;
exports.default = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _class, _temp, _initialiseProps;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactDom = require('react-dom');

var _instance = require('./instance.js');

var _instance2 = _interopRequireDefault(_instance);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var Picker = (_temp = _class = function (_Component) {
  _inherits(Picker, _Component);

  // 全局配置
  function Picker(props, context) {
    _classCallCheck(this, Picker);

    var _this = _possibleConstructorReturn(this, _Component.call(this, props, context));

    _initialiseProps.call(_this);

    return _this;
  }
  // 过滤已经回调的属性


  Picker.prototype.render = function render() {
    var _this2 = this;

    // 全局配置
    var _context$locale = this.context.locale,
        locale = _context$locale === undefined ? {} : _context$locale;

    var _props = this.props,
        portal = _props.portal,
        list = _props.list,
        show = _props.show,
        value = _props.value,
        valueForKey = _props.valueForKey,
        _props$maskAttribute = _props.maskAttribute,
        maskAttribute = _props$maskAttribute === undefined ? {} : _props$maskAttribute,
        _props$submitAttribut = _props.submitAttribute,
        submitAttribute = _props$submitAttribut === undefined ? {} : _props$submitAttribut,
        _props$cancelAttribut = _props.cancelAttribute,
        cancelAttribute = _props$cancelAttribut === undefined ? {} : _props$cancelAttribut,
        _props$slotAttribute = _props.slotAttribute,
        slotAttribute = _props$slotAttribute === undefined ? {} : _props$slotAttribute,
        others = _objectWithoutProperties(_props, ['portal', 'list', 'show', 'value', 'valueForKey', 'maskAttribute', 'submitAttribute', 'cancelAttribute', 'slotAttribute']);
    // 剔除掉onClick事件, 因为在instance时已经回调了


    maskAttribute = this.filterProps(maskAttribute);
    submitAttribute = this.filterProps(submitAttribute);
    cancelAttribute = this.filterProps(cancelAttribute);
    return (0, _reactDom.createPortal)(_react2.default.createElement(
      'div',
      _extends({ ref: function ref(el) {
          _this2.$el = el;
        } }, maskAttribute, { className: 'mask picker-mask' + (maskAttribute.className ? ' ' + maskAttribute.className : '') }),
      _react2.default.createElement(
        'div',
        _extends({}, others, { className: 'picker' + (others.className ? ' ' + others.className : '') }),
        _react2.default.createElement(
          'div',
          { className: 'picker-header' },
          _react2.default.createElement(
            'a',
            _extends({}, cancelAttribute, { className: 'picker-cancel' + (cancelAttribute.className ? ' ' + cancelAttribute.className : '') }),
            cancelAttribute.caption || locale['cancel'] || '取消'
          ),
          _react2.default.createElement(
            'a',
            _extends({}, submitAttribute, { className: 'picker-submit' + (submitAttribute.className ? ' ' + submitAttribute.className : '') }),
            cancelAttribute.caption || locale['finish'] || '完成'
          )
        ),
        _react2.default.createElement(
          'div',
          { className: 'picker-wrapper' },
          _react2.default.createElement(
            'div',
            { className: 'picker-layer' },
            _react2.default.createElement('div', { className: 'picker-layer-frame' })
          ),
          _react2.default.createElement('div', { className: 'picker-slotbox' })
        )
      )
    ), portal || this.context.portal || document.getElementById('root') || document.body);
  };

  return Picker;
}(_react.Component), _class.contextTypes = {
  locale: _propTypes2.default.object,
  portal: _propTypes2.default.object
}, _class.defaultProps = {}, _initialiseProps = function _initialiseProps() {
  var _this3 = this;

  this.componentDidMount = function () {
    var list = _this3.props.list;

    if (!list || !list.length) return;
    _this3.initInstance();
  };

  this.shouldComponentUpdate = function (nextProps) {
    if (nextProps.show === _this3.props.show) return false;
    return true;
  };

  this.componentDidUpdate = function () {
    var _props2 = _this3.props,
        _props2$list = _props2.list,
        list = _props2$list === undefined ? [] : _props2$list,
        show = _props2.show;

    if (_this3.instance) {
      if (show && list.length) {
        _this3.setDefault();
        _this3.instance.show();
      } else {
        _this3.instance.hide();
      }
    } else {
      if (list.length > 0) {
        _this3.initInstance();
      }
    }
  };

  this.setDefault = function () {
    var _props3 = _this3.props,
        valueForKey = _props3.valueForKey,
        list = _props3.list,
        _props3$slotAttribute = _props3.slotAttribute,
        slotAttribute = _props3$slotAttribute === undefined ? {} : _props3$slotAttribute;

    var key = valueForKey || '';
    if (!key) {
      var defaultOpt = _this3.getDefaults();
      if (defaultOpt && defaultOpt.key) key = defaultOpt.key;
    }
    _this3.instance.clearSlots();
    _this3.instance.addSlot(list, key || '', slotAttribute.className || 'text-center'); // 添加列,参数:数据,默认key,样式(lock样式为锁定列)
  };

  this.getDefaults = function () {
    var _props4 = _this3.props,
        list = _props4.list,
        valueForKey = _props4.valueForKey,
        value = _props4.value;

    if (!valueForKey && !value) {
      if (list && list[0]) return list[0];
      return [{ key: '', value: '' }];
    }
    var values = list.filter(function (item) {
      if (valueForKey) {
        if (valueForKey === item.key) return true;
      } else if (value) {
        if (item.value === value) return true;
      }
      return false;
    });
    return values[0];
  };

  this.initInstance = function () {
    var _props5 = _this3.props,
        list = _props5.list,
        show = _props5.show,
        _props5$maskAttribute = _props5.maskAttribute,
        maskAttribute = _props5$maskAttribute === undefined ? {} : _props5$maskAttribute,
        _props5$submitAttribu = _props5.submitAttribute,
        submitAttribute = _props5$submitAttribu === undefined ? {} : _props5$submitAttribu,
        _props5$cancelAttribu = _props5.cancelAttribute,
        cancelAttribute = _props5$cancelAttribu === undefined ? {} : _props5$cancelAttribu,
        _props5$slotAttribute = _props5.slotAttribute,
        slotAttribute = _props5$slotAttribute === undefined ? {} : _props5$slotAttribute;

    if (!list || list.length === 0 || _this3.instance) return;
    // render数据
    _this3.instance = new _instance2.default({
      mask: _this3.$el,
      onClickMask: function onClickMask(e) {
        if (maskAttribute.onClick) maskAttribute.onClick(e);
      },
      onClickCancel: function onClickCancel(e) {
        if (cancelAttribute.onClick) cancelAttribute.onClick(e);
      },
      onClickSubmit: function onClickSubmit(e) {
        if (submitAttribute.onClick) submitAttribute.onClick(e);
      },
      onHid: function onHid(e) {}
    });
    // 默认项
    var defaultOpt = _this3.getDefaults();
    var key = '';
    if (defaultOpt && defaultOpt.key) key = defaultOpt.key;
    _this3.instance.addSlot(list, key, slotAttribute.className || 'text-center');
    if (show && _this3.instance) {
      _this3.instance.show();
    }
  };

  this.filterProps = function (props) {
    var propsed = {};
    for (var n in props) {
      if (n !== 'onClick') {
        propsed[n] = props[n];
      }
    }
    return propsed;
  };
}, _temp);
exports.default = Picker;
Picker.propTypes = process.env.NODE_ENV !== "production" ? {
  portal: _propTypes2.default.object,
  list: _propTypes2.default.array, // [{key: '', value: ''}]

  show: _propTypes2.default.bool,
  value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
  valueForKey: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),

  maskAttribute: _propTypes2.default.object,
  submitAttribute: _propTypes2.default.object,
  cancelAttribute: _propTypes2.default.object,
  slotAttribute: _propTypes2.default.object
} : {};
module.exports = exports['default'];