import type common from "@ohos:app.ability.common";
import type Want from "@ohos:app.ability.Want";
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 bjccovms5u83s5 = new BjcCov({ version: "bjc v1.0.0", versionCode: 10000, path: "tencentqianwebview/src/main/ets/scheme/ExternalSchemeHandler.ets", hash: "9ae21e285a3c7714a91f0e70c536f45ebf7554c00005fa0944413b008fb09146", lineCnt: 67, count: 0, projectPath: "", functions: { 0: { name: "ExternalSchemeHandler.schemeOf", count: 0, regions: { 0: { startLoc: { line: 15, col: 3 }, endLoc: { line: 24, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 16, col: 64 }, endLoc: { line: 18, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 20, col: 19 }, endLoc: { line: 22, col: 6 }, count: 0, ignored: 0 }, 3: { startLoc: { line: 23, col: 5 }, endLoc: { line: 24, col: 4 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 16, col: 9 }, endLoc: { line: 16, col: 62 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 1: { startLoc: { line: 20, col: 9 }, endLoc: { line: 20, col: 17 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 0 }, 1: { name: "ExternalSchemeHandler.isWhitelisted", count: 0, regions: { 0: { startLoc: { line: 29, col: 3 }, endLoc: { line: 38, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 30, col: 82 }, endLoc: { line: 32, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 34, col: 30 }, endLoc: { line: 36, col: 6 }, count: 0, ignored: 0 }, 3: { startLoc: { line: 37, col: 5 }, endLoc: { line: 38, col: 4 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 30, col: 9 }, endLoc: { line: 30, col: 80 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 1: { startLoc: { line: 34, col: 9 }, endLoc: { line: 34, col: 28 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 1 }, 2: { name: "anonymous_0", count: 0, regions: { 0: { startLoc: { line: 37, col: 27 }, endLoc: { line: 37, col: 68 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 2 }, 3: { name: "ExternalSchemeHandler.open", count: 0, regions: { 0: { startLoc: { line: 45, col: 3 }, endLoc: { line: 62, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 49, col: 52 }, endLoc: { line: 51, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 52, col: 9 }, endLoc: { line: 59, col: 6 }, count: 0, ignored: 0 }, 3: { startLoc: { line: 59, col: 7 }, endLoc: { line: 61, col: 6 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 49, col: 9 }, endLoc: { line: 49, col: 50 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 3 }, 4: { name: "ExternalSchemeHandler.constructor", count: 0, regions: { 0: { startLoc: { line: 65, col: 3 }, endLoc: { line: 65, col: 27 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 4 } }, exeLine: { 0: 1, 1: 2, 2: 9, 3: 15, 4: 16, 5: 17, 6: 19, 7: 20, 8: 21, 9: 23, 10: 29, 11: 30, 12: 31, 13: 33, 14: 34, 15: 35, 16: 37, 17: 45, 18: 46, 19: 47, 20: 48, 21: 49, 22: 50, 23: 52, 24: 53, 25: 54, 26: 55, 27: 57, 28: 58, 29: 59, 30: 60, 31: 65 } });
/**
 * 处理外部 App scheme 跳转。
 *
 * 命中白名单 scheme（如 `weixin://`）时通过 `context.startAbility(Want)` 拉起目标 App。
 */
export class ExternalSchemeHandler {
    /**
     * 提取 URL 的 scheme(小写)。无法识别返回 ''。
     *
     * @example schemeOf('weixin://pay/abc') → 'weixin'
     */
    public static schemeOf(url: string): string {
        bjccovms5u83s5.instrumentFunction(0);
        if (url === null || url === undefined || url.length === 0) {
            bjccovms5u83s5.instrumentBranch(0, 0, true);
            bjccovms5u83s5.instrumentRegion(0, 1);
            return '';
        }
        else {
            bjccovms5u83s5.instrumentBranch(0, 0, false);
        }
        const idx: number = url.indexOf(':');
        if (idx <= 0) {
            bjccovms5u83s5.instrumentBranch(0, 1, true);
            bjccovms5u83s5.instrumentRegion(0, 2);
            return '';
        }
        else {
            bjccovms5u83s5.instrumentBranch(0, 1, false);
        }
        bjccovms5u83s5.instrumentRegion(0, 3);
        return url.substring(0, idx).toLowerCase();
    }
    /**
     * 判断 url 的 scheme 是否在白名单内。
     */
    public static isWhitelisted(url: string, whitelist: string[]): boolean {
        bjccovms5u83s5.instrumentFunction(1);
        if (whitelist === null || whitelist === undefined || whitelist.length === 0) {
            bjccovms5u83s5.instrumentBranch(1, 0, true);
            bjccovms5u83s5.instrumentRegion(1, 1);
            return false;
        }
        else {
            bjccovms5u83s5.instrumentBranch(1, 0, false);
        }
        const scheme: string = ExternalSchemeHandler.schemeOf(url);
        if (scheme.length === 0) {
            bjccovms5u83s5.instrumentBranch(1, 1, true);
            bjccovms5u83s5.instrumentRegion(1, 2);
            return false;
        }
        else {
            bjccovms5u83s5.instrumentBranch(1, 1, false);
        }
        bjccovms5u83s5.instrumentRegion(1, 3);
        return whitelist.some((w: string) => { bjccovms5u83s5.instrumentFunction(2); return w.toLowerCase() === scheme; });
    }
    /**
     * 尝试拉起外部 App。
     *
     * @returns true=拉起成功; false=拉起失败(调用方需自行上报)
     */
    public static async open(context: common.UIAbilityContext, url: string): Promise<boolean> {
        bjccovms5u83s5.instrumentFunction(3);
        if (context === null || context === undefined) {
            bjccovms5u83s5.instrumentBranch(3, 0, true);
            bjccovms5u83s5.instrumentRegion(3, 1);
            return false;
        }
        else {
            bjccovms5u83s5.instrumentBranch(3, 0, false);
        }
        try {
            bjccovms5u83s5.instrumentRegion(3, 2);
            const want: Want = {
                action: 'ohos.want.action.viewData',
                uri: url
            };
            await context.startAbility(want);
            return true;
        }
        catch (_e) {
            bjccovms5u83s5.instrumentRegion(3, 3);
            return false;
        }
    }
    // ArkTS 类禁止实例化
    private constructor() { bjccovms5u83s5.instrumentFunction(4); }
}
