'use strict';

exports.__esModule = true;

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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

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

var _instance2 = _interopRequireDefault(_instance);

var _instance3 = require('../Context/instance.js');

var _instance4 = _interopRequireDefault(_instance3);

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

var Picker = (0, _react.forwardRef)(function (_ref, ref) {
  var portal = _ref.portal,
      _ref$list = _ref.list,
      list = _ref$list === undefined ? [] : _ref$list,
      _ref$show = _ref.show,
      show = _ref$show === undefined ? false : _ref$show,
      value = _ref.value,
      selected = _ref.selected,
      _ref$maskAttribute = _ref.maskAttribute,
      maskAttribute = _ref$maskAttribute === undefined ? {} : _ref$maskAttribute,
      _ref$submitAttribute = _ref.submitAttribute,
      submitAttribute = _ref$submitAttribute === undefined ? {} : _ref$submitAttribute,
      _ref$cancelAttribute = _ref.cancelAttribute,
      cancelAttribute = _ref$cancelAttribute === undefined ? {} : _ref$cancelAttribute,
      _ref$slotAttribute = _ref.slotAttribute,
      slotAttribute = _ref$slotAttribute === undefined ? {} : _ref$slotAttribute,
      others = _objectWithoutProperties(_ref, ['portal', 'list', 'show', 'value', 'selected', 'maskAttribute', 'submitAttribute', 'cancelAttribute', 'slotAttribute']);

  var refEl = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return refEl.current;
  });
  var instance = (0, _react.useRef)(null);
  (0, _react.useEffect)(function () {
    initInstance();
  }, []); // eslint-disable-line

  (0, _react.useEffect)(function () {
    if (instance.current) {
      if (show && list.length) {
        setDefault();
        instance.current.show();
      } else {
        instance.current.hide();
      }
    } else if (list && list.length > 0) {
      initInstance();
    }
  }, [show]); // eslint-disable-line

  function setDefault() {
    var defaultOpt = getDefaultOption();
    instance.current.clearSlots();
    instance.current.addSlot(list, defaultOpt.id || '', slotAttribute.className || 'text-center'); // 添加列,参数:数据,默认id,样式(lock样式为锁定列)
  }

  function getDefaultOption() {
    if (selected && selected.length) {
      return selected[0];
    }
    if (value) {
      for (var _iterator = list, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
        var _ref2;

        if (_isArray) {
          if (_i >= _iterator.length) break;
          _ref2 = _iterator[_i++];
        } else {
          _i = _iterator.next();
          if (_i.done) break;
          _ref2 = _i.value;
        }

        var item = _ref2;

        if (item.name === value) return item;
      }
      return null;
    }
    return null;
  }

  function initInstance() {
    if (!list || !list.length || instance.current) {
      console.log('SeedsUI Picker: 参数list为空');
      return;
    }
    // render数据
    instance.current = new _instance2.default({
      mask: refEl.current,
      onClickMask: function onClickMask(e) {
        if (maskAttribute.onClick) maskAttribute.onClick(e);
      },
      onClickCancel: function onClickCancel(e) {
        if (cancelAttribute.onClick) cancelAttribute.onClick(e);
      },
      onClickSubmit: function onClickSubmit(e) {
        var value = e.activeOptions[0].name;
        var options = e.activeOptions;
        if (submitAttribute.onClick) submitAttribute.onClick(e, value, options);
      },
      onHid: function onHid(e) {}
    });
    // 默认项
    var defaultOpt = getDefaultOption();
    var id = '';
    if (defaultOpt && defaultOpt.id) id = defaultOpt.id;
    instance.current.addSlot(list, id, slotAttribute.className || 'text-center');
    if (show && instance.current) {
      instance.current.show();
    }
  }

  // 过滤已经回调的属性
  function filterProps(props) {
    if (!props) return {};

    var onClick = props.onClick,
        otherProps = _objectWithoutProperties(props, ['onClick']);

    return _extends({}, otherProps);
  }

  // context
  var context = (0, _react.useContext)(_instance4.default) || {};
  var locale = context.locale || {};
  // 剔除掉onClick事件, 因为在instance时已经回调了
  var otherMaskAttribute = filterProps(maskAttribute);
  var otherSubmitAttribute = filterProps(submitAttribute);
  var otherCancelAttribute = filterProps(cancelAttribute);
  return (0, _reactDom.createPortal)(_react2.default.createElement(
    'div',
    _extends({ ref: refEl }, otherMaskAttribute, { className: 'mask picker-mask' + (otherMaskAttribute.className ? ' ' + otherMaskAttribute.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({}, otherCancelAttribute, { className: 'picker-cancel' + (otherCancelAttribute.className ? ' ' + otherCancelAttribute.className : '') }),
          otherCancelAttribute.caption || locale['cancel'] || '取消'
        ),
        _react2.default.createElement(
          'a',
          _extends({}, otherSubmitAttribute, { className: 'picker-submit' + (otherSubmitAttribute.className ? ' ' + otherSubmitAttribute.className : '') }),
          otherSubmitAttribute.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 || context.portal || document.getElementById('root') || document.body);
});

exports.default = _react2.default.memo(Picker, function (prevProps, nextProps) {
  if (nextProps.show === prevProps.show) return true;
  return false;
});
module.exports = exports['default'];