'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _index = require('../index.js');

var _index2 = _interopRequireDefault(_index);

var _easing = require('../easing.js');

var _easing2 = _interopRequireDefault(_easing);

var _condition = require('../events/condition.js');

var _condition2 = _interopRequireDefault(_condition);

var _extent = require('../extent.js');

var _extent2 = _interopRequireDefault(_extent);

var _dragbox = require('../interaction/dragbox.js');

var _dragbox2 = _interopRequireDefault(_dragbox);

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

/**
 * @classdesc
 * Allows the user to zoom the map by clicking and dragging on the map,
 * normally combined with an {@link ol.events.condition} that limits
 * it to when a key, shift by default, is held down.
 *
 * To change the style of the box, use CSS and the `.ol-dragzoom` selector, or
 * your custom one configured with `className`.
 *
 * @constructor
 * @extends {ol.interaction.DragBox}
 * @param {olx.interaction.DragZoomOptions=} opt_options Options.
 * @api
 */
var _ol_interaction_DragZoom_ = function (opt_options) {
  var options = opt_options ? opt_options : {};

  var condition = options.condition ? options.condition : _condition2.default.shiftKeyOnly;

  /**
   * @private
   * @type {number}
   */
  this.duration_ = options.duration !== undefined ? options.duration : 200;

  /**
   * @private
   * @type {boolean}
   */
  this.out_ = options.out !== undefined ? options.out : false;

  _dragbox2.default.call(this, {
    condition: condition,
    className: options.className || 'ol-dragzoom'
  });
};

_index2.default.inherits(_ol_interaction_DragZoom_, _dragbox2.default);

/**
 * @inheritDoc
 */
_ol_interaction_DragZoom_.prototype.onBoxEnd = function () {
  var map = this.getMap();

  var view = /** @type {!ol.View} */map.getView();

  var size = /** @type {!ol.Size} */map.getSize();

  var extent = this.getGeometry().getExtent();

  if (this.out_) {
    var mapExtent = view.calculateExtent(size);
    var boxPixelExtent = _extent2.default.createOrUpdateFromCoordinates([map.getPixelFromCoordinate(_extent2.default.getBottomLeft(extent)), map.getPixelFromCoordinate(_extent2.default.getTopRight(extent))]);
    var factor = view.getResolutionForExtent(boxPixelExtent, size);

    _extent2.default.scaleFromCenter(mapExtent, 1 / factor);
    extent = mapExtent;
  }

  var resolution = view.constrainResolution(view.getResolutionForExtent(extent, size));

  var center = _extent2.default.getCenter(extent);
  center = view.constrainCenter(center);

  view.animate({
    resolution: resolution,
    center: center,
    duration: this.duration_,
    easing: _easing2.default.easeOut
  });
};
exports.default = _ol_interaction_DragZoom_;
module.exports = exports['default'];