// runtime/fs.el — filesystem operations
// Thin El wrappers over seed primitives; no logic beyond what is needed
// to present a clean API.  The heavy lifting lives in el_runtime.c.

fn fs_read(path: String) -> String {
    return __fs_read(path)
}

fn fs_write(path: String, content: String) -> Bool {
    return __fs_write(path, content)
}

fn fs_exists(path: String) -> Bool {
    return __fs_exists(path)
}

fn fs_mkdir(path: String) -> Bool {
    return __fs_mkdir(path)
}

fn fs_write_bytes(path: String, bytes: String, n: Int) -> Bool {
    return __fs_write_bytes(path, bytes, n)
}

// fs_list — return list of filenames in a directory.
// __fs_list_raw returns a newline-separated string (possibly with a trailing
// newline); callers that need a clean list should filter empty strings.
fn fs_list(path: String) -> [String] {
    let raw: String = __fs_list_raw(path)
    return str_split(raw, "\n")
}

// fs_list_json — return a JSON array of filenames in a directory.
// Empty strings produced by a trailing newline are stripped before encoding.
fn fs_list_json(path: String) -> String {
    let items: [String] = fs_list(path)
    let n: Int = el_list_len(items)
    let clean: [String] = el_list_empty()
    let i: Int = 0
    while i < n {
        let item: String = el_list_get(items, i)
        let trimmed: String = str_trim(item)
        if !str_eq(trimmed, "") {
            let clean = el_list_append(clean, "\"" + trimmed + "\"")
        }
        let i = i + 1
    }
    return json_build_array(clean)
}
