import type { NumberArray } from 'cheminfo-types';

import { xCheck } from './xCheck.ts';
import type { XGetFromToIndexOptions } from './xGetFromToIndex.ts';
import { xGetFromToIndex } from './xGetFromToIndex.ts';

/**
 * Calculate the sum of the values
 * @param array - object that contains property x (an ordered increasing array) and y (an array).
 * @param options - options.
 * @returns sum value on the specified range.
 */
export function xSum(
  array: NumberArray,
  options: XGetFromToIndexOptions = {},
): number {
  xCheck(array);
  const { fromIndex, toIndex } = xGetFromToIndex(array, options);

  let sumValue = array[fromIndex];
  for (let i = fromIndex + 1; i <= toIndex; i++) {
    sumValue += array[i];
  }
  return sumValue;
}
