const basePath = '/resources/landingpages'; // Adjust the base path accordingly

// Replace the existing index object with this structure
const index = {};

function crawlFolder(folderPath, productIdentifier) {
    // console.log('Crawling folder.');
    const files = fs.readdirSync(folderPath);

    files.forEach(file => {
        const filePath = path.join(folderPath, file);
        const stats = fs.statSync(filePath);

        if (stats.isDirectory()) {
            crawlFolder(filePath, productIdentifier);
        } else if (stats.isFile() && file.endsWith('.html')) {
            const fileContent = fs.readFileSync(filePath, 'utf-8');
            console.log('File Content:', fileContent);

            
            console.log('File Path:', filePath);
            console.log('File Content:', fileContent);

            const text = extractTextFromHTML(fileContent);
            const relativePath = path.relative(basePath, filePath);

            // Check if the product identifier exists in the index
            if (!index[productIdentifier]) {
                index[productIdentifier] = [];
            }

            index[productIdentifier].push({
                path: relativePath,
                text: text
            });
        }
    });
}


function extractTextFromHTML(htmlContent) {
    const parser = new DOMParser();
    const doc = parser.parseFromString(htmlContent, 'text/html');
    return doc.body.textContent;
}

function getFilesInFolder(folderPath) {
    // Implement logic to get files in the folder (e.g., via AJAX or fetch)
    // Return an array of file names
    return [];
}

// This function should return true if the given path is a directory, and false otherwise.
function isDirectory(filePath) {
    // Implement logic to check if the path is a directory (simplified example)
    // You might need to adjust this based on your project structure or use AJAX/fetch
    return filePath.endsWith('/');
}

// This function should return true if the given path is a file, and false otherwise.
function isFile(filePath) {
    // Implement logic to check if the path is a file (simplified example)
    // You might need to adjust this based on your project structure or use AJAX/fetch
    return !filePath.endsWith('/');
}

// This function should read the content of the file at the given path and return it as a string.
function readFileContent(filePath) {
    // Implement logic to read the file content (simplified example)
    // You might need to adjust this based on how you fetch content (e.g., via AJAX or fetch)
    const content = fetch(filePath).then(response => response.text());
    return content;
}

// Usage example
const products = ['DGS', 'Expere/Authoring', 'Expere/Engine'];

products.forEach(product => {
    const productPath = `${basePath}/${product}`;
    crawlFolder(productPath, product);
});

// Save the index data to separate JSON files for each product
for (const productIdentifier in index) {
    const productIndexPath = `${basePath}/js/${productIdentifier}_searchIndex.json`;
    saveIndexToFile(productIndexPath, index[productIdentifier]);
}

function saveIndexToFile(filePath, data) {
    const jsonData = JSON.stringify(data, null, 2);
    const blob = new Blob([jsonData], { type: 'application/json' });
    const url = URL.createObjectURL(blob);

    // Create a link element to trigger the download
    const a = document.createElement('a');
    a.href = url;
    a.download = filePath;

    // Append the link to the document and trigger a click event
    document.body.appendChild(a);
    a.click();

    // Remove the link from the document
    document.body.removeChild(a);

    // Revoke the Object URL to free up resources
    URL.revokeObjectURL(url);
}
