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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _InputText = require('./../InputText');

var _InputText2 = _interopRequireDefault(_InputText);

var _Bridge = require('./../Bridge');

var _Bridge2 = _interopRequireDefault(_Bridge);

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

  function InputLocation(props, context) {
    _classCallCheck(this, InputLocation);

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

    _this.onClick = function (event, value) {
      // 全局配置
      var _this$context$locale = _this.context.locale,
          locale = _this$context$locale === undefined ? {} : _this$context$locale;
      var _this$props = _this.props,
          _this$props$locationi = _this$props.locationingValue,
          locationingValue = _this$props$locationi === undefined ? locale['location'] || '定位中...' : _this$props$locationi,
          _this$props$failedVal = _this$props.failedValue,
          failedValue = _this$props$failedVal === undefined ? locale['hint_location_failed'] || '定位失败, 请检查定位权限是否开启' : _this$props$failedVal,
          onChange = _this$props.onChange,
          onClick = _this$props.onClick;

      var e = event.nativeEvent;
      if (onClick) onClick(e, value);
      if (_this.$input.value === locationingValue) return;
      // 定位中...
      _this.$input.value = locationingValue;
      _Bridge2.default.getLocation({
        type: 'gcj02',
        success: function success(data) {
          // 客户端中不需要再getAddress
          if (data.address) {
            // 赋值
            if (onChange) {
              onChange(e, data.address, data);
              _this.$input.value = data.address;
            }
            return;
          }
          _Bridge2.default.getAddress({ // 只支持gcj02
            latitude: data.latitude,
            longitude: data.longitude,
            success: function success(addrs) {
              // 赋值
              if (onChange) onChange(e, addrs.address, data);
              _this.$input.value = addrs.address;
            },
            fail: function fail(res) {
              // 赋值
              if (onChange) onChange(e, '', data);
              _this.$input.value = failedValue;
              // 提示获取地址失败
              _Bridge2.default.showToast(res.errMsg, { mask: false });
            }
          });
        },
        fail: function fail(res) {
          // 赋值
          if (onChange) onChange(e, '', null);
          _this.$input.value = failedValue;
          // 提示定位失败
          _Bridge2.default.showToast(res.errMsg, { mask: false });
        }
      });
    };

    return _this;
  }
  // 全局配置


  InputLocation.prototype.componentDidMount = function componentDidMount() {
    this.$el = null;
    this.$input = null;
    if (this.refs.$ComponentInputText && this.refs.$ComponentInputText.$el && this.refs.$ComponentInputText.$input) {
      this.$el = this.refs.$ComponentInputText.$el;
      this.$input = this.refs.$ComponentInputText.$input;
      this.$ComponentInputText = this.refs.$ComponentInputText;
    }
  };

  InputLocation.prototype.render = function render() {
    // 全局配置
    var _context$locale = this.context.locale,
        locale = _context$locale === undefined ? {} : _context$locale;

    var _props = this.props,
        _props$locationingVal = _props.locationingValue,
        locationingValue = _props$locationingVal === undefined ? locale['location'] || '定位中...' : _props$locationingVal,
        _props$failedValue = _props.failedValue,
        failedValue = _props$failedValue === undefined ? locale['hint_location_failed'] || '定位失败, 请检查定位权限是否开启' : _props$failedValue,
        onChange = _props.onChange,
        onClick = _props.onClick,
        others = _objectWithoutProperties(_props, ['locationingValue', 'failedValue', 'onChange', 'onClick']);

    return _react2.default.createElement(_InputText2.default, _extends({ ref: '$ComponentInputText' }, others, { readOnly: true, onClick: this.onClick }));
  };

  return InputLocation;
}(_react.Component), _class.contextTypes = {
  locale: _propTypes2.default.object,
  portal: _propTypes2.default.object
}, _temp);
exports.default = InputLocation;
InputLocation.propTypes = process.env.NODE_ENV !== "production" ? {
  locationingValue: _propTypes2.default.string,
  failedValue: _propTypes2.default.string,
  onClick: _propTypes2.default.func,
  onChange: _propTypes2.default.func
} : {};
module.exports = exports['default'];