
function end_of_spin_tasks()
{

	const totalAmt = bet.reduce((sum, b) => sum + (parseFloat(b.amt) || 0), 0);

// Check if total is <= 0
if (totalAmt <= 0) {
    system_win_loss = 3;
}

	console.log("WIN LOSS: ",system_win_loss);
	console.log("BET: ",bet);
	console.log("session_user_amount: ",session_user_amount);
	let user_session_profit = document.getElementById('session-profit-box')
		if(user_session_profit)
		{
			user_session_profit.value = session_user_amount;
		}
		session_user_amount_popup = session_user_amount;
		
	oldUserBets_Edge3 = new Map();
	unHighlightAll();
	masterEdgeChart_save.push(JSON.parse(JSON.stringify(masterEdgeChart)));
	masterEdgeChart.forEach((row, index) => {
		row.element_number += 1;
	});

	pushHistory();
	lastBets = JSON.parse(JSON.stringify(bet));
	currentBet = 0;
	document.getElementById('bankSpan').innerText = '' + bankValue.toLocaleString("en-GB") + '';
	document.getElementById('betSpan').innerText = '' + currentBet.toLocaleString("en-GB") + '';
	bet = [];
	numbersBet = [];
	wager = lastWager;
	clearBet();
	removeChips();
	
	document.getElementById('spins').innerText = numbers.length;
	previousBankValue = bankValue;


	tasks();
	spin_sim_zero_high();
	spin_sim_ddd();
session_user_amount_previous = session_user_amount;

if (numbers.length > 0) {
    const lastNumber = numbers[numbers.length - 1];
    if (!spin_profit_uniques_array.includes(lastNumber)) {
        spin_profit_uniques_array.push(lastNumber);
    } else {
        spin_profit_uniques_array = [];
    }

show_bets();

	updateBetProfitPopup();

	console.log("Numbres: ", numbers);

	system_win_loss = 3;

	
/*
	    // Now place the next cycle’s chips (no spin)
	console.log("BEFORE QUICKY ");
    if (window.AutoBet?.placeChipsOnly && !window.__autobetSpinTriggered) {
		console.log("QUICKY ");
      window.AutoBet.advanceThenPlace();
    }
*/

}

if (
  document.getElementById('autoBetPanel') &&
  window.AutoBet?.advanceThenPlace &&
  !window.__autobetSpinTriggered
) {
  window.AutoBet.advanceThenPlace();
}

}

function end_of_undo_tasks()
{

if (spin_profit_uniques_array.length > 0) {
    spin_profit_uniques_array.pop();
}

    randomNumberCount -=1;
    if(randomNumberCount<0) randomNumberCount = 0;
	removeChips();
	oldUserBets_Edge3 = new Map();
	unHighlightAll();

	let lastElement = masterEdgeChart_save.pop(); 
	if(lastElement)
	{
		
		masterEdgeChart = JSON.parse(JSON.stringify(lastElement));
	}

	tasks();
	undo_sim_zero_high();
	undo_sim_ddd();

	show_bets();

	let user_session_profit = document.getElementById('session-profit-box')
		if(user_session_profit)
		{
			user_session_profit.value = session_user_amount;
		}
session_user_amount_popup = session_user_amount;
		updateBetProfitPopup();
}

function tasks()
{
	addAllItemsToAlertWindow();
	biasChartUpdate();
	bouncyUpdate_bounceRow();
	bouncyUpdate();
	console.log("randomNumberCount ",randomNumberCount);
	console.log("numbers.length ",numbers.length);
if (randomNumberCount == numbers.length) 
	{
	  
	    	    checkAndUpdateMisses();
		updateMissedTable();
	}
	
	comboUpdate();
	display_alerts();
	edgeUpdate();
	law3rd();
	numbersUp_Update();
	statsAfterSpin();
	updateBorderVisibility();
	updateBuildMissAllNewNumber();
	updateColdNewNumber_edge_3();
	updateColdNewNumber();
	updateFrequnecyNewNumber();
	updateHistogramChart();
	updateHoverAfterNumberChange();
	updateLinesNewNumber();
	updateProfitChartWidget();

	updateNextBet();
	updateOnceNewNumber();
	updatePatternNewNumber();
	updateSleeperTable();
	updateStats();
	updateTable_Street_LR();
	updateTwoDozenTable();

	addEdgeToBottomSticky();
	misses();
	updateNeighborHeatmap();
}

function unHighlightAll()
{
    let arr = Array.from({ length: 37 }, (_, i) => i);
    arr.forEach(item => {
        let div;
        if (item === 0) {
            div = document.querySelector(`div[data-values="zero_${item}"]`);
        } else {
            div = document.querySelector(`div[data-values="${item}"]`);
        }

        if (div) {
            div.classList.remove('slider-highlights');
        }
    });
}

function show_bets()
{
	updateBetProfitPopup();
	// Display Table
	let chart = document.getElementById('spinProfit-panel');

	if (chart) {
		chart.innerHTML = '';
		//let new_chart = getAllBets();
		//chart.appendChild(new_chart);

		let end_chart = updateResults();
		chart.appendChild(end_chart);


	}

	const container = document.getElementById('heatMap-panel');
	container.replaceChildren(getRouletteHeatmapWidget());

	
}