{"size": 2270, "ext": "js", "max_stars_repo_path": "src/main/assets/js/calculate-length-repayment.js", "max_stars_repo_name": "hmcts/civil-citizen-ui", "max_stars_repo_stars_event_min_datetime": "2022-01-12T10:29:24.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-12T10:29:24.000Z", "max_issues_repo_path": "src/main/assets/js/calculate-length-repayment.js", "max_issues_repo_name": "hmcts/civil-citizen-ui", "max_issues_repo_issues_event_min_datetime": "2021-11-08T01:34:37.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-29T16:46:10.000Z", "max_forks_repo_path": "src/main/assets/js/calculate-length-repayment.js", "max_forks_repo_name": "hmcts/civil-citizen-ui", "max_forks_repo_forks_event_min_datetime": "2022-01-20T10:21:21.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-23T20:18:21.000Z"}
{"max_stars_count": 1, "max_issues_count": 97, "max_forks_count": 2, "avg_line_length": 33.3823529412, "max_line_length": 175, "alphanum_fraction": 0.6568281938}
/* Length of Repayment Calculation

1. Add in the njk file an input field type hidden and class repayment-amount to collect total cliam amount
   <input type="hidden" class="repayment-amount" value="{{ form.totalClaimAmount }}"/>

2. Add following css classes on elements for repayment: instalments, frequency and length

   A. {{ govukInput({
          classes: "repayment-instalments",
          .....
      )}

   B. {{ govukRadios({
          classes: "repayment-frequency",
          ....
      )}

   C. <p class="repayment-length"></p>
*/

(function () {
  const repaymentAmount = document.querySelector('.repayment-amount');
  const repaymentInstalments = document.querySelector('.repayment-instalments');
  const repaymentFrequency = document.querySelector('.repayment-frequency');
  const repaymentLength = document.querySelector('.repayment-length');
  let frequencyVal = null;


  if (repaymentFrequency) {
    repaymentInstalments.addEventListener('keyup', () => {
      getRepaymentSchedule(frequencyVal);
    });

    repaymentFrequency.addEventListener('click', (event) => {
      frequencyVal = event.target.value;
      getRepaymentSchedule(event.target.value);
    });

    const getRepaymentSchedule = (val) => {
      let numberOfInstalments = parseFloat(repaymentInstalments.value) > 0 ? Math.ceil(parseFloat(repaymentAmount.value) / parseFloat(repaymentInstalments.value)) : undefined;

      switch (val) {
        case 'WEEK':
          repaymentLength.innerHTML = numberOfInstalments ? numberOfInstalments + pluralize(numberOfInstalments, 'week') : '-';
          break;
        case 'TWO_WEEKS':
          numberOfInstalments = numberOfInstalments * 2;
          repaymentLength.innerHTML = numberOfInstalments ? numberOfInstalments + pluralize(numberOfInstalments, 'week') : '-';
          break;
        case 'MONTH':
          repaymentLength.innerHTML = numberOfInstalments ? numberOfInstalments + pluralize(numberOfInstalments, 'month') : '-';
          break;
        default:
          return undefined;
      }
    };

    const pluralize = (num, word) => {
      const plural = num < 2 ? '' : 's';
      return ` ${word}${plural}`;
    };

    if (repaymentFrequency) {
      getRepaymentSchedule(repaymentFrequency);
    }
  }
})();
