"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactDom = _interopRequireDefault(require("react-dom"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _cUrl = _interopRequireDefault(require("../c-url"));

var _beeSelect = _interopRequireDefault(require("bee-select"));

var _beeFormControl = _interopRequireDefault(require("bee-form-control"));

var _CountryLocale = _interopRequireDefault(require("./CountryLocale"));

var _lang = _interopRequireDefault(require("../lang"));

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _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; }; return _extends.apply(this, arguments); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

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

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

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

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var Option = _beeSelect["default"].Option;
var propTypes = {
  className: _propTypes["default"].string,
  countryList: _propTypes["default"].array,
  mobile: _propTypes["default"].string,
  onChange: _propTypes["default"].func,
  placeholder: _propTypes["default"].string,
  inputProps: _propTypes["default"].object,
  selectProps: _propTypes["default"].object,
  disabled: _propTypes["default"].bool,
  validate: _propTypes["default"].func // 自定义校验方法，return true校验成功，return false 校验失败

};
var defaultProps = {
  className: '',
  countryList: _CountryLocale["default"],
  placeholder: ''
};

var MobileLocale = /*#__PURE__*/function (_Component) {
  _inherits(MobileLocale, _Component);

  var _super = _createSuper(MobileLocale);

  function MobileLocale(props) {
    var _this;

    _classCallCheck(this, MobileLocale);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "getLanguage", function () {
      var _window$cb, _window$cb$lang, _window$cb2, _window$cb2$rest$AppC, _global, _global2;

      var lang = 'zh_CN';
      var global = (_window$cb = window.cb) === null || _window$cb === void 0 ? void 0 : (_window$cb$lang = _window$cb.lang) === null || _window$cb$lang === void 0 ? void 0 : _window$cb$lang.lang;
      if (global) return global;
      global = ((_window$cb2 = window.cb) === null || _window$cb2 === void 0 ? void 0 : (_window$cb2$rest$AppC = _window$cb2.rest.AppContext) === null || _window$cb2$rest$AppC === void 0 ? void 0 : _window$cb2$rest$AppC.globalization) || global; // 兼容非多语场景

      if ((_global = global) !== null && _global !== void 0 && _global.locale) {
        return global.locale;
      }

      if (window && _cUrl["default"].query('locale')) {
        // url 配置页面语言
        lang = _cUrl["default"].query('locale');
      } else if (_this.getCookie('locale')) {
        lang = _this.getCookie('locale');
      } else if ((_global2 = global) !== null && _global2 !== void 0 && _global2.locale) {
        lang = global.locale;
      } else {// 系统浏览器语言
      }

      if (!lang) console.error('Please add the _lang parameter to the browser !');
      return lang;
    });

    _defineProperty(_assertThisInitialized(_this), "getCookie", function (name) {
      var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
      var arr = document && document.cookie.match(reg);

      if (arr) {
        return unescape(arr[2]);
      } else {
        return null;
      }
    });

    _defineProperty(_assertThisInitialized(_this), "getLocale", function (key) {
      var lang = _this.getLanguage() || 'zh_CN';
      return _lang["default"][lang][key];
    });

    _defineProperty(_assertThisInitialized(_this), "validateValue", function (value) {
      var validate = _this.props.validate;

      if (validate && typeof validate === 'function') {
        return validate(value);
      } else {
        if (!value || value === '') return true;
        var reg = /^[0-9]\d{0,}$/;
        return reg.test(value);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onChange", function (obj) {
      _this.setState(_objectSpread({}, obj));

      _this.props.onChange && _this.props.onChange(_objectSpread({}, obj));
    });

    _defineProperty(_assertThisInitialized(_this), "getFindObj", function (code) {
      var countryList = _this.props.countryList;
      if (!countryList) return _this.zhcnInfo;
      return countryList.forEach(function (da) {
        return da.country_code === code;
      });
    });

    _this.zhcnInfo = {
      shoupinyin: 'Z',
      en: 'China',
      country: _this.getLocale('YS_FED_FW_YONUI_00050003')
      /* "中国" */
      ,
      locale: 'CN',
      country_code: '86'
    };
    var country_code = props.country_code,
        mobile = props.mobile;
    var countryObj = country_code ? _this.getFindObj(country_code) : null;
    _this.state = {
      country_code: countryObj ? countryObj.country_code : _this.zhcnInfo.country_code,
      country: countryObj ? countryObj.country : _this.zhcnInfo.country,
      mobile: mobile || ''
    };
    return _this;
  }

  _createClass(MobileLocale, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      var country_code = nextProps.country_code,
          mobile = nextProps.mobile;

      if (country_code != this.state.country_code || mobile != this.state.mobile) {
        var _country_code = country_code != this.state.country_code ? country_code : this.state.country_code;

        var _mobile = mobile != this.state.mobile ? mobile : this.state.mobile;

        this.setState({
          country_code: _country_code,
          mobile: _mobile
        });
      }
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      this.setState({
        elWidth: _reactDom["default"].findDOMNode(this.refs.the_input).getBoundingClientRect().width
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$state = this.state,
          country_code = _this$state.country_code,
          mobile = _this$state.mobile;
      var _this$props = this.props,
          countryList = _this$props.countryList,
          selectProps = _this$props.selectProps,
          className = _this$props.className,
          inputProps = _this$props.inputProps,
          _placeholder = _this$props.placeholder,
          disabled = _this$props.disabled;
      var _defaultValue = this.zhcnInfo.country_code;
      var styleProp = {};

      if (this.state.elWidth) {
        styleProp = {
          width: parseInt(this.state.elWidth)
        };
      }

      var placeholder = _placeholder || this.getLocale('YS_FED_FW_YONUI_00050005');
      /* "请输入您的手机号" */


      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "mobile-locale ".concat(className),
        ref: "the_input"
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "location"
      }, /*#__PURE__*/_react["default"].createElement(_beeSelect["default"], _extends({
        showSearch: true
      }, selectProps, {
        dropdownClassName: "mobile_locale_select",
        dropdownStyle: styleProp,
        dropdownMatchSelectWidth: false,
        value: country_code || _defaultValue,
        disabled: disabled,
        onChange: function onChange(key, e) {
          var current = countryList.find(function (value) {
            return String(value.country_code) === key;
          }, e);

          _this2.onChange({
            country_code: current.country_code,
            country: current.country,
            mobile: mobile
          });
        }
      }), countryList && countryList.map(function (account) {
        return /*#__PURE__*/_react["default"].createElement(Option, {
          key: account.country_code,
          value: String(account.country_code)
        }, /*#__PURE__*/_react["default"].createElement("span", null, account.country), /*#__PURE__*/_react["default"].createElement("span", {
          className: "opt1"
        }, "+", account.country_code));
      }))), /*#__PURE__*/_react["default"].createElement("div", {
        className: "area"
      }, "+ ", country_code || _defaultValue), /*#__PURE__*/_react["default"].createElement("div", {
        className: "mobile"
      }, /*#__PURE__*/_react["default"].createElement("span", null), /*#__PURE__*/_react["default"].createElement(_beeFormControl["default"], _extends({
        showClose: true,
        onBlur: function onBlur() {
          _this2.props.onBlur && _this2.props.onBlur(_objectSpread({}, _this2.state));
        },
        onChange: function onChange(v) {
          if (!_this2.validateValue(v)) return false; // 校验合法性

          _this2.onChange(_objectSpread(_objectSpread({}, _this2.state), {}, {
            mobile: v
          }));
        },
        disabled: disabled
      }, inputProps, {
        value: mobile,
        placeholder: placeholder
      }))));
    }
  }]);

  return MobileLocale;
}(_react.Component);

MobileLocale.propTypes = propTypes;
MobileLocale.defaultProps = defaultProps;
var _default = MobileLocale;
exports["default"] = _default;
//# sourceMappingURL=MobileLocale.js.map
