"use strict";

function _typeof(obj) { 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"] = CountrySelect;

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

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

var _classnames = _interopRequireDefault(require("classnames"));

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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function CountrySelect(_ref) {
  var value = _ref.value,
      onChange = _ref.onChange,
      options = _ref.options,
      className = _ref.className,
      Icon = _ref.iconComponent,
      SelectArrow = _ref.selectArrowComponent,
      rest = _objectWithoutProperties(_ref, ["value", "onChange", "options", "className", "iconComponent", "selectArrowComponent"]);

  var onChange_ = (0, _react.useCallback)(function (event) {
    var value = event.target.value;
    onChange(value === 'ZZ' ? undefined : value);
  }, [onChange]);
  var selectedOption = (0, _react.useMemo)(function () {
    for (var _iterator = options, _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 option = _ref2;

      if (!option.divider && option.value === value) {
        return option;
      }
    }
  }, [options, value]);
  return _react["default"].createElement("div", {
    className: "PhoneInputCountry"
  }, selectedOption && _react["default"].createElement(Icon, {
    country: value
  }), _react["default"].createElement("select", _extends({}, rest, {
    value: value || 'ZZ',
    onChange: onChange_,
    className: "PhoneInputCountrySelect"
  }), options.map(function (_ref3) {
    var value = _ref3.value,
        label = _ref3.label,
        divider = _ref3.divider;
    return _react["default"].createElement("option", {
      key: divider ? '|' : value || 'ZZ',
      value: divider ? '|' : value || 'ZZ',
      disabled: divider ? true : false,
      className: divider ? 'PhoneInputCountrySelectDivider' : undefined
    }, label);
  })), _react["default"].createElement(SelectArrow, null));
}

CountrySelect.propTypes = {
  // A two-letter country code.
  // E.g. "US", "RU", etc.
  value: _propTypes["default"].string,
  // Updates the `value`.
  onChange: _propTypes["default"].func.isRequired,
  // `<select/>` options.
  options: _propTypes["default"].arrayOf(_propTypes["default"].shape({
    value: _propTypes["default"].string,
    label: _propTypes["default"].string,
    divider: _propTypes["default"].bool
  })).isRequired,
  // Country flag component.
  iconComponent: _propTypes["default"].elementType,
  // Select arrow component.
  selectArrowComponent: _propTypes["default"].elementType.isRequired
};
CountrySelect.defaultProps = {
  selectArrowComponent: function selectArrowComponent() {
    return _react["default"].createElement("div", {
      className: "PhoneInputCountrySelectArrow"
    });
  }
};
//# sourceMappingURL=CountrySelect.js.map