"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.normalizeTransitionSettings = normalizeTransitionSettings;
exports.getSourceBufferAttribute = getSourceBufferAttribute;
exports.getAttributeTypeFromSize = getAttributeTypeFromSize;
exports.getFloat32VertexFormat = getFloat32VertexFormat;
exports.cycleBuffers = cycleBuffers;
exports.getAttributeBufferLength = getAttributeBufferLength;
exports.padBuffer = padBuffer;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _arrayUtils = require("../../utils/array-utils");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var DEFAULT_TRANSITION_SETTINGS = {
  interpolation: {
    duration: 0,
    easing: function easing(t) {
      return t;
    }
  },
  spring: {
    stiffness: 0.05,
    damping: 0.5
  }
};

function normalizeTransitionSettings(userSettings, layerSettings) {
  if (!userSettings) {
    return null;
  }

  if (Number.isFinite(userSettings)) {
    userSettings = {
      type: 'interpolation',
      duration: userSettings
    };
  }

  var type = userSettings.type || 'interpolation';
  return _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, DEFAULT_TRANSITION_SETTINGS[type]), layerSettings), userSettings), {}, {
    type: type
  });
}

function getSourceBufferAttribute(device, attribute) {
  var buffer = attribute.getBuffer();

  if (buffer) {
    return [buffer, {
      divisor: 0,
      size: attribute.size,
      normalized: attribute.settings.normalized
    }];
  }

  return attribute.value;
}

function getAttributeTypeFromSize(size) {
  switch (size) {
    case 1:
      return 'float';

    case 2:
      return 'vec2';

    case 3:
      return 'vec3';

    case 4:
      return 'vec4';

    default:
      throw new Error("No defined attribute type for size \"".concat(size, "\""));
  }
}

function getFloat32VertexFormat(size) {
  switch (size) {
    case 1:
      return 'float32';

    case 2:
      return 'float32x2';

    case 3:
      return 'float32x3';

    case 4:
      return 'float32x4';

    default:
      throw new Error('invalid type size');
  }
}

function cycleBuffers(buffers) {
  buffers.push(buffers.shift());
}

function getAttributeBufferLength(attribute, numInstances) {
  var doublePrecision = attribute.doublePrecision,
      settings = attribute.settings,
      value = attribute.value,
      size = attribute.size;
  var multiplier = doublePrecision && value instanceof Float64Array ? 2 : 1;
  return (settings.noAlloc ? value.length : numInstances * size) * multiplier;
}

function padBuffer(_ref) {
  var buffer = _ref.buffer,
      numInstances = _ref.numInstances,
      attribute = _ref.attribute,
      fromLength = _ref.fromLength,
      fromStartIndices = _ref.fromStartIndices,
      _ref$getData = _ref.getData,
      getData = _ref$getData === void 0 ? function (x) {
    return x;
  } : _ref$getData;
  var precisionMultiplier = attribute.doublePrecision && attribute.value instanceof Float64Array ? 2 : 1;
  var size = attribute.size * precisionMultiplier;
  var byteOffset = attribute.byteOffset;
  var toStartIndices = attribute.startIndices;
  var hasStartIndices = fromStartIndices && toStartIndices;
  var toLength = getAttributeBufferLength(attribute, numInstances);
  var isConstant = attribute.isConstant;

  if (!hasStartIndices && fromLength >= toLength) {
    return buffer;
  }

  var toData = isConstant ? attribute.value : toFloat32Array(attribute.getBuffer().readSyncWebGL2());

  if (attribute.settings.normalized && !isConstant) {
    var getter = getData;

    getData = function getData(value, chunk) {
      return attribute.normalizeConstant(getter(value, chunk));
    };
  }

  var getMissingData = isConstant ? function (i, chunk) {
    return getData(toData, chunk);
  } : function (i, chunk) {
    return getData(toData.subarray(i + byteOffset, i + byteOffset + size), chunk);
  };
  var source = toFloat32Array(buffer.readSyncWebGL2());
  var target = new Float32Array(toLength);
  (0, _arrayUtils.padArray)({
    source: source,
    target: target,
    sourceStartIndices: fromStartIndices,
    targetStartIndices: toStartIndices,
    size: size,
    getData: getMissingData
  });

  if (buffer.byteLength < target.byteLength + byteOffset) {
    buffer = buffer.device.createBuffer({
      byteLength: target.byteLength + byteOffset
    });
  }

  buffer.write(target, byteOffset);
  return buffer;
}

function toFloat32Array(bytes) {
  return new Float32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / Float32Array.BYTES_PER_ELEMENT);
}
//# sourceMappingURL=attribute-transition-utils.js.map