const { OS } = require("../lib/os/index.js")
const { Empty } = require('google-protobuf/google/protobuf/empty_pb')

let M = new OS()

globalThis.__nstd_env__ = {}
async function attachEnv() {
  let list = (await M.Environ(new Empty)).getCList();
  list.forEach((ele) => {
    const p = ele.match(/^(.+?)=(.+)$/)
    if (p && p.length == 3) {
      globalThis.__nstd_env__[p[1]] = p[2]
    } else {
      console.warn(`ignore environment "${ele}"`)
    }
  })
}
attachEnv();

async function getC(p) {
  let v = await p;
  return v.getC()
}

const { MockCache, todo } = require("./internal/util.js")
let cache = new MockCache("os")

module.exports = todo("os", {
  homedir: cache.build("homedir", getC(M.Homedir(new Empty))),
  hostname: cache.build("hostname", getC(M.Hostname(new Empty))),

  //__init__ must be the last entry.
  __init__: cache.wait_all(),
});
