// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
/**
 * Helpers for interacting with Deno's permissions system.
 *
 * @deprecated Use the
 * {@link https://deno.land/api?s=Deno.Permissions | Deno Permissions API}
 * directly instead. This module will be removed once the Standard Library
 * migrates to {@link https://jsr.io/ | JSR}.
 *
 * @module
 */ const { PermissionDenied } = Deno.errors;
function getPermissionString(descriptors) {
  return descriptors.length ? `  ${descriptors.map((pd)=>{
    switch(pd.name){
      case "read":
      case "write":
        return pd.path ? `--allow-${pd.name}=${pd.path}` : `--allow-${pd.name}`;
      case "net":
        return pd.host ? `--allow-${pd.name}=${pd.host}` : `--allow-${pd.name}`;
      default:
        return `--allow-${pd.name}`;
    }
  }).join("\n  ")}` : "";
}
export async function grant(descriptor, ...descriptors) {
  const result = [];
  descriptors = Array.isArray(descriptor) ? descriptor : [
    descriptor,
    ...descriptors
  ];
  for (const descriptor of descriptors){
    let state = (await Deno.permissions.query(descriptor)).state;
    if (state === "prompt") {
      state = (await Deno.permissions.request(descriptor)).state;
    }
    if (state === "granted") {
      result.push(descriptor);
    }
  }
  return result.length ? result : undefined;
}
export async function grantOrThrow(descriptor, ...descriptors) {
  const denied = [];
  descriptors = Array.isArray(descriptor) ? descriptor : [
    descriptor,
    ...descriptors
  ];
  for (const descriptor of descriptors){
    const { state } = await Deno.permissions.request(descriptor);
    if (state !== "granted") {
      denied.push(descriptor);
    }
  }
  if (denied.length) {
    throw new PermissionDenied(`The following permissions have not been granted:\n${getPermissionString(denied)}`);
  }
}
//# sourceMappingURL=data:application/json;base64,[encoded data, 9924 characters]
// denoCacheMetadata=1212837500159894149,18363477658099596425