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;

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; }

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { createPortal } from 'react-dom';

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

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

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

    _initialiseProps.call(_this);

    return _this;
  }
  // 获取选中的key

  // 点击遮罩

  // 过滤已经回调的属性


  PickerSelect.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,
        multiple = _props.multiple,
        list = _props.list,
        split = _props.split,
        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$optionAttribut = _props.optionAttribute,
        optionAttribute = _props$optionAttribut === undefined ? {} : _props$optionAttribut,
        others = _objectWithoutProperties(_props, ['portal', 'multiple', 'list', 'split', 'show', 'value', 'valueForKey', 'maskAttribute', 'submitAttribute', 'cancelAttribute', 'optionAttribute']);
    // 如果没有数据, 则不显示


    if (!list || !list.length) return null;
    // 剔除掉onClick事件, 因为在instance时已经回调了
    maskAttribute = this.filterProps(maskAttribute);
    submitAttribute = this.filterProps(submitAttribute);
    cancelAttribute = this.filterProps(cancelAttribute);
    optionAttribute = this.filterProps(optionAttribute);
    return createPortal(React.createElement(
      'div',
      _extends({ ref: function ref(el) {
          _this2.$el = el;
        } }, maskAttribute, { className: 'mask picker-mask' + (maskAttribute.className ? ' ' + maskAttribute.className : '') + (show ? ' active' : ''), onClick: this.onClick }),
      React.createElement(
        'div',
        _extends({}, others, { className: 'pickerselect' + (others.className ? ' ' + others.className : '') + (show ? ' active' : '') }),
        React.createElement(
          'div',
          { className: 'picker-header' },
          React.createElement(
            'a',
            _extends({}, cancelAttribute, { className: 'picker-cancel' + (cancelAttribute.className ? ' ' + cancelAttribute.className : '') }),
            cancelAttribute.caption || locale['cancel'] || '取消'
          ),
          React.createElement(
            'a',
            _extends({}, submitAttribute, { className: 'picker-submit' + (submitAttribute.className ? ' ' + submitAttribute.className : '') + (multiple ? '' : ' disabled') }),
            cancelAttribute.caption || locale['finish'] || '完成'
          )
        ),
        React.createElement(
          'div',
          { className: 'pickerselect-wrapper' },
          list.map(function (item, index) {
            return React.createElement(
              'div',
              _extends({ key: index }, optionAttribute, { className: 'pickerselect-option' + (optionAttribute.className ? ' ' + optionAttribute.className : ''), 'data-index': index }),
              React.createElement(
                'p',
                { className: 'pickerselect-option-caption' },
                item.value
              ),
              React.createElement('i', { className: 'pickerselect-option-icon' })
            );
          })
        )
      )
    ), portal || this.context.portal || document.getElementById('root') || document.body);
  };

  return PickerSelect;
}(Component), _class.contextTypes = {
  locale: PropTypes.object,
  portal: PropTypes.object
}, _class.defaultProps = {
  split: ','
}, _initialiseProps = function _initialiseProps() {
  var _this3 = this;

  this.componentDidMount = function () {};

  this.componentDidUpdate = function (prevProps) {
    var _props2 = _this3.props,
        list = _props2.list,
        show = _props2.show;

    if (!list || !list.length) return;
    if (_this3.$el && show !== prevProps.show) {
      var selectedKeys = Object.values(_this3.getSelectedKeys() || []) || [];
      [].slice.call(_this3.$el.querySelectorAll('.pickerselect-option')).forEach(function (n, i) {
        if (selectedKeys.indexOf(list[i].key) !== -1) {
          n.classList.add('active');
        } else {
          n.classList.remove('active');
        }
      });
    }
  };

  this.getSelectedKeys = function () {
    var _props3 = _this3.props,
        list = _props3.list,
        valueForKey = _props3.valueForKey,
        value = _props3.value,
        split = _props3.split;

    var selected = valueForKey || value;
    var selectedName = valueForKey ? 'key' : 'value';
    if (selected) {
      var options = selected.split(split || ',').map(function (val) {
        for (var i = 0, option; option = list[i++];) {
          // eslint-disable-line
          if (option[selectedName] === val) {
            return option.key;
          }
        }
        return '';
      });
      return options;
    } else {
      return [];
    }
  };

  this.onClick = function (e) {
    var _props4 = _this3.props,
        multiple = _props4.multiple,
        list = _props4.list,
        _props4$maskAttribute = _props4.maskAttribute,
        maskAttribute = _props4$maskAttribute === undefined ? {} : _props4$maskAttribute,
        _props4$optionAttribu = _props4.optionAttribute,
        optionAttribute = _props4$optionAttribu === undefined ? {} : _props4$optionAttribu,
        _props4$submitAttribu = _props4.submitAttribute,
        submitAttribute = _props4$submitAttribu === undefined ? {} : _props4$submitAttribu,
        _props4$cancelAttribu = _props4.cancelAttribute,
        cancelAttribute = _props4$cancelAttribu === undefined ? {} : _props4$cancelAttribu;

    if (e.target.classList.contains('picker-mask')) {
      // 点击遮罩
      if (maskAttribute.onClick) maskAttribute.onClick(e);
    } else if (e.target.classList.contains('pickerselect-option')) {
      // 点击项
      if (optionAttribute.onClick) optionAttribute.onClick(e);
      var index = e.target.getAttribute('data-index');
      if (!multiple && submitAttribute.onClick) {
        e.activeOptions = [list[index]];
        e.activeIndex = [Number(index)];
        submitAttribute.onClick(e);
      } else {
        e.target.classList.toggle('active');
      }
    } else if (e.target.classList.contains('picker-submit')) {
      // 点击确定按钮
      var selected = [];
      var selectedIndex = [];
      [].slice.call(_this3.$el.querySelectorAll('.pickerselect-option.active')).forEach(function (n) {
        var index = n.getAttribute('data-index');
        selected.push(list[index]);
        selectedIndex.push(index);
      });
      e.activeOptions = selected;
      e.activeIndex = selectedIndex;
      if (submitAttribute.onClick) submitAttribute.onClick(e);
    } else if (e.target.classList.contains('picker-cancel')) {
      // 点击取消按钮
      if (cancelAttribute.onClick) cancelAttribute.onClick(e);
    }
  };

  this.filterProps = function (props) {
    var propsed = {};
    for (var n in props) {
      if (n !== 'onClick') {
        propsed[n] = props[n];
      }
    }
    return propsed;
  };
}, _temp);
export { PickerSelect as default };
PickerSelect.propTypes = process.env.NODE_ENV !== "production" ? {
  portal: PropTypes.object,
  multiple: PropTypes.bool, // 是否允许多选
  list: PropTypes.array, // [{key: '', value: ''}]
  split: PropTypes.string,

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

  maskAttribute: PropTypes.object,
  submitAttribute: PropTypes.object,
  cancelAttribute: PropTypes.object,
  optionAttribute: PropTypes.object
} : {};