<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js" crossorigin="anonymous" type="application/javascript"></script>

<script src="https://cdn.jsdelivr.net/npm/moment@2.29.4/moment.min.js" crossorigin="anonymous" type="application/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/daterangepicker@3.1.0/daterangepicker.min.js" crossorigin="anonymous" type="application/javascript"></script>

<script src="https://cdn.jsdelivr.net/npm/tableexport.jquery.plugin@1.28.0/tableExport.min.js" crossorigin="anonymous" type="application/javascript"></script>

<script src="https://cdn.jsdelivr.net/npm/jquery-resizable-columns@0.2.3/dist/jquery.resizableColumns.min.js" crossorigin="anonymous" type="application/javascript"></script>

<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js" crossorigin="anonymous" type="application/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js" crossorigin="anonymous" type="application/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.22.1/dist/bootstrap-table.min.js" crossorigin="anonymous" type="application/javascript"></script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.22.1/dist/extensions/export/bootstrap-table-export.min.js" crossorigin="anonymous" type="application/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.22.1/dist/extensions/resizable/bootstrap-table-resizable.js" crossorigin="anonymous" type="application/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.22.1/dist/extensions/filter-control/bootstrap-table-filter-control.js" crossorigin="anonymous" type="application/javascript"></script>

<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/highlight.min.js" crossorigin="anonymous" type="application/javascript"></script>

<script>
    async function copyToClipboard(text, button) {
        if (navigator.clipboard === undefined) {
            const input = document.createElement("textarea");
            input.innerHTML = text;
            document.body.appendChild(input);
            input.select();
            document.execCommand("copy");
            document.body.removeChild(input);
        } else {
            await navigator.clipboard.writeText(text);
        }

        button.html("<i class=\"bi bi-clipboard-check\"></i> copied");
        setTimeout(()=> {
            button.html("<i class=\"bi bi-clipboard\"></i> copy");
        }, 2000);
    }

    function extractDataList(data, column) {
        const elements = data.flatMap(row => row[column].split("<br>")).filter(v => v); // remove empty elements from array
        return Array.from(new Set(elements)).sort();
    }

    function extractListProperties(description, property) {
        return Object.values(description.packages)
            .map(pkg => pkg[property])
            .reduce((left, right) => left.concat(right), []);
    }

    function filterContains(text, value) {
        return value.includes(text.toLowerCase().trim());
    }

    function filterDateRange(text, value) {
        const asOfStartOfDay = date => date.setUTCHours(0, 0, 0, 0);

        const [minDate, maxDate] = text.split(" - ");
        const buildDate = asOfStartOfDay(new Date(value));

        return (buildDate >= new Date(minDate)) && (buildDate <= new Date(maxDate));
    }

    function filterList(index, value, field, data) {
        const dataList = extractDataList(data, field);
        // the library removes all symbols from string, so it is just string
        return value.includes(dataList[index].toLowerCase());
    }

    function listToTable(data) {
        return Array.from(new Set(data))
            .sort()
            .map(entry => safe(entry))
            .join("<br>");
    }

    function safe(string) {
        return String(string)
            .replace(/&/g, "&amp;")
            .replace(/</g, "&lt;")
            .replace(/>/g, "&gt;")
            .replace(/"/g, "&quot;");
    }

    // because I'm tired of safe element generation
    function safeLink(url, text, title) {
        const element = document.createElement("a");
        element.href = url;
        element.innerText = text;
        if (title) element.title = title;
        return element;
    }

    Date.prototype.toISOStringShort = function() {
        const pad = number => String(number).padStart(2, "0");
        return `${this.getFullYear()}-${pad(this.getMonth() + 1)}-${pad(this.getDate())} ${pad(this.getHours())}:${pad(this.getMinutes())}:${pad(this.getSeconds())}`;
    }
</script>
