class CBHeader {
	constructor(header, arrow_down, arrow_up) {
		this.header = header;
		this.logo = this.header.querySelector('a'); //Header part 1
		this.burgerBtn = this.header.querySelector('button'); //Header part 2
		this.mainMenu = this.header.querySelector('ul'); //Header part 3
		this.loginMenu = this.header.querySelector('nav'); //Header part 4
		this.arrow_down = arrow_down;
		this.arrow_up = arrow_up;
	}

	init() {
		window.addEventListener('resize', (e) => {
			this.checkResponsive();
		});
		this.checkResponsive();
		this.toggleMobileMenu();
	}

	checkResponsive() {
		const width = window.innerWidth;
		if (
			width <= 1100 && this.header.classList.contains('mobile') ||
			width > 1100 && !this.header.classList.contains('mobile')
		) {
			return;
		}

		//Remove the header content
		this.header.innerHTML = '';

		//Add the mobile menu and login menu to the mobile wrapper
		if (width <= 1100) {
			this.header.classList.add('mobile');
			let mobileWrapper = document.createElement('section');
			let mobileMenu = this.mainMenu.cloneNode(true);

			//Add arrow icons to the menu items that have children
			mobileMenu.querySelectorAll('li').forEach(el => {
				if (el.querySelector('ul') !== null) {
					el.querySelector('a').insertAdjacentHTML('afterend', `<button onclick="this.parentNode.classList.toggle('active');">${this.arrow_down}</button>`);
				}
			});

			//Add the mobile menu and login menu to the mobile wrapper
			mobileWrapper.append(mobileMenu, this.loginMenu);
			this.header.append(this.logo, this.burgerBtn, mobileWrapper);
			this.toggleMobileSubMenu();
		}

		//Add the desktop menu and login menu to the header
		if (width > 1100) {
			this.header.classList.remove('mobile');
			this.header.append(this.logo, this.mainMenu, this.loginMenu);
		}
	}

	toggleMobileMenu() {
		this.header.querySelector('button').addEventListener('click', (e) => {
			e.preventDefault();
			this.header.classList.toggle('active');

			//Close all submenus when the mobile menu is closed
			if (!this.header.classList.contains('active')) {
				this.header.querySelector('nav').querySelector('button').classList.remove('active');
				this.header.querySelectorAll('section > ul > li').forEach(el => {
					el.classList.remove('active');
				});
			}
		});
	}

	toggleMobileSubMenu() {
		// this.header.querySelectorAll('section > ul > li > a').forEach(el => {
		// 	const parent = el.parentElement;
		// 	if (parent.querySelector('ul'))
		// 		el.addEventListener('click', (e) => {
		// 			if (!parent.classList.contains('active')) {
		// 				e.preventDefault();
		// 				parent.classList.toggle('active');
		// 			}
		// 		});
		// });
	}
}

CBFooter = function () {
	//Handle scroll to anchor links with offset.
	function scrollToAnchor(elName) {
		const targetElement = document.querySelector(`a[name=${elName}]`);
		if (targetElement) {
			const headerOffset = 80;
			const elementPosition = targetElement.getBoundingClientRect().top + window.scrollY;
			const offsetPosition = elementPosition - headerOffset;
			window.scrollTo({
				top: offsetPosition,
				behavior: "smooth"
			});
		}
	}

	//Add event sto links with hash
	document.querySelectorAll('a[href^="#"]').forEach(anchor => {
		anchor.addEventListener("click", function (e) {
			e.preventDefault();
			const elName = this.getAttribute("href").substring(1);
			scrollToAnchor(elName);
		});
	});

	//Scroll to anchor if hash is present in URL
	if (window.location.hash) {
		setTimeout(() => {
			const elName = window.location.hash.substring(1);
			scrollToAnchor(elName);
		}, 100); // Small delay to ensure the page is loaded before scrolling
	}
}

function CBError(errors){
	var _errno=null;
	var _error=null;
	function errno(){return _errno;}
	function error(){return _error;}
	function reset(){_errno=null;error=null;}
	function set(errno,replaceObj,ret){
		_errno=errno;
		_error=errors[errno];
		for(let i in replaceObj){
			_error=_error.replace('{'+i+'}',replaceObj[i]);
		}
		if(typeof(ret)=='undefined'){ret=false;}
		return ret;
	}
	return {
		errno	:errno,
		error	:error,
		reset	:reset,
		set		:set
	}
}


var CBMultipinMap = function (element) {
	let self = this;
	const tbody = element.querySelector('tbody');
	self.tbodyHtml = tbody.cloneNode(true);
	self.tbodyMobileHTML;
	self.table = element;

	self.init = function () {
		self.tbodyMobileHTML = self.getMobileTemplate(tbody);
		self.updateTableContent();
		window.addEventListener('resize', self.updateTableContent);
	}

	self.updateTableContent = function () {
		const isMobile = window.innerWidth <= 1024;
		self.table.innerHTML = '';
		self.table.appendChild(isMobile ? self.tbodyMobileHTML : self.tbodyHtml);
	}
	self.getMobileTemplate = function (tbody) {
		const clonedTbody = tbody.cloneNode(true);
		// 2. Get all <tr> elements in the cloned <tbody>
		const rows = Array.from(clonedTbody.querySelectorAll('tr'));

		// 3. For each row, find cells with rowSpan > 1 and replicate them
		rows.forEach((row, rowIndex) => {
			// We must convert HTMLCollection to an array before looping
			const cells = Array.from(row.cells);

			cells.forEach((cell, cellIndex) => {
				const span = cell.rowSpan;
				if (span > 1) {
					// Replicate this cell in the next (span - 1) rows
					for (let i = 1; i < span; i++) {
						if (!rows[rowIndex + i]) break; // Avoid out-of-bounds
						// Clone the cell
						const clonedCell = cell.cloneNode(true);
						// Reset the rowSpan to 1
						clonedCell.rowSpan = 1;

						// Insert the cloned cell into rowIndex + i, at the same cell index
						rows[rowIndex + i].insertCell(cellIndex);
						rows[rowIndex + i].cells[cellIndex].replaceWith(clonedCell);
					}
					// Finally, set the original cell’s rowSpan to 1
					cell.rowSpan = 1;
				}
			});
		});
		// 4. Return the HTML string of the transformed <tbody>
		return clonedTbody;
	}

	self.init();
}