const typedTextSpan_1 = document.querySelector(".new-typed-text");
const cursorSpan_1 = document.querySelector(".new-cursor");
var data_1 = $('.typing_cursor_data').data('type');
function get_data(){
	var items_1 =[];
	if(data_1 === undefined){
		return items_1.push(['']);
	}else{
		const theString_1 = data_1;
		$.each(theString_1.split(",").slice(0,-1), function(index, item) {
		  items_1.push(item);
		});
	}
	return items_1
}

var textArray_1 =  get_data();

// const textArray = ["hard", "fun", "a journey", "LIFE"];
const typingDelay_1 = 200;
const erasingDelay_1 = 100;
const newTextDelay_1 = 2000; // Delay between current and next text
let textArrayIndex_1 = 0;
let charIndex_1 = 0;

function type() {
  if (charIndex_1 < textArray_1[textArrayIndex_1].length) {
    if(!cursorSpan_1.classList.contains("typing")) cursorSpan_1.classList.add("typing");
    typedTextSpan_1.textContent += textArray_1[textArrayIndex_1].charAt(charIndex_1);
    charIndex_1++;
    setTimeout(type, typingDelay_1);
  } 
  else {
    cursorSpan_1.classList.remove("typing");
  	setTimeout(erase, newTextDelay_1);
  }
}

function erase() {
	if (charIndex_1 > 0) {
    if(!cursorSpan_1.classList.contains("typing")) cursorSpan_1.classList.add("typing");
    typedTextSpan_1.textContent = textArray_1[textArrayIndex_1].substring(0, charIndex_1-1);
    charIndex_1--;
    setTimeout(erase, erasingDelay_1);
  } 
  else {
    cursorSpan_1.classList.remove("typing");
    textArrayIndex_1++;
    if(textArrayIndex_1>=textArray_1.length) textArrayIndex_1=0;
    setTimeout(type, typingDelay_1 + 1100);
  }
}

document.addEventListener("DOMContentLoaded", function() { // On DOM Load initiate the effect
  if(textArray_1.length) setTimeout(type, newTextDelay_1 + 250);
});