const FS = require('fs/promises')
const Path = require('path')

export const sdcard = navigator.getDeviceStorage("sdcard")
export const music = navigator.getDeviceStorage("music")

function handle(sPath) {
    return {
        name: sPath,
        kind: sPath,
        path: sPath
    }
}

export async function Init() {
	// await loadTheme()

}

export async function Entries() {   
    let pool = new Map()

    for(let d of await FS.readdir(window.cwd, { withFileTypes: true })) {
        if(d.isFile()) {
            let h = handle(d.name)
            pool.set(d.name, h)
        }
    }

    return pool
}

export async function Create(sPath) {
    return handle(sPath)
} 

export async function Open(hFile) {
    let b = await FS.readFile(hFile.path)
    return new File([ b ], hFile.name)
}

export async function Write(hFile, sContent) {
    return await FS.writeFile(hFile.path, sContent, { encoding: 'utf-8' })
}