/**
 * Converts degrees to radians.
 * 
 * @param degrees Number of degrees.
 */
function degreesToRadians(degrees) {
    return degrees * Math.PI / 180;
}

/**
 * Returns the distance between 2 points of coordinates in Google Maps
 * 
 * @see https://stackoverflow.com/a/1502821/4241030
 * @param lat1 Latitude of the point A
 * @param lng1 Longitude of the point A
 * @param lat2 Latitude of the point B
 * @param lng2 Longitude of the point B
 */
function getDistanceBetweenPoints(lat1, lng1, lat2, lng2) {
    // The radius of the planet earth in meters
    let R = 6378137;
    let dLat = degreesToRadians(lat2 - lat1);
    let dLong = degreesToRadians(lng2 - lng1);
    let a = Math.sin(dLat / 2)
        *
        Math.sin(dLat / 2)
        +
        Math.cos(degreesToRadians(lat1))
        *
        Math.cos(degreesToRadians(lat1))
        *
        Math.sin(dLong / 2)
        *
        Math.sin(dLong / 2);

    let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    let distance = R * c;

    return distance;
}
function strtostar(str) {
    let s = "";
    if (typeof str === "number") str.toString();
    if (typeof str != 'string') str = "";
    str.split('').forEach(el => s += "*");
    return s;
}

function mm(params = {}) {
    let m = moment();
    return params.unix ? moment.unix(params.unix).locale('id') : moment().locale('id');
}
function capitalizeFirstLetter(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}
function myMoment(params = {}) {
    if (!params.tz) params.tz = timezone;
    let mm = params.from ? moment(params.from) : moment();
    return mm.locale('id').tz(params.tz || Intl.DateTimeFormat().resolvedOptions().timeZone);
}
function parseURLParams(url) {
    var queryStart = url.indexOf("?") + 1,
        queryEnd = url.indexOf("#") + 1 || url.length + 1,
        query = url.slice(queryStart, queryEnd - 1),
        pairs = query.replace(/\+/g, " ").split("&"),
        parms = {}, i, n, v, nv;

    if (query === url || query === "") return;

    for (i = 0; i < pairs.length; i++) {
        nv = pairs[i].split("=", 2);
        n = decodeURIComponent(nv[0]);
        v = decodeURIComponent(nv[1]);

        if (!parms.hasOwnProperty(n)) parms[n] = [];
        parms[n].push(nv.length === 2 ? v : null);
    }
    return parms;
}

function SwalDelete({ params = {}, confirmed = () => { }, denied = () => { } } = {}) {
    Swal.fire({
        title: params.title || 'Are you sure?',
        text: params.text || "You won't be able to revert this!",
        icon: params.icon || 'warning',
        showCancelButton: params.showCancelButton || true,
        cancelButtonText: params.cancelButtonText || 'Cancel',
        confirmButtonColor: params.confirmButtonColor || '#3085d6',
        cancelButtonColor: params.cancelButtonColor || '#d33',
        confirmButtonText: params.confirmButtonText || 'Yes, delete it!'
    }).then(async (result) => {
        if (result.isConfirmed) {
            confirmed();
        } else {
            denied();
        }
    })
}
function showDismisableLoading(title = "Please Wait..!", text = "") {
    Swal.fire({
        title: title,
        text: text,
        scrollbarPadding: false,
        allowOutsideClick: false,
        allowEscapeKey: false,
        allowEnterKey: false,
        didOpen: () => {
            Swal.showLoading()
        }
    });
}

