function HostListItem()
{
    let item = $(`
        
            | 
                
                
             | 
            
                
             | 
            
                DCIM_AMG_25TEM2025.png
                 
                
                    (Sizin cihazda)
                
             | 
            
                
             | 
        
    `);
    return {
        download: item.find(".action-download"),
        hide: item.find(".action-hide"),
        remove: item.find(".action-remove"),
        sha1: item.find(".action-sha1"),
        filename: item.find(".action-filename"),
        progress: item.find(".progress"),
        container: item
    }
}
function RemoteListItem()
{
    let item = $(`
        
            | 
                
                
             | 
            
                
             | 
            
                DCIM.mp4
                 
                
                    (Karşı cihazda)
                
             | 
            
                
             | 
        
    `);
    return {
        download: item.find(".action-download"),
        hide: item.find(".action-hide"),
        remove: item.find(".action-remove"),
        sha1: item.find(".action-sha1"),
        filename: item.find(".action-filename"),
        progress: item.find(".progress"),
        container: item
    }
}
function EmptyListItem()
{
    return $(`
        
            | 
                Gönderilecek veya alınacak dosya bulunmuyor
             | 
        
    `)
}
function formatBytes(a,b=2){if(!+a)return"0 Bytes";const c=0>b?0:b,d=Math.floor(Math.log(a)/Math.log(1024));return`${parseFloat((a/Math.pow(1024,d)).toFixed(c))} ${["Bytes","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"][d]}`}
const endpoint = `wss://ws.saqut.com/socket/webserver/${("000000"+(Math.random() * 512 | 0)).slice(-6)}.sock`;
let mwse = new MWSE({
    endpoint,
    autoReconnect: true,
    autoReconnectTimeout: 60_000
});
mwse.scope(Connect);
let room;
let ip;
async function Connect()
{
    await genRoom();
    $(".network-id").text(ip);
}
async function genRoom()
{
    do{
        let vip = ip = [
            (Math.random() * 16 | 0) + 10,
            (Math.random() * 8 | 0),
            (Math.random() * 4 | 0),
            (Math.random() * 254 | 0) + 1
        ].join('.');
        room = mwse.room({
            name: `WEB3-Smash [${vip}] RO CONNECT:2`,
            description: "Web3 Smash saQut Server Application",
            joinType: "free",
            notifyActionJoined: true,
            notifyActionEjected: true,
            ifexistsJoin: false
        });
        try{
            await room.createRoom();
            break;
        }catch{
            continue;
        }
    }while(1);
}
class FileDescriptor {
    static hostfiles = new Map();
    static remotefiles = new Map();
    static hostindex = 0;
    static remoteindex = 0;
    file = null;
    listItem = null;
    constructor(isHost, item,file){
        this.file = file;
        this.listItem = item;
        $("#thelist").append(item.container);
        if(isHost)
        {
            FileDescriptor.hostfiles.set(
                ++FileDescriptor.hostindex,
                this
            );
        }else{
            FileDescriptor.remotefiles.set(
                ++FileDescriptor.remoteindex,
                this
            );
        }
        this.init();
    }
    init(){
        this.listItem.filename.html(
            this.file.name + " " + formatBytes(this.file.size) + ""
        );
    }
};
$("#files").on('change',function(){
    for (const file of this.files) {
        $("#thelist .empty").remove();
        new FileDescriptor(
            true,
            HostListItem(),
            file
        )
    };
    $("#files").val('');
});
$(".joinroom").on("chnage",function(){
    let ip = $("#remoteadress").val();
    if(!/^\d+\.\d+\.\d+$/.test(ip)){
        return alert("  ")
    }
    $("#thelist .empty").remove();
    
    room = mwse.room({
        name: `WEB3-Smash [${vip}] RO CONNECT:2`,
        description: "Web3 Smash saQut Server Application",
        joinType: "free",
        notifyActionJoined: true,
        notifyActionEjected: true,
        ifexistsJoin: false
    });
})