import type webview from "@ohos:web.webview";
import connection from "@ohos:net.connection";
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++;
                    /** 获取 document_start 注入脚本。 */
                }
            }
        }
        else {
            this.coverage.functions[func].branches[branch].falseCount++;
        }
    }
}
let bjccovms5u83qk = new BjcCov({ version: "bjc v1.0.0", versionCode: 10000, path: "tencentqianwebview/src/main/ets/internal/NetworkStateInjector.ets", hash: "1345ce2529d5801c20ca77dea42c2a5ac625769c7fe4e2bd906ead1e60c01657", lineCnt: 138, count: 0, projectPath: "", functions: { 0: { name: "NetworkStateInjector.getDocumentStartScript", count: 0, regions: { 0: { startLoc: { line: 34, col: 3 }, endLoc: { line: 36, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 35, col: 5 }, endLoc: { line: 36, col: 4 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 0 }, 1: { name: "NetworkStateInjector.attach", count: 0, regions: { 0: { startLoc: { line: 39, col: 3 }, endLoc: { line: 43, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 40, col: 5 }, endLoc: { line: 43, col: 4 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 1 }, 2: { name: "NetworkStateInjector.detach", count: 0, regions: { 0: { startLoc: { line: 46, col: 3 }, endLoc: { line: 49, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 47, col: 5 }, endLoc: { line: 49, col: 4 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 2 }, 3: { name: "installDocumentStartScript", count: 0, regions: { 0: { startLoc: { line: 52, col: 3 }, endLoc: { line: 67, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 53, col: 35 }, endLoc: { line: 55, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 56, col: 9 }, endLoc: { line: 64, col: 6 }, count: 0, ignored: 0 }, 3: { startLoc: { line: 64, col: 7 }, endLoc: { line: 66, col: 6 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 53, col: 9 }, endLoc: { line: 53, col: 33 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 3 }, 4: { name: "NetworkStateInjector.updateRuntime", count: 0, regions: { 0: { startLoc: { line: 70, col: 3 }, endLoc: { line: 81, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 71, col: 35 }, endLoc: { line: 73, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 76, col: 9 }, endLoc: { line: 78, col: 6 }, count: 0, ignored: 0 }, 3: { startLoc: { line: 78, col: 7 }, endLoc: { line: 80, col: 6 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 71, col: 9 }, endLoc: { line: 71, col: 33 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 1: { startLoc: { line: 74, col: 27 }, endLoc: { line: 74, col: 52 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 4 }, 5: { name: "NetworkStateInjector.refreshFromSystem", count: 0, regions: { 0: { startLoc: { line: 84, col: 3 }, endLoc: { line: 101, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 85, col: 9 }, endLoc: { line: 98, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 98, col: 7 }, endLoc: { line: 100, col: 6 }, count: 0, ignored: 0 }, 3: { startLoc: { line: 89, col: 11 }, endLoc: { line: 92, col: 10 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 5 }, 6: { name: "anonymous_0", count: 0, regions: { 0: { startLoc: { line: 86, col: 39 }, endLoc: { line: 95, col: 8 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 87, col: 9 }, endLoc: { line: 95, col: 8 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 6 }, 7: { name: "anonymous_1", count: 0, regions: { 0: { startLoc: { line: 87, col: 52 }, endLoc: { line: 92, col: 10 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 7 }, 8: { name: "anonymous_2", count: 0, regions: { 0: { startLoc: { line: 92, col: 18 }, endLoc: { line: 94, col: 10 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 93, col: 11 }, endLoc: { line: 94, col: 10 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 8 }, 9: { name: "anonymous_3", count: 0, regions: { 0: { startLoc: { line: 95, col: 16 }, endLoc: { line: 97, col: 8 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 96, col: 9 }, endLoc: { line: 97, col: 8 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 9 }, 10: { name: "NetworkStateInjector.startListen", count: 0, regions: { 0: { startLoc: { line: 104, col: 3 }, endLoc: { line: 123, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 105, col: 9 }, endLoc: { line: 120, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 120, col: 7 }, endLoc: { line: 122, col: 6 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 10 }, 11: { name: "anonymous_4", count: 0, regions: { 0: { startLoc: { line: 107, col: 38 }, endLoc: { line: 109, col: 8 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 108, col: 9 }, endLoc: { line: 109, col: 8 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 11 }, 12: { name: "anonymous_5", count: 0, regions: { 0: { startLoc: { line: 110, col: 33 }, endLoc: { line: 112, col: 8 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 111, col: 9 }, endLoc: { line: 112, col: 8 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 12 }, 13: { name: "anonymous_6", count: 0, regions: { 0: { startLoc: { line: 114, col: 9 }, endLoc: { line: 116, col: 10 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 115, col: 11 }, endLoc: { line: 116, col: 10 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 13 }, 14: { name: "anonymous_7", count: 0, regions: { 0: { startLoc: { line: 117, col: 28 }, endLoc: { line: 119, col: 8 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 14 }, 15: { name: "NetworkStateInjector.stopListen", count: 0, regions: { 0: { startLoc: { line: 125, col: 3 }, endLoc: { line: 136, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 126, col: 31 }, endLoc: { line: 135, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 127, col: 11 }, endLoc: { line: 131, col: 8 }, count: 0, ignored: 0 }, 3: { startLoc: { line: 131, col: 9 }, endLoc: { line: 133, col: 8 }, count: 0, ignored: 0 }, 4: { startLoc: { line: 134, col: 7 }, endLoc: { line: 135, col: 6 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 126, col: 9 }, endLoc: { line: 126, col: 29 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 15 }, 16: { name: "anonymous_8", count: 0, regions: { 0: { startLoc: { line: 128, col: 32 }, endLoc: { line: 130, col: 10 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 16 } }, exeLine: { 0: 1, 1: 2, 2: 7, 3: 8, 4: 9, 5: 15, 6: 16, 7: 17, 8: 20, 9: 21, 10: 22, 11: 23, 12: 24, 13: 25, 14: 26, 15: 27, 16: 28, 17: 29, 18: 30, 19: 31, 20: 34, 21: 35, 22: 39, 23: 40, 24: 41, 25: 42, 26: 46, 27: 47, 28: 48, 29: 52, 30: 53, 31: 54, 32: 56, 33: 57, 34: 58, 35: 59, 36: 62, 37: 63, 38: 64, 39: 70, 40: 71, 41: 72, 42: 74, 43: 75, 44: 76, 45: 77, 46: 78, 47: 84, 48: 85, 49: 86, 50: 87, 51: 88, 52: 89, 53: 90, 54: 91, 55: 92, 56: 93, 57: 95, 58: 96, 59: 98, 60: 99, 61: 104, 62: 105, 63: 106, 64: 107, 65: 108, 66: 110, 67: 111, 68: 113, 69: 114, 70: 115, 71: 117, 72: 120, 73: 125, 74: 126, 75: 127, 76: 128, 77: 131, 78: 134 } });
/**
 * `javaScriptOnDocumentStart` 脚本项。
 */
interface ScriptItemLike {
    script: string;
    scriptRules: string[];
}
/**
 * 将设备网络状态实时同步到 H5 的 `navigator.onLine`。
 */
export class NetworkStateInjector {
    private netCon: connection.NetConnection | null = null;
    private controller: webview.WebviewController | null = null;
    /** 在页面加载前注入的 JS 脚本，重写 `navigator.onLine` 并监听网络变化。 */
    private static readonly DOCUMENT_START_SCRIPT: string = '(function(){try{' +
        'if(window.__qianOnline===undefined){window.__qianOnline=true;}' +
        'try{Object.defineProperty(navigator,"onLine",{configurable:true,get:function(){return window.__qianOnline!==false;}});}catch(e){}' +
        'window.__qianSetOnline=function(v){' +
        'var prev=window.__qianOnline;' +
        'window.__qianOnline=!!v;' +
        'if(prev!==window.__qianOnline){' +
        'try{window.dispatchEvent(new Event(window.__qianOnline?"online":"offline"));}catch(e){}' +
        '}' +
        '};' +
        '}catch(e){}})();';
    /** 获取 document_start 注入脚本。 */
    public static getDocumentStartScript(): string {
        bjccovms5u83qk.instrumentFunction(0);
        bjccovms5u83qk.instrumentRegion(0, 1);
        return NetworkStateInjector.DOCUMENT_START_SCRIPT;
    }
    /** 绑定 WebView 控制器并启动网络监听。 */
    public attach(controller: webview.WebviewController): void {
        bjccovms5u83qk.instrumentFunction(1);
        bjccovms5u83qk.instrumentRegion(1, 1);
        this.controller = controller;
        this.installDocumentStartScript();
        this.startListen();
    }
    /** 解绑并释放资源。 */
    public detach(): void {
        bjccovms5u83qk.instrumentFunction(2);
        bjccovms5u83qk.instrumentRegion(2, 1);
        this.stopListen();
        this.controller = null;
    }
    /** 注册 document_start 脚本注入。 */
    private installDocumentStartScript(): void {
        bjccovms5u83qk.instrumentFunction(3);
        if (this.controller === null) {
            bjccovms5u83qk.instrumentBranch(3, 0, true);
            bjccovms5u83qk.instrumentRegion(3, 1);
            return;
        }
        else {
            bjccovms5u83qk.instrumentBranch(3, 0, false);
        }
        try {
            bjccovms5u83qk.instrumentRegion(3, 2);
            const scriptItems: ScriptItemLike[] = [{
                    script: NetworkStateInjector.DOCUMENT_START_SCRIPT,
                    scriptRules: ['*']
                }];
            // 通过 ESObject cast 调用,因为老版 .d.ts 未导出 javaScriptOnDocumentStart
            const ctrl: any = this.controller as any;
            ctrl.javaScriptOnDocumentStart(scriptItems);
        }
        catch (_e) {
            bjccovms5u83qk.instrumentRegion(3, 3);
        }
    }
    /** 同步网络状态到 H5 端。 */
    private updateRuntime(online: boolean): void {
        bjccovms5u83qk.instrumentFunction(4);
        if (this.controller === null) {
            bjccovms5u83qk.instrumentBranch(4, 0, true);
            bjccovms5u83qk.instrumentRegion(4, 1);
            return;
        }
        else {
            bjccovms5u83qk.instrumentBranch(4, 0, false);
        }
        const value: string = online ? (bjccovms5u83qk.instrumentBranch(4, 1, true), 'true') : (bjccovms5u83qk.instrumentBranch(4, 1, false), 'false');
        const script: string = 'try{window.__qianSetOnline&&window.__qianSetOnline(' + value + ');}catch(e){}';
        try {
            bjccovms5u83qk.instrumentRegion(4, 2);
            this.controller.runJavaScript(script);
        }
        catch (_e) {
            bjccovms5u83qk.instrumentRegion(4, 3);
        }
    }
    /** 查询系统网络状态并同步。 */
    private refreshFromSystem(): void {
        bjccovms5u83qk.instrumentFunction(5);
        try {
            bjccovms5u83qk.instrumentRegion(5, 1);
            connection.getDefaultNet().then((handle: connection.NetHandle) => {
                bjccovms5u83qk.instrumentFunction(6);
                bjccovms5u83qk.instrumentRegion(6, 1);
                connection.getNetCapabilities(handle).then((cap: connection.NetCapabilities) => {
                    bjccovms5u83qk.instrumentFunction(7);
                    const hasBearer: boolean = cap.bearerTypes !== undefined && cap.bearerTypes.length > 0;
                    bjccovms5u83qk.instrumentRegion(5, 3);
                    const validated: boolean = cap.networkCap !== undefined
                        && cap.networkCap.indexOf(connection.NetCap.NET_CAPABILITY_VALIDATED) >= 0;
                    this.updateRuntime(hasBearer && validated);
                }).catch((_e: Object): void => {
                    bjccovms5u83qk.instrumentFunction(8);
                    bjccovms5u83qk.instrumentRegion(8, 1);
                    this.updateRuntime(true);
                });
            }).catch((_e: Object): void => {
                bjccovms5u83qk.instrumentFunction(9);
                bjccovms5u83qk.instrumentRegion(9, 1);
                this.updateRuntime(false);
            });
        }
        catch (_e) {
            bjccovms5u83qk.instrumentRegion(5, 2);
            this.updateRuntime(true);
        }
    }
    /** 启动网络状态订阅。 */
    private startListen(): void {
        bjccovms5u83qk.instrumentFunction(10);
        try {
            bjccovms5u83qk.instrumentRegion(10, 1);
            this.netCon = connection.createNetConnection();
            this.netCon.on('netAvailable', (_handle: connection.NetHandle): void => {
                bjccovms5u83qk.instrumentFunction(11);
                bjccovms5u83qk.instrumentRegion(11, 1);
                this.refreshFromSystem();
            });
            this.netCon.on('netLost', (_handle: connection.NetHandle): void => {
                bjccovms5u83qk.instrumentFunction(12);
                bjccovms5u83qk.instrumentRegion(12, 1);
                this.updateRuntime(false);
            });
            this.netCon.on('netCapabilitiesChange', (_info: connection.NetCapabilityInfo): void => {
                bjccovms5u83qk.instrumentFunction(13);
                bjccovms5u83qk.instrumentRegion(13, 1);
                this.refreshFromSystem();
            });
            this.netCon.register((_e: Object): void => {
                bjccovms5u83qk.instrumentFunction(14);
            });
        }
        catch (_e) {
            bjccovms5u83qk.instrumentRegion(10, 2);
        }
    }
    private stopListen(): void {
        bjccovms5u83qk.instrumentFunction(15);
        if (this.netCon !== null) {
            bjccovms5u83qk.instrumentBranch(15, 0, true);
            bjccovms5u83qk.instrumentRegion(15, 1);
            try {
                bjccovms5u83qk.instrumentRegion(15, 2);
                this.netCon.unregister((_e: Object): void => {
                    bjccovms5u83qk.instrumentFunction(16);
                });
            }
            catch (_e) {
                bjccovms5u83qk.instrumentRegion(15, 3);
            }
            bjccovms5u83qk.instrumentRegion(15, 4);
            this.netCon = null;
        }
        else {
            bjccovms5u83qk.instrumentBranch(15, 0, false);
        }
    }
}
