function replaceElements(max) {
    fetch('https://cdn.vrijwilligerswerk.be/widget/byUuid/cc142318-c71e-4ca8-8827-a25c3147ad71/text')
        .then(response => response.json())
        .then(data => {
            for (let i = 1; i <= max; i++) {
                let element = document.getElementById(`vrijwilligers-text-${i}`);
                if (element) {
                    fetch('https://cdn.vrijwilligerswerk.be/widget/cc142318-c71e-4ca8-8827-a25c3147ad71/text/' + i)
                        .then(response => response.json())
                        .then(text => {
                            renderText(text, element, data);
                        })
                }
            }
            console.log(data);
        })
        .catch(error => {
            console.error(error);
        });
}

function renderText(text, container, config) {

    container.innerHTML = `
            <article>
                ${config.display.showTitle ? `<h1>${text.name}</h1>` : ''}
                ${text.text}
                <p>
                    Bron: <a href="${text.source}">${text.source.replace("https://", "").replace("http://", "")}</a>
                    ${text.updated_at && config.display.showLastModified ? `- Laatst gewijzigd op ${text.updated_at}` : ''}
                </p>
                <article>`;

    forEach(container.querySelectorAll('a'), (element) => {
        element.setAttribute('target', '_blank');
        element.setAttribute('rel', 'noopener noreferrer');
    });
}
function forEach(array, callback) {
    for (var i = 0, len = array.length; i < len; ++i) {
        callback(array[i]);
    }
}
window.onload = function () {
    replaceElements(54);
};