import abilityAccessCtrl from "@ohos:abilityAccessCtrl";
import type { Permissions } from "@ohos:abilityAccessCtrl";
import type common from "@ohos:app.ability.common";
import type { OnSdkEvent } from '../model/SignCallback';
import { PermissionGrantedEvent, PermissionDeniedEvent } from "@normalized:N&&&tencentqianwebview/src/main/ets/model/SdkEvent&1.0.0";
class BjcCov {
    coverage: {
        [key: string]: string | number;
    };
    constructor(covData: object) {
        const gcv = "__coverage__";
        let coverage = globalThis[gcv] || (globalThis[gcv] = {});
        if (!coverage[covData.path] && true) {
            coverage[covData.path] = covData;
        }
        this.coverage = coverage[covData.path];
    }
    instrumentFunction(func: number) {
        this.coverage.functions[func].count++;
        this.coverage.functions[func].regions[0].count++;
    }
    instrumentRegion(func: number, region: number) {
        this.coverage.functions[func].regions[region].count++;
    }
    instrumentReturn(func: number, retIdx: number) {
        this.coverage.functions[func].returnes[retIdx].count++;
    }
    instrumentBranch(func: number, branch: number, trueOrFalse: boolean) {
        if (trueOrFalse) {
            this.coverage.functions[func].branches[branch].trueCount++;
            for (let r of Object.values(this.coverage.functions[func].branches[branch].group)) {
                if (r !== branch) {
                    this.coverage.functions[func].branches[r as number].falseCount++;
                }
            }
        }
        else {
            this.coverage.functions[func].branches[branch].falseCount++;
        }
    }
}
let bjccovms5u83ri = new BjcCov({ version: "bjc v1.0.0", versionCode: 10000, path: "tencentqianwebview/src/main/ets/permission/RuntimePermissionRequester.ets", hash: "8b51d39b95c561c7b23e2ed9257077d5e91b056ba2d8ef12ce1fa3f2f9cc4fb7", lineCnt: 82, count: 0, projectPath: "", functions: { 0: { name: "RuntimePermissionRequester.request", count: 0, regions: { 0: { startLoc: { line: 22, col: 3 }, endLoc: { line: 51, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 27, col: 88 }, endLoc: { line: 29, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 41, col: 9 }, endLoc: { line: 47, col: 6 }, count: 0, ignored: 0 }, 3: { startLoc: { line: 47, col: 7 }, endLoc: { line: 50, col: 6 }, count: 0, ignored: 0 }, 4: { startLoc: { line: 36, col: 7 }, endLoc: { line: 39, col: 6 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 27, col: 9 }, endLoc: { line: 27, col: 86 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 0 }, 1: { name: "anonymous_0", count: 0, regions: { 0: { startLoc: { line: 31, col: 49 }, endLoc: { line: 31, col: 78 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 1 }, 2: { name: "anonymous_1", count: 0, regions: { 0: { startLoc: { line: 32, col: 18 }, endLoc: { line: 39, col: 6 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 33, col: 34 }, endLoc: { line: 35, col: 8 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 33, col: 11 }, endLoc: { line: 33, col: 32 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 1: { startLoc: { line: 36, col: 15 }, endLoc: { line: 38, col: 47 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 2 }, 3: { name: "anonymous_2", count: 0, regions: { 0: { startLoc: { line: 44, col: 65 }, endLoc: { line: 44, col: 87 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 3 }, 4: { name: "RuntimePermissionRequester.mapWebResourcesToPermissions", count: 0, regions: { 0: { startLoc: { line: 65, col: 3 }, endLoc: { line: 77, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 68, col: 5 }, endLoc: { line: 75, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 71, col: 50 }, endLoc: { line: 74, col: 8 }, count: 0, ignored: 0 }, 3: { startLoc: { line: 76, col: 5 }, endLoc: { line: 77, col: 4 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 71, col: 11 }, endLoc: { line: 71, col: 48 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 4 }, 5: { name: "RuntimePermissionRequester.constructor", count: 0, regions: { 0: { startLoc: { line: 80, col: 3 }, endLoc: { line: 80, col: 27 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 5 } }, exeLine: { 0: 1, 1: 2, 2: 3, 3: 4, 4: 13, 5: 22, 6: 23, 7: 24, 8: 25, 9: 26, 10: 27, 11: 28, 12: 31, 13: 32, 14: 33, 15: 34, 16: 36, 17: 37, 18: 38, 19: 41, 20: 42, 21: 43, 22: 44, 23: 45, 24: 46, 25: 47, 26: 48, 27: 49, 28: 54, 29: 55, 30: 56, 31: 65, 32: 66, 33: 67, 34: 68, 35: 69, 36: 70, 37: 71, 38: 72, 39: 73, 40: 76, 41: 80 } });
/**
 * 运行时权限请求器(SDK 内部使用)。
 *
 * 在 Web 触发 `onPermissionRequest`(getUserMedia / 拍照 / 文件选择等)时,
 * 把对应的鸿蒙权限通过 `requestPermissionsFromUser` 弹给用户,
 * 并通过 `PermissionGranted` / `PermissionDenied` 事件回调通知客户。
 */
export class RuntimePermissionRequester {
    /**
     * 请求一组运行时权限。
     *
     * @param context UIAbility/页面 context
     * @param permissions 鸿蒙权限名数组(如 `['ohos.permission.CAMERA']`)
     * @param onEvent 可选的 SDK 事件回调
     * @returns Promise<boolean> true=全部 grant; false=有任意被拒
     */
    public static async request(context: common.UIAbilityContext, permissions: Permissions[], onEvent?: OnSdkEvent): Promise<boolean> {
        bjccovms5u83ri.instrumentFunction(0);
        if (permissions === null || permissions === undefined || permissions.length === 0) {
            bjccovms5u83ri.instrumentBranch(0, 0, true);
            bjccovms5u83ri.instrumentRegion(0, 1);
            return true;
        }
        else {
            bjccovms5u83ri.instrumentBranch(0, 0, false);
        }
        const permNames: string[] = permissions.map((p: Permissions) => { bjccovms5u83ri.instrumentFunction(1); return String(p); });
        const emit = (granted: boolean): void => {
            bjccovms5u83ri.instrumentFunction(2);
            if (onEvent === undefined) {
                bjccovms5u83ri.instrumentBranch(2, 0, true);
                bjccovms5u83ri.instrumentRegion(2, 1);
                return;
            }
            else {
                bjccovms5u83ri.instrumentBranch(2, 0, false);
            }
            bjccovms5u83ri.instrumentRegion(0, 4);
            onEvent(granted
                ? (bjccovms5u83ri.instrumentBranch(2, 1, true), new PermissionGrantedEvent(permNames)) : (bjccovms5u83ri.instrumentBranch(2, 1, false), new PermissionDeniedEvent(permNames)));
        };
        try {
            bjccovms5u83ri.instrumentRegion(0, 2);
            const atManager = abilityAccessCtrl.createAtManager();
            const grantStatus = await atManager.requestPermissionsFromUser(context, permissions);
            const allGranted: boolean = grantStatus.authResults.every((r: number) => { bjccovms5u83ri.instrumentFunction(3); return r === 0; });
            emit(allGranted);
            return allGranted;
        }
        catch (_e) {
            bjccovms5u83ri.instrumentRegion(0, 3);
            emit(false);
            return false;
        }
    }
    /** Web 资源标识 → 鸿蒙权限的映射表。 */
    private static readonly WEB_RESOURCE_TO_PERMISSION: Map<string, Permissions> = new Map<string, Permissions>([
        ['TYPE_VIDEO_CAPTURE', 'ohos.permission.CAMERA'],
        ['TYPE_AUDIO_CAPTURE', 'ohos.permission.MICROPHONE']
    ]);
    /**
     * 把 Web 资源标识(如 `TYPE_VIDEO_CAPTURE` / `TYPE_AUDIO_CAPTURE`)映射到鸿蒙权限名。
     *
     * @param webResources Web 端请求的资源类型字符串数组
     * @returns 对应的鸿蒙权限数组(去重)
     */
    public static mapWebResourcesToPermissions(webResources: string[]): Permissions[] {
        bjccovms5u83ri.instrumentFunction(4);
        const result: Permissions[] = [];
        const seen: Set<string> = new Set<string>();
        for (const resource of webResources) {
            bjccovms5u83ri.instrumentRegion(4, 1);
            const perm: Permissions | undefined = RuntimePermissionRequester.WEB_RESOURCE_TO_PERMISSION.get(resource);
            if (perm !== undefined && !seen.has(perm)) {
                bjccovms5u83ri.instrumentBranch(4, 0, true);
                bjccovms5u83ri.instrumentRegion(4, 2);
                seen.add(perm);
                result.push(perm);
            }
            else {
                bjccovms5u83ri.instrumentBranch(4, 0, false);
            }
        }
        bjccovms5u83ri.instrumentRegion(4, 3);
        return result;
    }
    // ArkTS 类禁止实例化
    private constructor() { bjccovms5u83ri.instrumentFunction(5); }
}
