// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
/**
 * Helpers for interacting with Deno's permissions system.
 * @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 descriptor1 of descriptors){
        let state = (await Deno.permissions.query(descriptor1)).state;
        if (state === "prompt") {
            state = (await Deno.permissions.request(descriptor1)).state;
        }
        if (state === "granted") {
            result.push(descriptor1);
        }
    }
    return result.length ? result : undefined;
}
export async function grantOrThrow(descriptor, ...descriptors) {
    const denied = [];
    descriptors = Array.isArray(descriptor) ? descriptor : [
        descriptor,
        ...descriptors
    ];
    for (const descriptor1 of descriptors){
        const { state  } = await Deno.permissions.request(descriptor1);
        if (state !== "granted") {
            denied.push(descriptor1);
        }
    }
    if (denied.length) {
        throw new PermissionDenied(`The following permissions have not been granted:\n${getPermissionString(denied)}`);
    }
}
//# sourceMappingURL=data:application/json;base64,[encoded data, 8724 characters]