import {
  checkArrayOfNumbers,
  checkNumber,
  funArrDiv,
  funArrSub,
  funMS,
  funRound,
  TMixed,
} from "../common";

export function BRAR(
  CLOSE: number[],
  HIGH: number[],
  LOW: number[],
  OPEN: number[],
  N: number = 26
): { BR: TMixed[]; AR: TMixed[] } {
  // check input
  checkArrayOfNumbers(CLOSE, "CLOSE");
  checkArrayOfNumbers(HIGH, "HIGH");
  checkArrayOfNumbers(LOW, "LOW");
  checkArrayOfNumbers(OPEN, "OPEN");
  checkNumber(N, 1, CLOSE, "N", "CLOSE");

  // calculate BR and AR
  const BR = funArrDiv(
    funMS(
      [
        null,
        ...HIGH.slice(1).map((item, index) => Math.max(0, item - CLOSE[index])),
      ],
      N
    ),
    funMS(
      [
        0,
        ...LOW.slice(1).map((item, index) => Math.max(0, CLOSE[index] - item)),
      ],
      N
    )
  ).map((item) => funRound(item !== null ? 100 * item : null, 3));

  const AR = funArrDiv(
    funMS(funArrSub(HIGH, OPEN), N),
    funMS(funArrSub(OPEN, LOW), N)
  ).map((item) => funRound(item !== null ? 100 * item : null, 3));
  return { BR, AR };
}
