"use strict";
// Script ini membutuhkan script lain yaitu:
// sweetalert.all.min.js : https://cdn.jsdelivr.net/npm/sweetalert
// jquery-3.7.0.js : https://jquery.com
// Framework SND
// c1.js adalah versi min dari file ini
let Content = (function() {
    // PENTING :
    // untuk menghindari jalannya script yang tidak sesuai,
    // untuk element yang dihasilkan oleh script lain, diharapkan menggunakan element
    // dengan id '__SND__' untuk parent dari element tersebut jika element yang dihasilkan 
    // berada diluar dari content. jika menggunakan parent element diluar element dengan id
    // '__SND__', ada kemungkinan element yang dihasilkan akan terduplikat | dibuat ulang
    // contohnya : library 'daterangepicker' yang membuat element dengan menggunakan 'body'
    // sebagai parent element

    // tambahan di content.js
    // menampilkan loading ketika halaman belum siap 
    var component = {
            // digunakan untuk mencari class yang akan menjadi target halaman, link harus berupa <a>
            // dan harus berupa link halaman tanpa '#' | 'void' | string dari skipUrl
            linkClass: 'side-link',

            // nilai yang akan dikirim ke url halaman dalam bentuk $_POST['a']
            nilaiPost: 'a',

            mouseRequestTime:50000,
            // sebagian dari url yang akan dilewati ketika mempunyai class dari linkClass
            skipUrl: ['proses'],
            // nama fungsi untuk proses ketika content tampil. dalam fungsi, memiliki 1 parameter
            // fungsi ini harus berada dalam template dan hanya akan di jalankan ketika
            // content yang akan ditampilkan sudah ada
            // param 1 : ketika content selesai ditampilkan, dan bernilai true
            contentTampil: 'waitingDone',
            // menentukan berapa lama waktu tunggu untuk permintaan ke server
            requestTimeout: 30000,
            // nama fungsi untuk proses ketika link berdasarkan 'linkClass' di tekan
            // secara bawaan, event dari link tersebut tidak akan bertindak seperti link biasa
            // dalam fungsi linkClicked, memiliki 2 parameter
            // param 1 : event click
            // param 2 : element link yang di tekan
            linkClicked: 'clickingLink',

            // cache | menyimpan sementara hasil dari halaman yang di minta
            // ketika bernilai 'true', saat berpindah kehalaman sebelumnya,
            // akan menggunakan cache | simpanan halaman yang telah diminta sebelumnya
            // ketika bernilai 'false', hasil halaman yang diminta tidak akan tersimpan
            // nilai yang dapat digunakan adalah 'true' | 'false'
            // selain dari nilai diatas, maka akan dianggap 'false'
            cache: null,
            // jika ingin menampilkan loading element ketika proses request halaman,
            // ganti nilai menjadi true. untuk menghilangkan, ganti jadi false
            showLoading: true,
            // loading element ketika halaman sedang dimuat
            loadingId:'preloader',
        },
        a = {
            // requesting
            aa: false, 
            // mouse hovering
            ab: false, 
            // mouse stay
            ac: false, 
            // dom content loaded
            ad: null,
            // requested url
            ae: null, 
            // clicking
            af: false,
            // mouse time
            ag: null,
            // waiting time
            ai: null,
            // waiting content
            aj: null,
            // simpanan fungsi
            ak: null,
            // start navigate
            al: null,
            // waiting script
            am: null,
            // jumlah cek
            an: null,
            // loaded toBody
            ao: null,
            // loading element
            ap: null,
            // load script siap ke
            aq: null
        },
        b = (ba, bb) => {
            if (k['null'] != undefined) { delete k['null']; }
            if (k[ba] !== undefined) {
                document.getElementById('__SND__').innerHTML = k[ba].i;
                a.ad = document.getElementById('__SND__').children;
                g.ga();
                let bc = null;
                l(ba);
                bc = $("#__SND__ ." + component.linkClass);
                if (bc.length > 0) { o(bc); }
                if (bb == true) { window.history.pushState({ a: ba }, '', ba); }
                if (k[ba].t !== undefined) { document.title = k[ba].t; }
                if (a.af == true) { a.af = false; }
                if(typeof window[component.contentTampil] === 'function'){
                    let bm = window[component.contentTampil];
                    bm(true);
                }
                if(typeof a.ak === 'object'){
                    q();
                }
                if(a.ap !== null && a.ap !== undefined && component.showLoading === true){
                    a.ap.style.display = 'none';
                }
            } else {
                if(a.aa === false){
                    setTimeout(()=>{
                        a.aa = true;
                        h(ba);
                    }, 50)  
                }
            }
        },
        c = (ca,cc) => {
            u(cc);
            let cb;
            if(a.al === true){
                if(a.ak == null){
                    a.ak = [];
                }
                a.ak.push(ca);
                q();
            }
            else{
                cb = document.getElementById('__SND__').children;
                ca(cb);
            }
        },
        d = ()=>{ 
            setTimeout(() => {
                if (a.aa == false && a.ab == false && a.af == false && a.ac == false) {
                    a.ae = null;
                    a.ag = null;
                    let da = (typeof component.cache === 'boolean')? component.cache:false;
                    if(da === false){
                        k = {};
                    }
                    if(a.aj !== null){
                        clearTimeout(a.aj);
                        a.aj = null;
                    }
                }
                d();
            }, 5000);
        },
        e = (ea)=>{
            ea.preventDefault();
            let eb = window.location.href,
            ec = (typeof component.cache === 'boolean')? component.cache:true;
            if(ec === true && k[eb] !== undefined){
                b(eb);
            }
            else{
                a.aa = true;
                h(eb, false);
            }
        },
        f = () => {
            setTimeout(() => {
                if (!a.ab && a.ag != null && a.aa == false) {
                    clearTimeout(a.ag);
                    a.af = false;
                }
            }, 250);
            if(a.ab === true){
                a.ab = false;
                a.ac = false;
            }
        },
        g = {
            ga:()=>{
                let ga = document.getElementById('__SND__'),
                gb = ga.querySelectorAll('script');
                if(gb.length > 0){
                    for(let gc =0;gc<gb.length;gc++){
                        let gd = null,
                        ge = null,
                        gf = null,
                        gg = null,
                        gh = null,
                        gi = null;
                        gd = gb[gc].getAttribute('tobody');
                        if(gd !== undefined && gd !== null){
                            gb[gc].removeAttribute('tobody');
                            if(gd !== null && gd.length > 0){
                                if(gd.indexOf('before;') == 0){
                                    g.gb(gb[gc], gd, 7, 'before;');
                                }
                                else if(gd.indexOf('after;') == 0){
                                    g.gb(gb[gc], gd, 6, 'after;');
                                }
                                else if(gd.indexOf('top;') == 0){
                                    g.gb(gb[gc], gd, 4, 'top;');
                                }
                                else if(gd.indexOf('bottom;')==0){
                                    g.gb(gb[gc], gd, 7, 'bottom;');
                                }
                                else if(gd.indexOf('sesudah;')==0){
                                    g.gb(gb[gc], gd, 8, 'sesudah;');
                                }
                                else if(gd.indexOf('sebelum;')==0){
                                    g.gb(gb[gc], gd, 8, 'sebelum;');
                                }
                                else if(gd.indexOf('awal;')==0){
                                    g.gb(gb[gc], gd, 5, 'awal;');
                                }
                                else{
                                    console.error('nilai "tobody" tidak sesuai. "[after;|before;|top;|bottom;|sesudah;|sebelum;|awal;][src;script_src|id_element]"',gb[gc]);
                                }
                            }
                            else if(a.ao[gb[gc].tagName] !== undefined){
                                gh = gb[gc].getAttribute('src');
                                if(gh !== undefined){
                                    gi = gh;
                                    gb[gc].removeAttribute('src');
                                }
                                if(gi !== null && !a.ao[gb[gc].tagName]['src'].includes(gi)){
                                    if(gb[gc].attributes.length > 0){
                                        gf = g.gc(gb[gc].attributes);
                                    }
                                    gg = gb[gc].innerHTML;
                                    ge = Main.buat.script(gg, gi, gf);
                                    v(ga, ge);
                                    a.ao[gb[gc].tagName]['src'].push(gi);
                                    gb[gc].remove();
                                }
                                else if(gb[gc].innerHTML.length > 0 && !a.ao[gb[gc].tagName]['inner'].includes(gb[gc].innerHTML)){
                                    if(gb[gc].attributes.length > 0){
                                        gf = g.gc(gb[gc].attributes);
                                    }
                                    gg = gb[gc].innerHTML;
                                    ge = Main.buat.script(gg, gi, gf);
                                    v(ga, ge);
                                    a.ao[gb[gc].tagName]['inner'].push(gb[gc].innerHTML);
                                    gb[gc].remove();
                                }
                            }
                        }
                        else{
                            gh = gb[gc].getAttribute('src');
                            if(gh !== undefined){
                                gi = gh;
                                gb[gc].removeAttribute('src');
                            }
                            if(gb[gc].attributes.length > 0){
                                gf = g.gc(gb[gc].attributes);
                            }
                            gg = gb[gc].innerHTML;
                            ge = Main.buat.script(gg, gi, gf);
                            gb[gc].parentNode.replaceChild(ge,gb[gc]);
                        }
                    }
                }
                ga = null;
                gb = null;
                g.gd();
            },
            // khusus script
            // element, tobody, index ke, jenis
            gb:(ga, gb, gc, gd)=>{
                let ge = null, 
                gf = null, 
                gg = null, 
                gh = null, 
                gi = null, 
                gj = null,
                gk = null;
                gj = ga.getAttribute('src');
                if(gj !== undefined){
                    ga.removeAttribute('src');
                }
                else{
                    gj = null;
                }
                gh = ga.innerHTML;
                if(a.ao[ga.tagName] == undefined){
                    a.ao[ga.tagName] = [];
                    a.ao[ga.tagName]['src'] = [];
                    a.ao[ga.tagName]['inner'] = [];
                }
                gg = g.gc(ga.attributes);
                gi = Main.buat.script(gh,gj,gg);
                if(gb.indexOf('src;') == gc){
                    ge = gb.split(';');
                    switch(ge[0]){
                        case 'top':
                        case 'before':
                        case 'sebelum':
                            if(gj !== null && gj !== '' && !a.ao[ga.tagName]['src'].includes(gj)){
                                gf = $('script[src="'+ge[2]+'"]');
                                a.ao[ga.tagName]['src'].push(gj);
                                v(gf[0], gi);
                            }
                            if(gh !== null && gh !== '' && !a.ao[ga.tagName]['inner'].includes(gh)){
                                gf = $('script[src="'+ge[2]+'"]');
                                a.ao[ga.tagName]['inner'].push(gh);
                                v(gf[0], gi);
                            }
                            break;
                        case 'bottom':
                        case 'after':
                        case 'sesudah':
                            if(gj !== null && gj !== '' && !a.ao[ga.tagName]['src'].includes(gj)){
                                gf = $('script[src="'+ge[2]+'"]');
                                a.ao[ga.tagName]['src'].push(gj);
                                v(gf[0], gi, true);
                            }
                            if(gh !== null && gh !== '' && !a.ao[ga.tagName]['inner'].includes(gh)){
                                gf = $('script[src="'+ge[2]+'"]');
                                a.ao[ga.tagName]['inner'].push(gh);
                                v(gf[0], gi, true);
                            }
                            break;
                    }                    
                    ga.remove();
                }
                else if(gb.indexOf('#') == gc){
                    ge = gb.split(';');
                    switch(ge[0]){
                        case 'top':
                        case 'before':
                        case 'sebelum':
                            if(gj !== null && gj !== '' && !a.ao[ga.tagName]['src'].includes(gj)){
                                gf = $(ge[1]);
                                a.ao[ga.tagName]['src'].push(gj);
                                v(gf[0], gi);
                            }
                            if(gh !== null && gh !== '' && !a.ao[ga.tagName]['inner'].includes(gh)){
                                gf = $(ge[1]);
                                a.ao[ga.tagName]['inner'].push(gh);
                                v(gf[0], gi);
                            }
                            break;
                        case 'bottom':
                        case 'after':
                        case 'sesudah':
                            if(gj !== null && gj !== '' && !a.ao[ga.tagName]['src'].includes(gj)){
                                gf = $(ge[1]);
                                a.ao[ga.tagName]['src'].push(gj);
                                v(gf[0], gi, true);
                            }
                            if(gh !== null && gh !== '' && !a.ao[ga.tagName]['inner'].includes(gh)){
                                gf = $(ge[1]);
                                a.ao[ga.tagName]['inner'].push(gh);
                                v(gf[0], gi, true);
                            }
                            break;
                    }
                    ga.remove();
                }
                else if(gb.indexOf('awal;')==0){
                    gf = document.querySelector('body');
                    if(gj !== null && gj !== '' && !a.ao[ga.tagName]['src'].includes(gj)){
                        a.ao[ga.tagName]['src'].push(gj);
                        gf.insertBefore(gi, gf.firstChild);
                    }
                    if(gh !== null && gh !== '' && !a.ao[ga.tagName]['inner'].includes(gh)){
                        a.ao[ga.tagName]['inner'].push(gh);
                        gf.insertBefore(gi, gf.firstChild);
                    }
                    ga.remove();
                }
                else{
                    console.error('nilai setelah "'+gd+'" tidak sesuai. "[src;script_src|id_element]"',ga);
                }
                ge = null; 
                gf = null; 
                gg = null; 
                gh = null; 
                gi = null; 
                gj = null;
                gk = null;
            },
            // attr to object
            gc:(ga)=>{
                let gb = {};
                for(let gc = 0; gc < ga.length; gc++){
                    let gd = ga.item(gc);
                    gb[gd.nodeName] = gd.nodeValue;
                }
                return gb;
            },
            gd:()=>{
                let ga = document.getElementById('__SND__'),
                gb = ga.querySelectorAll('link');
                if(gb.length > 0){
                    for(let gc=0;gc<gb.length;gc++){
                        let gd = null,
                        ge = null,
                        gf = null,
                        gg = null,
                        gh = null,
                        gi =null;
                        gd = gb[gc].getAttribute('tobody');
                        if(gd !== undefined && gd !== null){
                            gb[gc].removeAttribute('tobody');
                            if(gd !== null && gd.length > 0){
                                if(gd.indexOf('before;') == 0){
                                    g.ge(gb[gc], gd, 7, 'before;');
                                }
                                else if(gd.indexOf('after;') == 0){
                                    g.ge(gb[gc], gd, 6, 'after;');
                                }
                                else if(gd.indexOf('top;') == 0){
                                    g.ge(gb[gc], gd, 4, 'top;');
                                }
                                else if(gd.indexOf('bottom;')==0){
                                    g.ge(gb[gc], gd, 7, 'bottom;');
                                }
                                else if(gd.indexOf('sesudah;')==0){
                                    g.ge(gb[gc], gd, 8, 'sesudah;');
                                }
                                else if(gd.indexOf('sebelum;')==0){
                                    g.ge(gb[gc], gd, 8, 'sebelum;');
                                }
                                else if(gd.indexOf('awal;')==0){
                                    g.ge(gb[gc], gd, 5, 'awal;');
                                }
                                else{
                                    console.error('nilai "tobody" tidak sesuai. "[after;|before;|top;|bottom;|sesudah;|sebelum;|awal;][href;link_href|id_element]"',gb[gc]);
                                }
                            }
                            else if(a.ao[gb[gc].tagName] !== undefined){
                                gh = gb[gc].getAttribute('href');
                                if(gh !== undefined){
                                    gi = gh;
                                    gb[gc].removeAttribute('href');
                                }
                                if(gi !== null && !a.ao[gb[gc].tagName]['href'].includes(gi)){
                                    if(gb[gc].attributes.length > 0){
                                        gf = g.gc(gb[gc].attributes);
                                    }
                                    gg = gb[gc].innerHTML;
                                    ge = Main.buat.link(gg, gi, gf);
                                    v(ga, ge);
                                    a.ao[gb[gc].tagName]['href'].push(gi);
                                    gb[gc].remove();
                                }
                                else if(gb[gc].innerHTML.length > 0 && !a.ao[gb[gc].tagName]['inner'].includes(gb[gc].innerHTML)){
                                    if(gb[gc].attributes.length > 0){
                                        gf = g.gc(gb[gc].attributes);
                                    }
                                    gg = gb[gc].innerHTML;
                                    ge = Main.buat.link(gg, gi, gf);
                                    v(ga, ge);
                                    a.ao[gb[gc].tagName]['inner'].push(gb[gc].innerHTML);
                                    gb[gc].remove();
                                }
                            }
                        }
                        else{
                            if(gd !== undefined){
                                gb[gc].removeAttribute('tobody');
                            }
                            gh = gb[gc].getAttribute('href');
                            if(gh !== undefined){
                                gi = gh;
                                gb[gc].removeAttribute('href');
                            }
                            if(gb[gc].attributes.length > 0){
                                gf = g.gc(gb[gc].attributes);
                            }
                            gg = gb[gc].innerHTML;
                            ge = Main.buat.link(gg, gi, gf);
                            gb[gc].parentNode.replaceChild(ge,gb[gc]);
                        }
                    }
                }

                ga = null;
                gb = null;
            },
            // khusus link
            // element, tobody, index ke, jenis
            ge:(ga, gb, gc, gd)=>{
                let ge = null, 
                gf = null, 
                gg = null, 
                gh = null, 
                gi = null, 
                gj = null,
                gk = null;
                gj = ga.getAttribute('href');
                if(gj !== undefined){
                    ga.removeAttribute('href');
                }
                else{
                    gj = null;
                }
                gh = ga.innerHTML;
                if(a.ao[ga.tagName] == undefined){
                    a.ao[ga.tagName] = [];
                    a.ao[ga.tagName]['href'] = [];
                    a.ao[ga.tagName]['inner'] = [];
                }
                gg = g.gc(ga.attributes);
                gi = Main.buat.link(gh,gj,gg);
                if(gb.indexOf('href;') == gc){
                    ge = gb.split(';');
                    switch(ge[0]){
                        case 'top':
                        case 'before':
                        case 'sebelum':
                            if(gj !== null && gj !== '' && !a.ao[ga.tagName]['href'].includes(gj)){
                                gf = $('link[href="'+ge[2]+'"]');
                                a.ao[ga.tagName]['href'].push(gj);
                                v(gf[0], gi);
                            }
                            if(gh !== null && gh !== '' && !a.ao[ga.tagName]['inner'].includes(gh)){
                                gf = $('link[href="'+ge[2]+'"]');
                                a.ao[ga.tagName]['inner'].push(gh);
                                v(gf[0], gi);
                            }
                            break;
                        case 'bottom':
                        case 'after':
                        case 'sesudah':
                            if(gj !== null && gj !== '' && !a.ao[ga.tagName]['href'].includes(gj)){
                                gf = $('link[href="'+ge[2]+'"]');
                                a.ao[ga.tagName]['href'].push(gj);
                                v(gf[0], gi, true);
                            }
                            if(gh !== null && gh !== '' && !a.ao[ga.tagName]['inner'].includes(gh)){
                                gf = $('link[href="'+ge[2]+'"]');
                                a.ao[ga.tagName]['inner'].push(gh);
                                v(gf[0], gi, true);
                            }
                            break;
                    }                    
                    ga.remove();
                }
                else if(gb.indexOf('#') == gc){
                    ge = gb.split(';');
                    switch(ge[0]){
                        case 'top':
                        case 'before':
                        case 'sebelum':
                            if(gj !== null && gj !== '' && !a.ao[ga.tagName]['href'].includes(gj)){
                                gf = $(ge[1]);
                                a.ao[ga.tagName]['href'].push(gj);
                                v(gf[0], gi);
                            }
                            if(gh !== null && gh !== '' && !a.ao[ga.tagName]['inner'].includes(gh)){
                                gf = $(ge[1]);
                                a.ao[ga.tagName]['inner'].push(gh);
                                v(gf[0], gi);
                            }
                            break;
                        case 'bottom':
                        case 'after':
                        case 'sesudah':
                            if(gj !== null && gj !== '' && !a.ao[ga.tagName]['href'].includes(gj)){
                                gf = $(ge[1]);
                                a.ao[ga.tagName]['href'].push(gj);
                                v(gf[0], gi, true);
                            }
                            if(gh !== null && gh !== '' && !a.ao[ga.tagName]['inner'].includes(gh)){
                                gf = $(ge[1]);
                                a.ao[ga.tagName]['inner'].push(gh);
                                v(gf[0], gi, true);
                            }
                            break;
                    }
                    ga.remove();
                }
                else if(gb.indexOf('awal;')==0){
                    gf = document.querySelector('body');
                    if(gj !== null && gj !== '' && !a.ao[ga.tagName]['href'].includes(gj)){
                        a.ao[ga.tagName]['href'].push(gj);
                        gf.insertBefore(gi, gf.firstChild);
                    }
                    if(gh !== null && gh !== '' && !a.ao[ga.tagName]['inner'].includes(gh)){
                        a.ao[ga.tagName]['inner'].push(gh);
                        gf.insertBefore(gi, gf.firstChild);
                    }
                    ga.remove();
                }
                else{
                    console.error('nilai setelah "'+gd+'" tidak sesuai. "[href;link_href|id_element]"',ga);
                }
                ge = null; 
                gf = null; 
                gg = null; 
                gh = null; 
                gi = null; 
                gj = null;
                gk = null;
            }
        },
        h = (ha, hb = true) => {
            if(a.al === null){
                a.al = true;
            }
            a.ak = [];
            a.am = [];
            a.aq = 0;
            if(a.aa === true){
                a.ae = ha;
                let hc = {};
                hc['_SND_PAGE_'] = component.nilaiPost;
                $.ajax({
                    url:ha,
                    type:'post',
                    data: hc,
                    timeout: component.requestTimeout,
                    enctype: 'mutipart/form-data',
                    beforeSend:function(){
                        
                    },
                    success: function(hd) {
                        a.aa = false;
                        if(typeof hd == "string"){
                            hd = JSON.parse(hd);
                        }
                        if (typeof hd === "object") {
                            if (hd.h == true) {
                                k[ha] = {};
                                k[ha].i = hd.i;
                                k[ha].t = hd.t;
                                if (a.af == true) { 
                                    b(ha, true);
                                }
                                if(hb === false){
                                    b(ha);
                                }
                            }
                            else if(hd.h == 'p'){
                                window.location.href = hd.i;
                            }
                            else if(hd.h == 'r'){
                                window.location.href = window.location.href;
                            }
                            else{
                                Main.pesan(hd.i, hd.t);
                            }
                        }
                    },
                    error: function(er) {
                        a.aa = false;
                        if(er.status == '0' && k[ha]){
                            b(ha, true);
                        }
                    },
                    complete: function() {
                        a.aa = false;
                    },
                    cache: (typeof component.cache == 'boolean')? component.cache:false
                });
            }
        },
        i = (ia, ib, ic)=>{
            ib.preventDefault();
            let id = true;
            if(typeof window[component.linkClicked] === 'function'){
                let id = window[component.linkClicked],
                ie = id(ib, ic);
                if(ie === false)
                    return false;
            }
            if(id === true){
                if(ia === a.ae){
                    if(a.aa === true){
                        if(a.ap !== null && a.ap !== undefined && component.showLoading){
                            a.ap.style.display = 'block';
                        }
                        p(ia);
                    }
                    else{
                        a.af = true;
                        b(ia, true);
                    }
                }
                else{
                    a.aa = true;
                    a.af = true;
                    if(a.ap !== null && a.ap !== undefined && component.showLoading){
                        a.ap.style.display = 'block';
                    }
                    h(ia);
                }
            }
        },
        j = (ja) => { try { let jb = JSON.parse(ja); if (jb && typeof jb === "object") { return jb; } } catch (jc) {} return false; },
        k = {},
        l = (la)=>{
            let lb = document.querySelectorAll('.'+component.linkClass);
            if(lb.length > 0){
                lb.forEach((lc, ld)=>{
                    let le = lc.getAttribute('href'), lf = lc.getAttribute('atas');
                    if(lf){
                    	let lg = $(lc).parent('.'+lf);
                    	if(lg[0].classList.contains('active') && le != la){
	                        lg[0].classList.remove('active');
	                    }
	                    if(la.includes(le)){
	                        lg[0].classList.add('active');
	                    }
                    }
                    else{
	                    if(lc.classList.contains('active') && le != la){
	                        lc.classList.remove('active');
	                    }
	                    if(la.includes(le)){
	                        lc.classList.add('active');
	                    }
                    }
                });
            }
        },
        m = (ma)=>{
            if(a.ab === false){
                a.ab = true;
                if(a.ag !== null){
                    clearTimeout(a.ag);
                }
                a.ag = setTimeout(()=>{
                    if(a.aa === false && a.af === false && a.ac === false){
                        a.aa = true;
                        a.ac = true;
                        h(ma);
                    }
                    else{
                        clearTimeout(a.ag);
                    }
                }, component.mouseRequestTime);
            }
        },
        n = () => { 
            if (Main.adakah('.'+component.linkClass)) { 
                let na = $('.'+component.linkClass); 
                if (na.length > 0) { 
                    o(na); 
                } 
            }
            if(component.cache === true){
                let nb = $('.side-link.active');
                if(nb.length == 1){
                    let nc = nb.attr('href');
                    if(k[nc] == undefined){
                        let nd = document.getElementById('__SND__'),
                        ne = document.title;
                        k[nc] = {};
                        k[nc].i = nd.innerHTML;
                        k[nc].t = ne;
                    }
                }
            }
        },
        o = (oa) => {
            console.log('linked link');
            oa.each((oc, ob) => {
                let of = ob.getAttribute('href'), 
                og = component.skipUrl.some(oh => of.includes(oh));
                if ( of !== null && ! of .includes('#') && !og && ! of.includes('void')) {
                    ob.addEventListener('click', (oh) => {
                        i(of, oh, ob);
                    });
                    ob.addEventListener('mouseover', (od) => {
                        m(of);
                    });
                    ob.addEventListener('mouseleave', (oe) => { 
                        f();
                    });
                }
            });
        },
        p =(pa)=>{
            if(a.aj !== null){
                clearTimeout(a.aj);
            }
            a.aj = setTimeout(()=>{
                if(a.aa === false){
                    a.af = true;
                    b(pa, true);
                }
                else{
                    p(pa);
                }
            }, 100);
        },
        q=()=>{
            if(typeof a.am === 'object' && a.am !== null){
                if(a.am.length > 0){
                    t(0);
                }
                else{
                    for(;a.aq < a.ak.length;a.aq++){
                        let qc = a.ak[a.aq];
                        qc(a.ad);
                    }
                }
            }
            else{
                setTimeout(()=>{
                    q();
                },200);
            }
        },
        r=(ra, rb)=>{
            switch(rb){
                case 'a':
                    throw Error('parameter Content.siap harus berupa fungsi. tipe \''+(typeof ra)+'\' yang dimasukan');
                    break;
                case 'b':
                    throw Error('parameter Content.tungguAda harus berupa array | object | string. null yang dimasukan');
                    break;
                case 'c':
                    throw Error('nilai Content.tungguAda harus berupa array | object | string. null yang dimasukan');
                    break;
                case 'd':
                    console.error(ra, ' yang ditunggu, tidak ada');
                    break;
                case 'e':
                    console.error(ra, ' tidak diperbolehkan dalam tungguAda');
                    break;
            }
        },
        s = (elm = '__SND__', pop = true)=>{
            let sb = $('*[tobody]'), 
            se = null,
            sf = null,
            sg = null,
            sh = null,
            si = null,
            sj = document.getElementById(elm),
            sk = null;
            if(a.ao == null){
                a.ao = [];
            }
            if(a.ap === null && component.showLoading){
                a.ap = document.getElementById(component.loadingId);
            }
            sb.each((sc, sd)=>{
                se = sd.getAttribute('tobody');
                sd.removeAttribute('tobody');
                if(a.ao[sd.tagName] === undefined){
                    a.ao[sd.tagName] = [];
                    if(sd.tagName == 'SCRIPT'){
                        a.ao[sd.tagName]['src'] = [];
                    }
                    else if(sd.tagName == 'LINK'){
                        a.ao[sd.tagName]['href'] = [];
                    }
                    a.ao[sd.tagName]['inner'] = [];
                }
                if(se !== null && sd.tagName == 'SCRIPT'){
                    if(se.indexOf('before;') == 0){
                        g.gb(sd, se, 7, 'before;');
                    }
                    else if(se.indexOf('after;') == 0){
                        g.gb(sd, se, 6, 'after;');
                    }
                    else if(se.indexOf('top;') == 0){
                        g.gb(sd, se, 4, 'top;');
                    }
                    else if(se.indexOf('bottom;')==0){
                        g.gb(sd, se, 7, 'bottom;');
                    }
                    else if(se.indexOf('sesudah;')==0){
                        g.gb(sd, se, 8, 'sesudah;');
                    }
                    else if(se.indexOf('sebelum;')==0){
                        g.gb(sd, se, 8, 'sebelum;');
                    }
                    else if(se.indexOf('awal;')==0){
                        g.gb(sd, se, 5, 'awal;');
                    }
                    else{
                        sf = sd.getAttribute('src');
                        sk = false;
                        if(sf.length > 0 && !a.ao[sd.tagName]['src'].includes(sf)){
                            sk = true;
                        }
                        else if(sd.innerHTML.length > 0 && !a.ao[sd.tagName]['inner'].includes(sd.innerHTML)){
                            sk = false;
                        }
                        if(sd.attributes.length > 0){
                            sg = g.gc(sd.attributes);
                        }
                        sh = sd.innerHTML;
                        si = Main.buat.script(sh, sf, sg);
                        v(sj, si);
                        if(sk === true){
                            a.ao[sd.tagName]['src'].push(sf);
                        }
                        else{
                            a.ao[sd.tagName]['inner'].push(sh);
                        }
                        sd.remove();
                    }
                }
                else if(se !== null && sd.tagName == 'LINK'){
                    if(se.indexOf('before;') == 0){
                        g.ge(sd, se, 7, 'before;');
                    }
                    else if(se.indexOf('after;') == 0){
                        g.ge(sd, se, 6, 'after;');
                    }
                    else if(se.indexOf('top;') == 0){
                        g.ge(sd, se, 4, 'top;');
                    }
                    else if(se.indexOf('bottom;')==0){
                        g.ge(sd, se, 7, 'bottom;');
                    }
                    else if(se.indexOf('sesudah;')==0){
                        g.ge(sd, se, 8, 'sesudah;');
                    }
                    else if(se.indexOf('sebelum;')==0){
                        g.ge(sd, se, 8, 'sebelum;');
                    }
                    else if(se.indexOf('awal;')==0){
                        g.ge(sd, se, 5, 'awal;');
                    }
                    else{
                        sf = sd.getAttribute('href');
                        sk = false;
                        if(sf.length > 0 && !a.ao[sd.tagName]['href'].includes(sf)){
                            sk = true;
                        }
                        else if(sd.innerHTML.length > 0 && !a.ao[sd.tagName]['inner'].includes(sd.innerHTML)){
                            sk = false;
                        }
                        if(sd.attributes.length > 0){
                            sg = g.gc(sd.attributes);
                        }
                        sh = sd.innerHTML;
                        si = Main.buat.link(sh, sf, sg);
                        v(sj, si);
                        if(sk === true){
                            a.ao[sd.tagName]['href'].push(sf);
                        }
                        else{
                            a.ao[sd.tagName]['inner'].push(sh);
                        }
                        sd.remove();
                    }
                }
            });
            sb = null;
            se = null;
            sf = null;
            sg = null;
            sh = null;
            si = null;
            sj = null;
            sk = null;
            if(pop === true){
                window.addEventListener("popstate", (sa) => {
                    e(sa);
                });
            }
        },
        t=(ta)=>{
            setTimeout(()=>{
                if(ta < a.am.length){
                    if(a.an > 0){
                        let tb = (typeof window[a.am[ta]] === 'function' || typeof jQuery.fn[a.am[ta]] === 'function')? true:false;
                        if(tb === false){
                            a.an--;
                            t(ta);
                        }
                        else{
                            a.an = 100;
                            t(ta+1);
                        }
                    }
                    else{
                        a.an = 100;
                        r(a.am[ta], 'd');
                        t(ta+1);
                    }
                }
                else{
                    a.am = [];
                    a.an = 100;
                    q();
                }
            }, 100);
        },
        u=(ua)=>{
            if(a.an === null){
                a.an = 100;
            }
            if((typeof ua === 'object' || typeof ua === 'string') && ua.length > 0){
                switch(typeof ua){
                    case 'object':
                        ua.forEach((ub, uc)=>{
                            if(ub !== null){
                                if(typeof ub == 'object' || typeof ub == 'array'){
                                    u(ub);
                                }
                                else if(typeof ub == 'string'){
                                    if(a.am !== null && !a.am.includes(ub)){
                                        a.am.push(ub);
                                    }
                                }
                                else{
                                    r(ub, 'e');
                                }
                            }
                            else{
                                r(ub, 'c');
                            }
                        });
                        break;
                    case 'string':
                        if(!a.am.includes(ua)){
                            a.am.push(ua);
                        }
                        break
                    default:
                        r(ua, 'b');
                        break;
                }
            }
        },
        // va: element tujuan
        // vb: element yang dimasukan
        // vc: 'true':after | 'false':before
        v=(va, vb, vc=false)=>{
            if(vc === true){
                va.parentNode.insertBefore(vb, va.nextSibling);
            }
            else{
                va.parentNode.insertBefore(vb, va);
            }
        },
        // wa : string 1
        // wb : string 2
        // wk : toleransi, semakin besar toleransi, semakin kurang kesamaannya
        w=(wa, wb, wk)=>{
            let wc, wd, we;
            wc = wa.length;
            wd = wb.length;
            we = Array(wc + 1).fill(null).map(()=>Array(wd+1).fill(0));
            for(let wf= 0; wf<=wc;wf++){
                we[wf][0]=wf;
            }
            for(let wg=0;wg<=wd;wg++){
                we[0][wg]=wg;
            }
            for(let wh=0;wh<=wc;wh++){
                for(let wi=0;wi<=wd;wi++){
                    let wj =wa[wh-1]===wd[wi-1]?0:1;
                    we[wh][wi] = Math.min(we[wh-1][wi]+1,we[wh][wi-1]+1,we[wh-1][wi-1]+wj);
                }
            }
            return we[wc][wd] < wk;
        },
        // mengambil string yang sama dari belakang
        x=(xa, xb)=>{
            let xc = '';
            for(let xd=Math.min(xa.length,xb.length); xd>0;xd--){
                if(xa[xd]===xb[xd]){
                    xc = xa[xd]+xc;
                }
            }
        };
    return { 
        init: () => { s(), n(), d() }, 
        siap:(sa, sb)=>{
            (typeof sa === 'function')? (c(sa, sb)):r(sa, 'a')
        },
        linked:(aa)=>{
            // jquery selector elements
            o(aa);
        },
        tungguAda:(ta)=>{u(ta)},
        linkClass:()=>{
            return component.linkClass;
        },
        load(aa, ab){
            // link, isi content
            k[aa] = {};
            k[aa].i = ab.i;
            k[aa].t = ab.t;
            b(aa, ab);
        }
}
})();
window.Content = Content;
if(window.siap === undefined){
    window.siap = Content.siap;
}
if(window.tungguAda === undefined){
    window.tungguAda = Content.tungguAda;
}
$(() => { Content.init(); });