const {Worker} = require('worker_threads')
const pathModule = require('path')

const worker = new Worker(pathModule.join(GetResourcePath(GetCurrentResourceName()), 'utils', 'wrapper', 'sv_fs_worker.js'))
const signal = new SharedArrayBuffer(4)

function workerCall(op, args) {
	const view = new Int32Array(signal)
	Atomics.store(view, 0, 0)
	worker.postMessage({op, args, signal})
	Atomics.wait(view, 0, 0)
	return Atomics.load(view, 0) === 1
}

exports('SaveResourceFile', (filePath, data) => {
	if (!filePath || !data) return false
	return workerCall('writeFile', {path: filePath, data})
})
