{% include "hexdoc:index.js.jinja" %}

function stupidver_valid(v) {
    let cv = v.split(".");
    if (cv.length == 4) {
        try {
            let a = Number(cv[0]);
            let b = Number(cv[1]);
            let c = Number(cv[2]);
            let d = Number(cv[3]);
        } catch (any) {
            return false;
        }
        return true;
    } else {
        return semver.valid(v) != null;
    }
}

/** and if it errors, fuck thyself. */
function stupidver_rcompare(v1, v2) {
    let s1 = v1.split(".").map(Number);
    let s2 = v2.split(".").map(Number);
    if (s1[0] > s2[0]) {
        return -1;
    } else if (s1[0] < s2[0]) {
        return 1;
    } else if (s1[1] > s2[1]) {
        return -1;
    } else if (s1[1] < s2[1]) {
        return 1;
    } else if (s1[2] > s2[2]) {
        return -1;
    } else if (s1[2] < s2[2]) {
        return 1;
    }
    if (s1.length > s2.length) {
        return -1;
    } else if (s1.length < s2.length) {
        return 1;
    } else if (s1[3] > s2[3]) {
        reutrn -1;
    } else if (s1[3] < s2[3]) {
        return 1;
    }
    return 0;
}

sortSitemapVersions = function(unsortedVersions) {
    let [versions, branches] = partition(unsortedVersions, (v) => stupidver_valid(v));

    // branches ascending, versions descending
    // eg. ["dev", "main"], ["0.10.0", "0.9.0"]
    branches.sort();
    versions.sort(stupidver_rcompare);

    return [branches, versions];
}
