'use strict';

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

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

var _index2 = _interopRequireDefault(_index);

var _rotationconstraint = require('../rotationconstraint.js');

var _rotationconstraint2 = _interopRequireDefault(_rotationconstraint);

var _viewhint = require('../viewhint.js');

var _viewhint2 = _interopRequireDefault(_viewhint);

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

var _condition2 = _interopRequireDefault(_condition);

var _functions = require('../functions.js');

var _functions2 = _interopRequireDefault(_functions);

var _interaction = require('../interaction/interaction.js');

var _interaction2 = _interopRequireDefault(_interaction);

var _pointer = require('../interaction/pointer.js');

var _pointer2 = _interopRequireDefault(_pointer);

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

/**
 * @classdesc
 * Allows the user to rotate the map by clicking and dragging on the map,
 * normally combined with an {@link ol.events.condition} that limits
 * it to when the alt and shift keys are held down.
 *
 * This interaction is only supported for mouse devices.
 *
 * @constructor
 * @extends {ol.interaction.Pointer}
 * @param {olx.interaction.DragRotateOptions=} opt_options Options.
 * @api
 */
var _ol_interaction_DragRotate_ = function (opt_options) {

  var options = opt_options ? opt_options : {};

  _pointer2.default.call(this, {
    handleDownEvent: _ol_interaction_DragRotate_.handleDownEvent_,
    handleDragEvent: _ol_interaction_DragRotate_.handleDragEvent_,
    handleUpEvent: _ol_interaction_DragRotate_.handleUpEvent_
  });

  /**
   * @private
   * @type {ol.EventsConditionType}
   */
  this.condition_ = options.condition ? options.condition : _condition2.default.altShiftKeysOnly;

  /**
   * @private
   * @type {number|undefined}
   */
  this.lastAngle_ = undefined;

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

_index2.default.inherits(_ol_interaction_DragRotate_, _pointer2.default);

/**
 * @param {ol.MapBrowserPointerEvent} mapBrowserEvent Event.
 * @this {ol.interaction.DragRotate}
 * @private
 */
_ol_interaction_DragRotate_.handleDragEvent_ = function (mapBrowserEvent) {
  if (!_condition2.default.mouseOnly(mapBrowserEvent)) {
    return;
  }

  var map = mapBrowserEvent.map;
  var view = map.getView();
  if (view.getConstraints().rotation === _rotationconstraint2.default.disable) {
    return;
  }
  var size = map.getSize();
  var offset = mapBrowserEvent.pixel;
  var theta = Math.atan2(size[1] / 2 - offset[1], offset[0] - size[0] / 2);
  if (this.lastAngle_ !== undefined) {
    var delta = theta - this.lastAngle_;
    var rotation = view.getRotation();
    _interaction2.default.rotateWithoutConstraints(view, rotation - delta);
  }
  this.lastAngle_ = theta;
};

/**
 * @param {ol.MapBrowserPointerEvent} mapBrowserEvent Event.
 * @return {boolean} Stop drag sequence?
 * @this {ol.interaction.DragRotate}
 * @private
 */
_ol_interaction_DragRotate_.handleUpEvent_ = function (mapBrowserEvent) {
  if (!_condition2.default.mouseOnly(mapBrowserEvent)) {
    return true;
  }

  var map = mapBrowserEvent.map;
  var view = map.getView();
  view.setHint(_viewhint2.default.INTERACTING, -1);
  var rotation = view.getRotation();
  _interaction2.default.rotate(view, rotation, undefined, this.duration_);
  return false;
};

/**
 * @param {ol.MapBrowserPointerEvent} mapBrowserEvent Event.
 * @return {boolean} Start drag sequence?
 * @this {ol.interaction.DragRotate}
 * @private
 */
_ol_interaction_DragRotate_.handleDownEvent_ = function (mapBrowserEvent) {
  if (!_condition2.default.mouseOnly(mapBrowserEvent)) {
    return false;
  }

  if (_condition2.default.mouseActionButton(mapBrowserEvent) && this.condition_(mapBrowserEvent)) {
    var map = mapBrowserEvent.map;
    map.getView().setHint(_viewhint2.default.INTERACTING, 1);
    this.lastAngle_ = undefined;
    return true;
  } else {
    return false;
  }
};

/**
 * @inheritDoc
 */
_ol_interaction_DragRotate_.prototype.shouldStopEvent = _functions2.default.FALSE;
exports.default = _ol_interaction_DragRotate_;
module.exports = exports['default'];