'use strict';
var L = require('leaflet');

var MapControl = L.Map.extend({
  includes: L.Mixin.Events,
  options: {
    attribution: '<a href="https://mapzen.com">Mapzen</a> - <a href="https://www.mapzen.com/rights">Attribution</a>, Data ©<a href="https://openstreetmap.org/copyright">OSM</a> contributors',
    debugTangram: false,
    zoomSnap: 0,
    _useTangram: true
  },

  // overriding Leaflet's map initializer
  initialize: function (element, options) {
    var opts = L.extend({}, L.Map.prototype.options, options);
    L.Map.prototype.initialize.call(this, element, opts);

    if (this.options._useTangram) {

      // debugTangram is deprecated; remove in v1.0
      if (this.options.debugTangram) {
        options.tangramOptions = L.extend({}, options.tangramOptions, {debug: true});
      }

      this._tangram = L.Mapzen._tangram(options.tangramOptions);

      this._tangram.addTo(this);

      var self = this;
      self._tangram.on('loaded', function (e) {
        self.fire('tangramloaded', {
          tangramLayer: e.layer,
          tangramVersion: e.version
        });
      });
    }

    this._setupConfig(opts);
    this._setDefaultUIPositions();
    this._addAttribution();
    this._checkConditions(false);
  },

  _setupConfig: function (opts) {
    // If the key is passed through map initialization
    if (opts.apiKey) {
      this.apiKey = opts.apiKey;
      // When there is no L.Mapzen.apiKey yet, map one takes over.
      if (!L.Mapzen.apiKey) L.Mapzen.apiKey = opts.apiKey;
    // If the key is not passed through the map, but when there is a global one.
    } else if (L.Mapzen.apiKey) {
      this.apiKey = L.Mapzen.apiKey;
    }
  },

  _checkConditions: function (force) {
    if (this._isThisIframed()) {
      // do not scroll zoom when it is iframed
      this.scrollWheelZoom.disable();
      this.scrollWheelZoom = false; // This is for Leaflet v1.0

      var anchors = document.querySelectorAll('a');

      for (var i = 0, j = anchors.length; i < j; i++) {
        var el = anchors[i];
        // Only set target when not explicitly specified
        // to avoid overwriting intentional targeting behavior
        // Unless the force parameter is true, then targets of
        // '_blank' are forced to to be '_top'
        if (!el.target || (force === true && el.target === '_blank')) {
          el.target = '_top';
        }
      }
    }
    // do not show zoom control buttons on mobile
    // need to add more check to detect touch device
    if ('ontouchstart' in window) {
      this._disableZoomControl();
    }
  },

  _isThisIframed: function () {
    return (window.self !== window.top);
  },

  _disableZoomControl: function () {
    if (this.options.zoomControl) {
      this.zoomControl._container.hidden = true;
    }
  },

  _setDefaultUIPositions: function () {
    if (this.options.zoomControl) {
      this.zoomControl.setPosition('bottomright');
    }
  },

  _addAttribution: function () {
    // Adding Mapzen attribution to Leaflet
    if (this.attributionControl) {
      this.attributionControl.setPrefix('');
      var tempAttr = this.options.attributionText || this.options.attribution;
      this.attributionControl.addAttribution(tempAttr);
      this.attributionControl.addAttribution('<a href="http://leafletjs.com/">Leaflet</a>');
    }
  }
});

module.exports = MapControl;

module.exports.map = function (element, options) {
  return new MapControl(element, options);
};

@@ -796,24 +796,230 @@
 e%7D);%0A      %7D
+%0A      // As of v1.0, scene will need to be part of tangramOptions%0A      if (this.options.scene) %7B%0A        options.tangramOptions = L.extend(%7B%7D, options.tangramOptions, %7Bscene: this.options.scene%7D);%0A      %7D
 %0A%0A      this

Support (but deprecate) 'this.options.scene' in favor of 'this.options.tangramOptions.scene'