import * as THREE from 'three'
import Dexie from 'dexie';

const db = new Dexie('DB')
db.version(1).stores({
    modal: "++id, modalName, modalVal",
})

self.addEventListener('message', function(e) {
    const { data, message } = e.data;
    if(message == 'end') {
        // self.postMessage({message: 'end', data: null})
        // self.close()
    }else {
        if(data) {
            loadGltf(data)
        }
    }
    
}, false);

function loadGltf(url){
    const loader = new THREE.FileLoader(); 

    loader.setPath( '/3d/glft/' );
    loader.setResponseType( 'arraybuffer' );
    loader.setRequestHeader( {} );
    loader.setWithCredentials( false );
    try {
        loader.load(url, (data)=> {
            const modalName = url.replace(/(.*\/)*([^.]+).*/ig,"$2")
            
            db.modal.add({
                modalName: modalName,
                modalVal: data
            })
            self.postMessage({message: 'end', data: null})
            // self.close()
        })
    } catch (error) {
        console.log(error);
    }
}

