import modalWorker from "./loadGltf.worker?worker";
import Dexie from 'dexie';
import { useGlobSetting } from '/@/hooks/setting';
const glob = useGlobSetting();


const db:any = new Dexie('DB')
window['CustomDB'] = db


export function initModalWorker () {

  db.version(1).stores({
    modal: "++id, modalName, modalVal",
  })

  const worker = new modalWorker()

  worker.onmessage = async function(e) {
    const { data, message } = e.data;
    if(message === 'end'){
      setTimeout(() => {
        worker.terminate()
      }, 10000)
    }
  }

  glob.modalUrlArr.forEach( async (url) => {    
    const modalName = url.replace(/(.*\/)*([^.]+).*/ig,"$2")
    const modalArr =  await db.modal.where('modalName').equals(modalName).toArray()
    if(modalArr.length < 1) {
      worker.postMessage({message: 'load', data: url})
    }
  });
  
}

initModalWorker()