Liferay.Loader.define("dynamic-data-mapping-form-field-type@5.0.73/Geolocation/useGeolocation.es", ['module', 'exports', 'require', 'dynamic-data-mapping-form-field-type$leaflet', 'map-google-maps/js/MapGoogleMaps.es', 'map-openstreetmap/js/MapOpenStreetMap.es', 'frontend-js-react-web$react'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.useGeolocation = exports.MAP_PROVIDER = void 0;

    var _leaflet = _interopRequireDefault(require("dynamic-data-mapping-form-field-type$leaflet"));

    var _MapGoogleMaps = _interopRequireDefault(require("map-google-maps/js/MapGoogleMaps.es"));

    var _MapOpenStreetMap = _interopRequireDefault(require("map-openstreetmap/js/MapOpenStreetMap.es"));

    var _react = require("frontend-js-react-web$react");

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

    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 _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 MAP_PROVIDER = {
      googleMaps: 'GoogleMaps',
      openStreetMap: 'OpenStreetMap'
    };
    exports.MAP_PROVIDER = MAP_PROVIDER;
    var CONTROLS = Liferay.MapBase.CONTROLS;
    var MAP_CONFIG = {
      controls: [CONTROLS.HOME, CONTROLS.PAN, CONTROLS.SEARCH, CONTROLS.TYPE, CONTROLS.ZOOM],
      geolocation: true,
      position: {
        location: {
          lat: 0,
          lng: 0
        }
      }
    };

    var setupMapOpenStreetMaps = function setupMapOpenStreetMaps(callback) {
      _leaflet.default.Icon.Default.imagePath = 'https://npmcdn.com/leaflet@1.2.0/dist/images/';

      if (!window['L']) {
        window['L'] = _leaflet.default;
      }

      callback();
    };

    var setupGoogleMaps = function setupGoogleMaps(googleMapsAPIKey, callback) {
      if (window.google && window.google.maps && Liferay.Maps && Liferay.Maps.gmapsReady) {
        callback();
      } else {
        Liferay.namespace('Maps').onGMapsReady = function () {
          Liferay.Maps.gmapsReady = true;
          Liferay.fire('gmapsReady');
        };

        Liferay.once('gmapsReady', function () {
          return callback();
        });
        var apiURL = "".concat(location.protocol, "//maps.googleapis.com/maps/api/js?v=3.exp&libraries=places&callback=Liferay.Maps.onGMapsReady");

        if (googleMapsAPIKey) {
          apiURL += '&key=' + googleMapsAPIKey;
        }

        var script = document.createElement('script');
        script.setAttribute('src', apiURL);
        document.head.appendChild(script);
        script = null;
      }
    };

    var useGeolocation = function useGeolocation(_ref) {
      var disabled = _ref.disabled,
          googleMapsAPIKey = _ref.googleMapsAPIKey,
          instanceId = _ref.instanceId,
          mapProviderKey = _ref.mapProviderKey,
          name = _ref.name,
          onChange = _ref.onChange,
          value = _ref.value,
          viewMode = _ref.viewMode;
      var componentInstance = (0, _react.useRef)(null);

      var eventHandlerPositionChanged = function eventHandlerPositionChanged(event) {
        var location = event.newVal.location;
        var newValue = JSON.stringify(location);
        document.getElementById("input_value_".concat(instanceId)).setAttribute('value', newValue);
        onChange(newValue);
      };

      var registerMapBase = function registerMapBase(MapProvide, mapConfig) {
        componentInstance.current = new MapProvide(mapConfig);
        componentInstance.current.on('positionChange', eventHandlerPositionChanged);
        Liferay.MapBase.register(name, componentInstance.current, "#map_".concat(instanceId));
      };

      (0, _react.useEffect)(function () {
        if (!disabled || viewMode) {
          var mapConfig = _objectSpread({}, MAP_CONFIG);

          mapConfig.boundingBox = "#map_".concat(instanceId);

          if (value && disabled) {
            mapConfig.position.location = JSON.parse(value);
          }

          switch (mapProviderKey) {
            case MAP_PROVIDER.openStreetMap:
              setupMapOpenStreetMaps(function () {
                return registerMapBase(_MapOpenStreetMap.default, mapConfig);
              });
              break;

            case MAP_PROVIDER.googleMaps:
              setupGoogleMaps(googleMapsAPIKey, function () {
                return registerMapBase(_MapGoogleMaps.default, mapConfig);
              });
              break;

            default:
              throw new Error('mapProvider is not supported!');
          }
        }

        return function () {
          if (componentInstance.current) {
            componentInstance.current.dispose();
          }
        }; // eslint-disable-next-line react-hooks/exhaustive-deps
      }, []);
    };

    exports.useGeolocation = useGeolocation;
    //# sourceMappingURL=useGeolocation.es.js.map
  }
});
//# sourceMappingURL=useGeolocation.es.js.map