var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

/**
 * Projection utils
 * TODO: move to Viewport class?
 */
import { COORDINATE_SYSTEM } from '../../lib/constants';
import vec4_transformMat4 from 'gl-vec4/transformMat4';
import vec3_sub from 'gl-vec3/subtract';

function lngLatZToWorldPosition(lngLatZ, viewport) {
  var _viewport$projectFlat = viewport.projectFlat(lngLatZ),
      _viewport$projectFlat2 = _slicedToArray(_viewport$projectFlat, 2),
      X = _viewport$projectFlat2[0],
      Y = _viewport$projectFlat2[1];

  var Z = (lngLatZ[2] || 0) * viewport.distanceScales.pixelsPerMeter[2];
  return [X, Y, Z];
}

/**
 * Equivalent to project_position in project.glsl
 * projects a user supplied position to world position in the target coordinates system
 * @param {array} position - [x, y, z]
 * @param {object} params
 * @param {Viewport} params.viewport - the current viewport
 * @param {number} params.coordinateSystem - the coordinate system to project into
 * @param {array} params.coordinateOrigin - the coordinate origin to project into
 * @param {Matrix4} [params.modelMatrix] - the model matrix of the supplied position
 * @param {number} [params.fromCoordinateSystem] - the coordinate system that the
 *   supplied position is in. Default to the same as `coordinateSystem`.
 * @param {array} [params.fromCoordinateOrigin] - the coordinate origin that the
 *   supplied position is in. Default to the same as `coordinateOrigin`.
 */
export function projectPosition(position, _ref) {
  var viewport = _ref.viewport,
      coordinateSystem = _ref.coordinateSystem,
      coordinateOrigin = _ref.coordinateOrigin,
      modelMatrix = _ref.modelMatrix,
      fromCoordinateSystem = _ref.fromCoordinateSystem,
      fromCoordinateOrigin = _ref.fromCoordinateOrigin;

  var _position = _slicedToArray(position, 3),
      x = _position[0],
      y = _position[1],
      z = _position[2];

  var worldPosition = void 0;

  if (modelMatrix) {
    var _vec4_transformMat = vec4_transformMat4([], [x, y, z, 1.0], modelMatrix);

    var _vec4_transformMat2 = _slicedToArray(_vec4_transformMat, 3);

    x = _vec4_transformMat2[0];
    y = _vec4_transformMat2[1];
    z = _vec4_transformMat2[2];
  }
  if (fromCoordinateSystem === undefined) {
    fromCoordinateSystem = coordinateSystem;
  }
  if (fromCoordinateOrigin === undefined) {
    fromCoordinateOrigin = coordinateOrigin;
  }

  // pre-project light coordinates
  switch (fromCoordinateSystem) {
    case COORDINATE_SYSTEM.LNGLAT:
      worldPosition = lngLatZToWorldPosition([x, y, z], viewport);
      break;

    case COORDINATE_SYSTEM.LNGLAT_OFFSETS:
      worldPosition = lngLatZToWorldPosition([x + fromCoordinateOrigin[0], y + fromCoordinateOrigin[1], z + (fromCoordinateOrigin[2] || 0)], viewport);
      break;

    case COORDINATE_SYSTEM.METER_OFFSETS:
      worldPosition = lngLatZToWorldPosition(viewport.addMetersToLngLat(fromCoordinateOrigin, [x, y, z]), viewport);
      break;

    default:
      worldPosition = [x, y, z];
  }

  switch (coordinateSystem) {
    case COORDINATE_SYSTEM.LNGLAT_OFFSETS:
    case COORDINATE_SYSTEM.METER_OFFSETS:
      var originWorld = lngLatZToWorldPosition(coordinateOrigin, viewport);
      vec3_sub(worldPosition, worldPosition, originWorld);
      worldPosition[1] = -worldPosition[1];
      break;

    default:
  }

  return worldPosition;
}
//# sourceMappingURL=project-functions.js.map