'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 _instanceData = require('./instance.data.js');

var _instanceData2 = _interopRequireDefault(_instanceData);

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 PickerCity = (_temp = _class = function (_Component) {
  _inherits(PickerCity, _Component);

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

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

    _initialiseProps.call(_this);

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


  PickerCity.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,
        data = _props.data,
        dataFormat = _props.dataFormat,
        split = _props.split,
        type = _props.type,
        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,
        others = _objectWithoutProperties(_props, ['portal', 'data', 'dataFormat', 'split', 'type', 'show', 'value', 'valueForKey', 'maskAttribute', 'submitAttribute', 'cancelAttribute']);
    // 剔除掉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 PickerCity;
}(_react.Component), _class.contextTypes = {
  locale: _propTypes2.default.object,
  portal: _propTypes2.default.object
}, _class.defaultProps = {
  data: null,
  dataFormat: {
    keyName: 'key',
    valueName: 'value',
    childName: 'children'
  },
  split: '-',
  type: 'district'
}, _initialiseProps = function _initialiseProps() {
  var _this3 = this;

  this.componentDidMount = function () {
    _this3.initInstance();
  };

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

  this.componentDidUpdate = function () {
    if (_this3.instance) {
      var _props2 = _this3.props,
          show = _props2.show,
          _data = _props2.data,
          dataFormat = _props2.dataFormat;

      if (show) {
        if (_data) {
          _this3.instance.setData(_this3.props.data, {
            dataChildPropertyName: dataFormat.childName,
            dataKeyPropertyName: dataFormat.keyName,
            dataValuePropertyName: dataFormat.valueName
          });
        }
        _this3.setDefault();
        _this3.instance.show();
      } else _this3.instance.hide();
    }
  };

  this.setDefault = function () {
    var _props3 = _this3.props,
        valueForKey = _props3.valueForKey,
        split = _props3.split;

    if (valueForKey && valueForKey.split(split).length > 1 && valueForKey.split(split).some(function (key) {
      return !isNaN(key);
    })) {
      _this3.instance.setDefaultKeys(valueForKey.split(split));
    } else {
      var defaultValues = _this3.getDefaultValues();
      _this3.instance.setDefaultValues(defaultValues);
    }
    _this3.instance.update();
  };

  this.getDefaultValues = function () {
    var _props4 = _this3.props,
        value = _props4.value,
        split = _props4.split;
    // 默认值

    var defaultValue = value;
    var defaultValues = [];
    if (defaultValue) {
      defaultValues = defaultValue.split(split).map(function (item) {
        return item.trim();
      });
    }
    return defaultValues;
  };

  this.getDefaultKeys = function () {
    var _props5 = _this3.props,
        valueForKey = _props5.valueForKey,
        split = _props5.split;

    if (valueForKey && valueForKey.split(split).length > 1) {
      return valueForKey.split(split);
    }
    return ['', '', ''];
  };

  this.initInstance = function () {
    var defaultValues = _this3.getDefaultValues();
    var defaultKeys = _this3.getDefaultKeys();
    // render数据
    var _props6 = _this3.props,
        dataFormat = _props6.dataFormat,
        split = _props6.split,
        type = _props6.type,
        show = _props6.show,
        _props6$maskAttribute = _props6.maskAttribute,
        maskAttribute = _props6$maskAttribute === undefined ? {} : _props6$maskAttribute,
        _props6$submitAttribu = _props6.submitAttribute,
        submitAttribute = _props6$submitAttribu === undefined ? {} : _props6$submitAttribu,
        _props6$cancelAttribu = _props6.cancelAttribute,
        cancelAttribute = _props6$cancelAttribu === undefined ? {} : _props6$cancelAttribu;

    _this3.instance = new _instance2.default({
      data: _this3.props.data || _instanceData2.default,
      dataKeyPropertyName: dataFormat.keyName,
      dataValuePropertyName: dataFormat.valueName,
      dataChildPropertyName: dataFormat.childName,

      mask: _this3.$el,
      split: split,
      viewType: type,
      defaultProvinceKey: defaultKeys[0] || '',
      defaultCityKey: defaultKeys[1] || '',
      defaultDistrictKey: defaultKeys[2] || '',
      defaultProvince: defaultValues[0] || '',
      defaultCity: defaultValues[1] || '',
      defaultDistrict: defaultValues[2] || '',
      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) {}
    });
    if (show && _this3.instance) {
      setTimeout(function () {
        this.instance.show();
      }, 10);
    }
  };

  this.filterProps = function (props) {
    var propsed = {};
    for (var n in props) {
      if (n !== 'onClick') {
        propsed[n] = props[n];
      }
    }
    return propsed;
  };
}, _temp);
exports.default = PickerCity;
PickerCity.propTypes = process.env.NODE_ENV !== "production" ? {
  portal: _propTypes2.default.object,
  data: _propTypes2.default.array,
  dataFormat: _propTypes2.default.object, // {keyName: 'key', valueName: 'value', childName: 'children'}
  split: _propTypes2.default.string,

  type: _propTypes2.default.string, // district | city
  show: _propTypes2.default.bool,
  value: _propTypes2.default.string, // '北京-东城区'
  valueForKey: _propTypes2.default.string, // '110000-110101'

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