import type webview from "@ohos:web.webview";
import type { TencentQianWebViewConfig } from '../model/TencentQianWebViewConfig';
import type { OnSdkEvent } from '../model/SignCallback';
import { UserAgentInjectFailedEvent } from "@normalized:N&&&tencentqianwebview/src/main/ets/model/SdkEvent&1.0.0";
import { TencentQianWebView } from "@normalized:N&&&tencentqianwebview/src/main/ets/TencentQianWebView&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 bjccovms5u83qt = new BjcCov({ version: "bjc v1.0.0", versionCode: 10000, path: "tencentqianwebview/src/main/ets/internal/UserAgentInjector.ets", hash: "1f0dd7abbbe0f584bd083fb4d72f96d118fe7dc4d1fff0703ec0fda4c0f34bd5", lineCnt: 49, count: 0, projectPath: "", functions: { 0: { name: "UserAgentInjector.inject", count: 0, regions: { 0: { startLoc: { line: 22, col: 3 }, endLoc: { line: 44, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 27, col: 34 }, endLoc: { line: 29, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 30, col: 9 }, endLoc: { line: 38, col: 6 }, count: 0, ignored: 0 }, 3: { startLoc: { line: 38, col: 7 }, endLoc: { line: 43, col: 6 }, count: 0, ignored: 0 }, 4: { startLoc: { line: 34, col: 70 }, endLoc: { line: 36, col: 8 }, count: 0, ignored: 0 }, 5: { startLoc: { line: 40, col: 34 }, endLoc: { line: 42, col: 8 }, count: 0, ignored: 0 }, 6: { startLoc: { line: 37, col: 7 }, endLoc: { line: 38, col: 6 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 27, col: 9 }, endLoc: { line: 27, col: 32 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 1: { startLoc: { line: 34, col: 11 }, endLoc: { line: 34, col: 68 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 2: { startLoc: { line: 39, col: 30 }, endLoc: { line: 39, col: 74 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 3: { startLoc: { line: 40, col: 11 }, endLoc: { line: 40, col: 32 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 0 }, 1: { name: "UserAgentInjector.constructor", count: 0, regions: { 0: { startLoc: { line: 47, col: 3 }, endLoc: { line: 47, col: 27 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 1 } }, exeLine: { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 14, 6: 22, 7: 23, 8: 24, 9: 25, 10: 26, 11: 27, 12: 28, 13: 30, 14: 31, 15: 32, 16: 33, 17: 34, 18: 35, 19: 37, 20: 38, 21: 39, 22: 40, 23: 41, 24: 47 } });
/**
 * UA 注入器。
 *
 * 在 controller 绑定 Web 组件之后（`onControllerAttached` 时机），
 * 将 SDK UA 标识 `qianwv/<version>` 与客户附加段拼接到默认 UA 之后。
 * 注入失败时通过 `UserAgentInjectFailed` 事件回报，不抛异常、不影响主流程。
 */
export class UserAgentInjector {
    /**
     * 注入 UA。
     *
     * @param controller 已 attach 的 WebviewController
     * @param config 客户配置
     * @param onEvent 可选的事件回调(注入失败时上报)
     */
    public static inject(controller: webview.WebviewController, config: TencentQianWebViewConfig, onEvent?: OnSdkEvent): void {
        bjccovms5u83qt.instrumentFunction(0);
        if (!config.injectUserAgent) {
            bjccovms5u83qt.instrumentBranch(0, 0, true);
            bjccovms5u83qt.instrumentRegion(0, 1);
            return;
        }
        else {
            bjccovms5u83qt.instrumentBranch(0, 0, false);
        }
        try {
            bjccovms5u83qt.instrumentRegion(0, 2);
            const defaultUa: string = controller.getUserAgent();
            let combined: string = defaultUa + ' ' + 'qianwv/' + TencentQianWebView.sdkVersion();
            const extra: string = config.userAgentExtra;
            if (extra !== null && extra !== undefined && extra.length > 0) {
                bjccovms5u83qt.instrumentBranch(0, 1, true);
                bjccovms5u83qt.instrumentRegion(0, 4);
                combined = combined + ' ' + extra;
            }
            else {
                bjccovms5u83qt.instrumentBranch(0, 1, false);
            }
            bjccovms5u83qt.instrumentRegion(0, 6);
            controller.setCustomUserAgent(combined);
        }
        catch (e) {
            bjccovms5u83qt.instrumentRegion(0, 3);
            const reason: string = (e instanceof Error) ? (bjccovms5u83qt.instrumentBranch(0, 2, true), e.message) : (bjccovms5u83qt.instrumentBranch(0, 2, false), String(e));
            if (onEvent !== undefined) {
                bjccovms5u83qt.instrumentBranch(0, 3, true);
                bjccovms5u83qt.instrumentRegion(0, 5);
                onEvent(new UserAgentInjectFailedEvent(reason));
            }
            else {
                bjccovms5u83qt.instrumentBranch(0, 3, false);
            }
        }
    }
    // ArkTS 类禁止实例化
    private constructor() { bjccovms5u83qt.instrumentFunction(1); }
}
