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, 8956 characters]