function bulanIndo(index) {
    let bulan = [
        'Januari',
        'Februari',
        'Maret',
        'April',
        'Mei',
        'Juni',
        'Juli',
        'Agustus',
        'September',
        'Oktober',
        'November',
        'Desember'
    ];
    return bulan[index] || bulan;
}
function makeid(length = 10) {
    var result = '';
    var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() *
            charactersLength));
    }
    return result;
}
function getHari(index) {

    let hari = ['Minggu', 'Senin', 'Selasa', 'Rabu', 'Kamis', 'Jumat', 'Sabtu'];
    return hari[index - 1] || hari;
}
function range(start, end) {
    return Array(end - start + 1).fill().map((_, idx) => start + idx)
}
function parseTimeToString(time) {
    return `${(time.d ? time.d + ' Hari' : '')} ${(time.h ? time.h + ' Jam' : '')} ${(time.m ? time.m + ' Menit' : '')} ${(time.s ? time.s + ' Detik' : '')}`;
}
function secondsToTime(inputSeconds, normal = false) {
    let secondsInAMinute = 60;
    let secondsInAnHour = 60 * secondsInAMinute;
    let secondsInADay = 24 * secondsInAnHour;

    // extract days
    let days = Math.floor(inputSeconds / secondsInADay);

    // extract hours
    let hourSeconds = inputSeconds % secondsInADay;
    let hours = Math.floor(hourSeconds / secondsInAnHour);

    // extract minutes
    let minuteSeconds = hourSeconds % secondsInAnHour;
    let minutes = Math.floor(minuteSeconds / secondsInAMinute);

    // extract the remaining seconds
    let remainingSeconds = minuteSeconds % secondsInAMinute;
    let seconds = Math.ceil(remainingSeconds);

    // return the final array
    obj = {
        "d": days,
        "h": hours,
        "m": minutes,
        "s": normal ? seconds : seconds >= 1 ? seconds - 1 : seconds
    };
    if (obj.s == 0) obj.s = null;
    return obj;
}
function archiveIcon(name) {
    let n = name.split('.');
    switch (n[n.length - 1]) {
        case 'pdf':
            return 'fas fa-file-pdf';
        case 'doc':
            return 'fas fa-file-word';
        case 'docx':
            return 'fas fa-file-word';
        case 'xls':
            return 'fas fa-file-excel';
        case 'xlsx':
            return 'fas fa-file-excel';
        case 'ppt':
            return 'fas fa-file-powerpoint';
        case 'pptx':
            return 'fas fa-file-powerpoint';
        case 'jpg':
            return 'fas fa-file-image';
        case 'jpeg':
            return 'fas fa-file-image';
        case 'png':
            return 'fas fa-file-image';
        case 'gif':
            return 'fas fa-file-image';
        case 'mp4':
            return 'fas fa-file-video';
        case 'mp3':
            return 'fas fa-file-audio';
        case 'zip':
            return 'fas fa-file-archive';
        case 'rar':
            return 'fas fa-file-archive';
        case 'txt':
            return 'fas fa-file-text';
        default:
            return 'fas fa-file';
    }
}
function isJson(str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}
function isObject(obj) {
    return obj === Object(obj);
}
function jenisSoal(id) {
    switch (id) {
        case '1':
            return "Pilihan Ganda";
        case '2':
            return "Esai";
        case '3':
            return "Pilihan Ganda Multi";
        case '4':
            return "Pilihan Ganda Pasangan";
        case '5':
            return "Isian Singkat";
        case '6':
            return "Pilihan Ganda True False";
    }
}
var cssRule =
    "color: red;" +
    "font-size: 50px;" +
    "font-weight: bold;" +
    "text-shadow: 1px 1px 5px rgb(249, 162, 34);" +
    "filter: dropshadow(color=rgb(249, 162, 34), offx=1, offy=1);"
setTimeout(console.log.bind(console, '%cBerhenti!', cssRule), 0);
setTimeout(console.log.bind(console, `%cIni adalah fitur browser yang ditujukan untuk developer. Jika seseorang meminta Anda untuk menyalin-menempel sesuatu di sini untuk mengaktifkan fitur Aplikasi atau "meretas" akun seseorang, ini adalah penipuan dan akan memberikannya akses ke akun Aplikasi Anda.`, "font-size:20px"), 0);
