text: angular.module("leaflet-directive")
  .factory('leafletMapEvents', function ($rootScope, $q, $log, leafletHelpers, leafletEventsHelpers) {
      var safeApply = leafletHelpers.safeApply,
          isDefined = leafletHelpers.isDefined,
          isObject = leafletHelpers.isObject,
          Helpers = leafletHelpers,
          errorHeader = leafletHelpers.errorHeader,
          fire = leafletEventsHelpers.fire;
  
      var _getAvailableMapEvents = function() {
          return [
              'click',
              'dblclick',
              'mousedown',
              'mouseup',
              'mouseover',
              'mouseout',
              'mousemove',
              'contextmenu',
              'focus',
              'blur',
              'preclick',
              'load',
              'unload',
              'viewreset',
              'movestart',
              'move',
              'moveend',
              'dragstart',
              'drag',
              'dragend',
              'zoomstart',
              'zoomend',
              'zoomlevelschange',
              'resize',
              'autopanstart',
              'layeradd',
              'layerremove',
              'baselayerchange',
              'overlayadd',
              'overlayremove',
              'locationfound',
              'locationerror',
              'popupopen',
              'popupclose',
              'draw:created',
              'draw:edited',
              'draw:deleted',
              'draw:drawstart',
              'draw:drawstop',
              'draw:editstart',
              'draw:editstop',
              'draw:deletestart',
              'draw:deletestop'
          ];
      };
  
      var _genDispatchMapEvent = function(scope, eventName, logic) {
          // (nmccready) We should consider passing mapId as an argument or using it from scope
          return function(e) {
              // Put together broadcast name
              // (nmccready) We should consider passing mapId joining mapId to the broadcastName to keep the event unique. Same should be done for all directives so we know what map it comes from.
              // problem with this is it will cause a minor bump and break backwards compat
              var broadcastName = 'leafletDirectiveMap.' + eventName;
              // Safely broadcast the event
              fire(scope, broadcastName, logic, e, e.target, scope)
          };
      };
  
      var _notifyCenterChangedToBounds = function(scope) {
          scope.$broadcast("boundsChanged");
      };
  
      var _notifyCenterUrlHashChanged = function(scope, map, attrs, search) {
          if (!isDefined(attrs.urlHashCenter)) {
              return;
          }
          var center = map.getCenter();
          var centerUrlHash = (center.lat).toFixed(4) + ":" + (center.lng).toFixed(4) + ":" + map.getZoom();
          if (!isDefined(search.c) || search.c !== centerUrlHash) {
              //$log.debug("notified new center...");
              scope.$emit("centerUrlHash", centerUrlHash);
          }
      };
  
      return {
          getAvailableMapEvents: _getAvailableMapEvents,
          genDispatchMapEvent: _genDispatchMapEvent,
          notifyCenterChangedToBounds: _notifyCenterChangedToBounds,
          notifyCenterUrlHashChanged: _notifyCenterUrlHashChanged
      };
  });
  