import $ from 'jquery';
import buildUrl from './build-url';
import formatNumber from './format-number';
import sendGetDaysRequestForTurnOfTheYear from './send-get-days-request-for-turn-of-the-year';

export default function sendGetDaysRequest(urlPrefix, startDate, toDate, dayLength, personId, element) {

  $(element).empty();

  if (startDate !== undefined && toDate !== undefined && startDate !== null && toDate !== null) {

    if (startDate <= toDate) {

      var startDateString = startDate.getFullYear() + '-' + padZeros(startDate.getMonth() + 1) + '-' + padZeros(startDate.getDate());
      var toDateString = toDate.getFullYear() + '-' + padZeros(toDate.getMonth() + 1) + '-' + padZeros(toDate.getDate());

      var requestUrl = urlPrefix + "/workdays";

      var url = buildUrl(requestUrl, startDateString, toDateString, dayLength, personId);

      $.get(url, function (data) {

        var workDays = data.response.workDays;

        var text;

        if(isNaN(workDays)) {
          text = "Ung&uuml;ltiger Zeitraum"
        } else if (workDays == 1) {
          text = formatNumber(workDays) + " Tag";
        } else {
          text = formatNumber(workDays) + " Tage";
        }

        $(element).html(text);

        if (startDate.getFullYear() != toDate.getFullYear()) {
          $(element).append('<span class="days-turn-of-the-year"></span>');
          sendGetDaysRequestForTurnOfTheYear(urlPrefix, startDate, toDate, dayLength, personId, element + ' .days-turn-of-the-year');
        }

      });
    }

  }

}

function padZeros(number){
  return number <10? '0'+ number:''+ number;
}

@@ -1,27 +1,195 @@
-import $ from 'jquery';
+// disabling date-fns#format is ok since we're formatting dates for api requests%0A// eslint-disable-next-line @urlaubsverwaltung/no-date-fns%0Aimport %7B isAfter, format, getYear %7D from 'date-fns'
 %0Aimp
@@ -359,16 +359,54 @@
 e-year';
+%0Aimport %7B getJSON %7D from '../js/fetch'
 %0A%0Aexport
@@ -414,16 +414,22 @@
 default 
+async 
 function
@@ -510,93 +510,114 @@
 ment
+Selector
 ) %7B%0A%0A  
-$(
+const 
 element
-).empty(
+ = document.querySelector(elementSelector
 );%0A
-%0A
   
-if (startDate !== undefined && toDate !== undefined && 
+element.innerHTML = %22%22;%0A%0A  if (!
 star
@@ -626,47 +626,52 @@
 ate 
-!== null 
 && 
+!
 toDate
- !== null) %7B%0A%0A    if 
+) %7B%0A    return;%0A  %7D%0A%0A  if (isAfter
 (sta
@@ -680,19 +680,17 @@
 Date
- %3C=
+,
  toDate)
  %7B%0A%0A
@@ -689,327 +689,195 @@
 ate)
+)
  %7B%0A
-%0A 
     
- var startDateString = startDate.getFullYear() + '-' + padZeros(startDate.getMonth() + 1) + '-' + padZeros(startDate.getDate());%0A      var toDateString = toDate.getFullYear() + '-' + padZeros(toDate.getMonth() + 1) + '-' + padZeros(toDate.getDate()
+return;%0A  %7D%0A%0A  const requestUrl = urlPrefix + %22/workdays%22;%0A%0A  const startDateString = format(startDate, %22YYYY-MM-DD%22);%0A  const toDateString = format(toDate, %22YYYY-MM-DD%22
 );%0A
-%0A
   
-    var requestUrl = urlPrefix + %22/workdays%22;%0A%0A      var
+const
  url
@@ -960,53 +960,48 @@
 %0A%0A  
-    $.get(url, function (data) %7B%0A%0A        var
+const data = await getJSON(url);%0A  const
  wor
@@ -1033,22 +1033,16 @@
 kDays;%0A%0A
-      
   var te
@@ -1046,22 +1046,16 @@
  text;%0A%0A
-      
   if(isN
@@ -1066,30 +1066,24 @@
 orkDays)) %7B%0A
-      
     text = %22
@@ -1108,22 +1108,16 @@
 itraum%22%0A
-      
   %7D else
@@ -1136,20 +1136,15 @@
 s ==
+=
  1) %7B%0A
-      
     
@@ -1183,22 +1183,16 @@
 %22 Tag%22;%0A
-      
   %7D else
@@ -1194,22 +1194,16 @@
  else %7B%0A
-      
     text
@@ -1245,141 +1245,114 @@
 ;%0A  
-      %7D%0A%0A        $(element).html(
+%7D%0A%0A  element.innerHTML = 
 text
-)
 ;%0A%0A  
-      if (startDate.getFullYear() != toDate.getFullYear()) %7B%0A          $(element).append('
+if (getYear(startDate) !== getYear(toDate)) %7B%0A    element.innerHTML += %22
 %3Cspa
@@ -1359,16 +1359,17 @@
 n class=
+%5C
 %22days-tu
@@ -1382,16 +1382,17 @@
 the-year
+%5C
 %22%3E%3C/span
@@ -1396,21 +1396,20 @@
 pan%3E
-')
+%22
 ;%0A    
-     
+await
  sen
@@ -1495,19 +1495,27 @@
 Id, 
+%60$%7B
 element
- + '
+Selector%7D
  .da
@@ -1537,118 +1537,14 @@
 year
-'
+%60
 );%0A  
-      %7D%0A%0A      %7D);%0A    %7D%0A%0A  %7D%0A%0A%7D%0A%0Afunction padZeros(number)%7B%0A  return number %3C10? '0'+ number:''+ number;
+%7D
 %0A%7D%0A

refactor send-get-days-request js