$(document).ready(async function() {
    await initSubscriptionTable();
    await initFiltersTable();
    await initContentTable();

    $("#subscriptionsTab").click();

    await loadSavedGuilds();
    await loadServerOptions();

    handleDiscordChannelNames();
});

$('#serverTabs [data-bs-toggle="tab"]').on("show.bs.tab", function(event) {
    const activeTab = $(event.target);
    $(".tab-pane-buttons .tab-pane-buttons-item").hide();
    $(`.tab-pane-buttons .tab-pane-buttons-item[data-tab="${activeTab.attr("id")}"]`).show();
});

$(document).on("selectedServerChange", function() {
    $("#subscriptionsTab").click();
});

function formatDate(date) {

    // Array of weekday names
    const weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    
    // Array of month names
    const months = [
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"
    ];
    
    // Get individual components
    let hours = String(date.getHours()).padStart(2, '0');
    let minutes = String(date.getMinutes()).padStart(2, '0');
    let seconds = String(date.getSeconds()).padStart(2, '0');
    let dayOfWeek = weekdays[date.getDay()];
    let dayOfMonth = date.getDate();
    let month = months[date.getMonth()];
    let year = date.getFullYear();
    
    // Format day with ordinal suffix
    let dayOfMonthSuffix;
    if (dayOfMonth % 10 === 1 && dayOfMonth !== 11) {
        dayOfMonthSuffix = dayOfMonth + "st";
    } else if (dayOfMonth % 10 === 2 && dayOfMonth !== 12) {
        dayOfMonthSuffix = dayOfMonth + "nd";
    } else if (dayOfMonth % 10 === 3 && dayOfMonth !== 13) {
        dayOfMonthSuffix = dayOfMonth + "rd";
    } else {
        dayOfMonthSuffix = dayOfMonth + "th";
    }
    
    // `${hours}:${minutes}:${seconds} · `
    return `${dayOfWeek}, ${dayOfMonthSuffix} ${month}, ${year}<br>${hours}:${minutes}:${seconds}`;
}

function genHexString(len) {
    let output = '';
    for (let i = 0; i < len; ++i) {
        output += (Math.floor(Math.random() * 16)).toString(16);
    }
    return output;
}