console.log('RUNNING LILLADENT TABLE 3');
// The container for the table rows.
const tableList = document.querySelector('.table1_list');
// --- URL Switching Logic ---
// Default URL for the original language (e.g., Hungarian).
let csvUrl =
'https://docs.google.com/spreadsheets/d/1HC5VbYn9dFC76I2AyfjTHpfYliatBUikECur6BtVXYE/export?format=csv';
// URL for the Vietnamese version.
const vnCsvUrl =
'https://docs.google.com/spreadsheets/d/1ul7DGoRqWWHOdqrxsWiLAswTsudF8LtLUqVo6GA-1jg/export?format=csv';
// Check if the current page's path includes '/vn'.
if (window.location.pathname.includes('/vn')) {
csvUrl = vnCsvUrl; // If it does, switch to the Vietnamese sheet.
}
// --- End of URL Switching Logic ---
/**
* Splits the entire CSV text into an array of row strings, handling newlines within quoted fields.
* @param {string} csvText - The entire CSV document as a single string.
* @returns {string[]} An array of strings, where each string is a complete row.
*/
function splitCsvIntoRows(csvText) {
const rows = [];
let currentRow = '';
let inQuotes = false;
for (const char of csvText) {
if (char === '"') {
inQuotes = !inQuotes;
}
if (char === '\n' && !inQuotes) {
if (currentRow) {
rows.push(currentRow);
}
currentRow = '';
} else {
currentRow += char;
}
}
if (currentRow) {
rows.push(currentRow);
}
return rows;
}
/**
* Parses a single, complete row string into its constituent columns.
* @param {string} rowString - A single line from the CSV.
* @returns {string[]} An array of column values.
*/
function parseCsvRow(rowString) {
const result = [];
let current = '';
let inQuotes = false;
for (let i = 0; i < rowString.length; i++) {
const char = rowString[i];
if (char === '"' && (i === 0 || rowString[i - 1] !== '\\')) {
inQuotes = !inQuotes;
} else if (char === ',' && !inQuotes) {
result.push(current);
current = '';
} else {
current += char;
}
}
result.push(current);
return result.map(field => field.trim().replace(/^"|"$/g, ''));
}
/**
* Asynchronously fetches, parses, and displays the pricing data from the selected URL.
*/
async function populateTable() {
if (!tableList) {
console.error('Error: The container element (.table1_list) was not found.');
return;
}
try {
const response = await fetch(csvUrl);
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.statusText}`);
}
const data = await response.text();
const allRows = splitCsvIntoRows(data.trim());
const contentRows = allRows.slice(1); // Remove header row.
let rowsHtml = '';
contentRows.forEach(row => {
if (!row || !row.trim()) return;
const columns = parseCsvRow(row);
if (columns.every(col => col.trim() === '')) {
return;
}
let name = columns[0] || '';
let price = columns[1] || '';
let isTitleRow = false;
if (!price) {
const lastCommaIndex = name.lastIndexOf(',');
if (
lastCommaIndex !== -1 &&
/\d/.test(name.substring(lastCommaIndex + 1))
) {
price = name.substring(lastCommaIndex + 1);
name = name.substring(0, lastCommaIndex);
} else {
isTitleRow = true;
}
}
name = name.trim();
price = price.trim().replace(/,$/, '');
name = name.replace(/\n/g, '
');
if (isTitleRow) {
rowsHtml += `