import { padArray } from '../../utils/array-utils';
const DEFAULT_TRANSITION_SETTINGS = {
  interpolation: {
    duration: 0,
    easing: t => t
  },
  spring: {
    stiffness: 0.05,
    damping: 0.5
  }
};
export function normalizeTransitionSettings(userSettings, layerSettings) {
  if (!userSettings) {
    return null;
  }

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

  const type = userSettings.type || 'interpolation';
  return { ...DEFAULT_TRANSITION_SETTINGS[type],
    ...layerSettings,
    ...userSettings,
    type
  };
}
export function getSourceBufferAttribute(device, attribute) {
  const buffer = attribute.getBuffer();

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

  return attribute.value;
}
export 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 "${size}"`);
  }
}
export 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');
  }
}
export function cycleBuffers(buffers) {
  buffers.push(buffers.shift());
}
export function getAttributeBufferLength(attribute, numInstances) {
  const {
    doublePrecision,
    settings,
    value,
    size
  } = attribute;
  const multiplier = doublePrecision && value instanceof Float64Array ? 2 : 1;
  return (settings.noAlloc ? value.length : numInstances * size) * multiplier;
}
export function padBuffer({
  buffer,
  numInstances,
  attribute,
  fromLength,
  fromStartIndices,
  getData = x => x
}) {
  const precisionMultiplier = attribute.doublePrecision && attribute.value instanceof Float64Array ? 2 : 1;
  const size = attribute.size * precisionMultiplier;
  const byteOffset = attribute.byteOffset;
  const toStartIndices = attribute.startIndices;
  const hasStartIndices = fromStartIndices && toStartIndices;
  const toLength = getAttributeBufferLength(attribute, numInstances);
  const isConstant = attribute.isConstant;

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

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

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

    getData = (value, chunk) => attribute.normalizeConstant(getter(value, chunk));
  }

  const getMissingData = isConstant ? (i, chunk) => getData(toData, chunk) : (i, chunk) => getData(toData.subarray(i + byteOffset, i + byteOffset + size), chunk);
  const source = toFloat32Array(buffer.readSyncWebGL2());
  const target = new Float32Array(toLength);
  padArray({
    source,
    target,
    sourceStartIndices: fromStartIndices,
    targetStartIndices: toStartIndices,
    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