"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.google2address = google2address;
exports.trySetStreetNumberIfNotReceived = exports.includes = void 0;
var _isUndefined = _interopRequireDefault(require("lodash/isUndefined"));
/* eslint camelcase: off*/

var includes = (arr, value) => {
  return Boolean(arr && arr.find(item => item === value)); // we compare only primitives
};
exports.includes = includes;
var locationFuncOrValue = locationProp => {
  return typeof locationProp === 'function' ? locationProp() : locationProp;
};
var getFormattedStreetAddress = address => {
  try {
    if (!address || !document) {
      return undefined;
    }
    var wrapperElement = document.createElement('div');
    wrapperElement.innerHTML = address;
    var addressElement = wrapperElement.querySelector('.street-address');
    return addressElement ? addressElement.textContent : undefined;
  } catch (e) {
    return undefined;
  }
};
function google2address(google) {
  var components = {};
  google.address_components.forEach(_ref => {
    var {
      types,
      long_name,
      short_name
    } = _ref;
    types.forEach(type => {
      components[type] = {
        long_name,
        short_name
      };
    });
  });
  var locality = components.locality || components.sublocality || components.postal_town;
  var {
    lat,
    lng
  } = google.geometry.location;
  var result = {
    formatted: google.formatted_address,
    formattedStreetAddress: getFormattedStreetAddress(google.adr_address),
    latLng: {
      lat: locationFuncOrValue(lat),
      lng: locationFuncOrValue(lng)
    },
    approximate: !includes(google.types, 'street_address') && !includes(google.types, 'premise'),
    city: locality && locality.long_name,
    state: components.administrative_area_level_1 && components.administrative_area_level_1.short_name,
    country: components.country && components.country.long_name,
    countryCode: components.country && components.country.short_name,
    street: components.route && components.route.long_name,
    number: components.street_number && components.street_number.long_name,
    postalCode: components.postal_code && components.postal_code.long_name,
    subpremise: components.subpremise && components.subpremise.long_name
  };
  for (var key in result) {
    if ((0, _isUndefined.default)(result[key])) {
      delete result[key];
    }
  }
  return result;
}
var trySetStreetNumberIfNotReceived = (google, inputValue) => {
  var addressParts = inputValue.match(/^\d+[ -/]*\d*[^\D]/);
  var hasStreetNumber = google.address_components.some(address => address.types.some(t => t === 'street_number'));
  if (hasStreetNumber || !addressParts) {
    return google;
  }
  google.address_components.unshift({
    long_name: addressParts.join(),
    short_name: addressParts.join(),
    types: ['street_number']
  });
  return google;
};
exports.trySetStreetNumberIfNotReceived = trySetStreetNumberIfNotReceived;
//# sourceMappingURL=google2address.js.map