import {
  funMA,
  funRound,
  TMixed,
  checkArrayOfNumbers,
  checkNumber,
} from "../common";

/**
 * - Moving Average (MA)
 * @param {number[]} CLOSE - - closing prices of the stock
 * @param {number} N - - number of periods to calculate the moving average, must be between 2 and the length of the CLOSE array,defaults : 5, 10, 20, 60
 * @returns {{ [key: string]: TMixed[] } } - object with the moving averages for each period
 */
export function MA(
  CLOSE: number[],
  ...N: number[]
): { [key: string]: TMixed[] } {
  // - check input data
  checkArrayOfNumbers(CLOSE, "CLOSE");

  // - set default values for N
  if (N.length === 0) {
    N = [5, 10, 20, 60];
  }

  // - check N values
  N.forEach((item, index) => {
    checkNumber(item, 2, CLOSE, `N[${index}]`, "CLOSE");
  });

  return N.reduce((acc: { [key: string]: TMixed[] }, cur) => {
    acc[`MA${cur}`] = funMA(CLOSE, cur).map((item) => funRound(item, 3));
    return acc;
  }, {});
}
