'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isValid = isValid;
exports.getEndValueByShortestPath = getEndValueByShortestPath;
exports.extractViewState = extractViewState;

var _mathUtils = require('../utils/math-utils');

var VIEWPORT_PROPS = ['width', 'height', 'longitude', 'latitude', 'zoom', 'bearing', 'pitch', 'position'];

var WRAPPED_ANGULAR_PROPS = {
  longitude: 1,
  bearing: 1
};

function isValid(prop) {
  return Number.isFinite(prop) || Array.isArray(prop);
}

function isWrappedAngularProp(propName) {
  return WRAPPED_ANGULAR_PROPS[propName];
}

function getEndValueByShortestPath(propName, startValue, endValue) {
  if (isWrappedAngularProp(propName) && Math.abs(endValue - startValue) > 180) {
    endValue = endValue < 0 ? endValue + 360 : endValue - 360;
  }
  return endValue;
}

// TODO/xiaoji: This should be merged with the controller's prop constraint system
function extractViewState(props) {
  var viewport = {};
  VIEWPORT_PROPS.forEach(function (key) {
    var value = props[key];
    if (isValid(value)) {
      viewport[key] = value;
      // Normalize longitude and bearing into [-180, 180) range
      // This gurantees the props are in same range when they are interpolated.
      if (isWrappedAngularProp(key)) {
        viewport[key] = (0, _mathUtils.mod)(value + 180, 360) - 180;
      }
    }
  });
  return viewport;
}
//# sourceMappingURL=transition-utils.js.map