import router from "@ohos:router";
import type common from "@ohos:app.ability.common";
import { TencentQianWebViewConfig } from "@normalized:N&&&tencentqianwebview/src/main/ets/model/TencentQianWebViewConfig&1.0.0";
import type { OnSignResult, OnSdkEvent } from './model/SignCallback';
import { SignResultParser } from "@normalized:N&&&tencentqianwebview/src/main/ets/scheme/SignResultParser&1.0.0";
import { IntentBridge } from "@normalized:N&&&tencentqianwebview/src/main/ets/internal/IntentBridge&1.0.0";
import { TencentQianWebViewPrewarmer } from "@normalized:N&&&tencentqianwebview/src/main/ets/TencentQianWebViewPrewarmer&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 bjccovms5u83nl = new BjcCov({ version: "bjc v1.0.0", versionCode: 10000, path: "tencentqianwebview/src/main/ets/TencentQianWebView.ets", hash: "41a2338d947eeb45aa8c069b2ef34f4f18a797d87af28177fc51e93ba62e720f", lineCnt: 137, count: 0, projectPath: "", functions: { 0: { name: "TencentQianWebView.open", count: 0, regions: { 0: { startLoc: { line: 52, col: 3 }, endLoc: { line: 81, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 72, col: 9 }, endLoc: { line: 76, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 76, col: 7 }, endLoc: { line: 80, col: 6 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 60, col: 7 }, endLoc: { line: 60, col: 88 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 1: { startLoc: { line: 78, col: 30 }, endLoc: { line: 78, col: 74 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 0 }, 1: { name: "TencentQianWebView.openCompatibilityTest", count: 0, regions: { 0: { startLoc: { line: 94, col: 3 }, endLoc: { line: 117, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 100, col: 5 }, endLoc: { line: 117, col: 4 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 101, col: 7 }, endLoc: { line: 101, col: 88 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 1: { startLoc: { line: 103, col: 25 }, endLoc: { line: 107, col: 50 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 2: { startLoc: { line: 110, col: 36 }, endLoc: { line: 114, col: 10 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 1 }, 2: { name: "anonymous_0", count: 0, regions: { 0: { startLoc: { line: 112, col: 9 }, endLoc: { line: 114, col: 10 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 2 }, 3: { name: "TencentQianWebView.sdkVersion", count: 0, regions: { 0: { startLoc: { line: 120, col: 3 }, endLoc: { line: 122, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 121, col: 5 }, endLoc: { line: 122, col: 4 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 3 }, 4: { name: "TencentQianWebView.canHandle", count: 0, regions: { 0: { startLoc: { line: 130, col: 3 }, endLoc: { line: 132, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 131, col: 5 }, endLoc: { line: 132, col: 4 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 4 }, 5: { name: "TencentQianWebView.constructor", count: 0, regions: { 0: { startLoc: { line: 135, col: 3 }, endLoc: { line: 135, col: 27 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 5 } }, exeLine: { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 24, 8: 30, 9: 31, 10: 34, 11: 52, 12: 53, 13: 54, 14: 55, 15: 56, 16: 57, 17: 58, 18: 59, 19: 60, 20: 63, 21: 65, 22: 66, 23: 67, 24: 68, 25: 69, 26: 72, 27: 73, 28: 74, 29: 76, 30: 77, 31: 78, 32: 79, 33: 94, 34: 95, 35: 96, 36: 97, 37: 98, 38: 99, 39: 100, 40: 101, 41: 103, 42: 104, 43: 105, 44: 106, 45: 107, 46: 110, 47: 111, 48: 112, 49: 116, 50: 120, 51: 121, 52: 130, 53: 131, 54: 135 } });
/**
 * TencentQianWebView SDK 主入口。
 *
 * **典型用法**:
 * ```ts
 * TencentQianWebView.open(
 *   getContext(this) as common.UIAbilityContext,
 *   'https://example.com/sign?flowId=xxx',
 *   new TencentQianWebViewConfig(),
 *   (result) => {
 *     console.log('sign result', result.action, result.result);
 *   }
 * );
 * ```
 */
export class TencentQianWebView {
    /**
     * 自检页 URL 常量(SDK 默认 beta 环境,兼容性测试无需切生产)。
     */
    public static readonly COMPATIBILITY_TEST_URL: string = 'https://quick.beta.qian.tencent.cn/compatibilityTest';
    /** 容器内置页面的注册名(配套 router_map.json)。 */
    private static readonly INTERNAL_PAGE_NAME: string = 'TencentQianWebViewPage';
    /**
     * 在 SDK 内置容器页面中打开 H5 URL(页面形态,主推)。
     *
     * SDK 自动接管整个签署生命周期:
     * - 内置顶栏、进度条、关闭确认对话框
     * - 拦截 `qianapp://` 协议,触发 `onSignResult` 回调并自动关闭页面
     * - 处理权限申请、文件选择、下载、JS 弹窗
     *
     * 调用后立即返回,签署结果通过 `onSignResult` 异步回调。
     *
     * @param context UIAbility/页面 context
     * @param url 目标 H5 URL
     * @param config 容器配置(可省,使用默认)
     * @param onSignResult 签署结果回调(必填)
     * @param onSdkEvent SDK 事件回调(可选,用于日志/埋点)
     */
    public static async open(context: common.UIAbilityContext, url: string, config: TencentQianWebViewConfig | null, onSignResult: OnSignResult, onSdkEvent?: OnSdkEvent): Promise<void> {
        bjccovms5u83nl.instrumentFunction(0);
        const effectiveConfig: TencentQianWebViewConfig = config !== null && config !== undefined ? (bjccovms5u83nl.instrumentBranch(0, 0, true), config) : (bjccovms5u83nl.instrumentBranch(0, 0, false), new TencentQianWebViewConfig());
        // 预热环境错配检测（best-effort，仅告警，不阻断）
        TencentQianWebViewPrewarmer.warnIfEnvironmentMismatch(url);
        IntentBridge.set({
            url: url,
            config: effectiveConfig,
            onSignResult: onSignResult,
            onSdkEvent: onSdkEvent
        });
        try {
            bjccovms5u83nl.instrumentRegion(0, 1);
            await router.pushNamedRoute({
                name: TencentQianWebView.INTERNAL_PAGE_NAME
            });
        }
        catch (e) {
            bjccovms5u83nl.instrumentRegion(0, 2);
            IntentBridge.clear();
            const reason: string = (e instanceof Error) ? (bjccovms5u83nl.instrumentBranch(0, 1, true), e.message) : (bjccovms5u83nl.instrumentBranch(0, 1, false), String(e));
            console.error('[TencentQianWebView] router.pushNamedRoute failed: ' + reason);
        }
    }
    /**
     * 打开 SDK 自检页,用于在客户 App 中快速验证 SDK 集成情况
     * (UA 注入、scheme 拦截、文件上传、协议跳转等)。
     *
     * 默认走 SDK 内置 beta 环境地址,客户也可通过 `config.compatibilityTestUrl` 自定义。
     *
     * @param context UIAbilityContext
     * @param config 容器配置(可省)
     * @param onSignResult 签署结果回调(可省,自检页通常不关心)
     * @param onSdkEvent SDK 事件回调(可省)
     */
    public static async openCompatibilityTest(context: common.UIAbilityContext, config?: TencentQianWebViewConfig, onSignResult?: OnSignResult, onSdkEvent?: OnSdkEvent): Promise<void> {
        bjccovms5u83nl.instrumentFunction(1);
        bjccovms5u83nl.instrumentRegion(1, 1);
        const effectiveConfig: TencentQianWebViewConfig = config !== null && config !== undefined ? (bjccovms5u83nl.instrumentBranch(1, 0, true), config) : (bjccovms5u83nl.instrumentBranch(1, 0, false), new TencentQianWebViewConfig());
        const url: string = effectiveConfig.compatibilityTestUrl !== null
            && effectiveConfig.compatibilityTestUrl !== undefined
            && effectiveConfig.compatibilityTestUrl.length > 0
            ? (bjccovms5u83nl.instrumentBranch(1, 1, true), effectiveConfig.compatibilityTestUrl) : (bjccovms5u83nl.instrumentBranch(1, 1, false), TencentQianWebView.COMPATIBILITY_TEST_URL);
        // 自检页若客户不关心 sign result,提供空回调
        const callback: OnSignResult = onSignResult !== undefined
            ? (bjccovms5u83nl.instrumentBranch(1, 2, true), onSignResult) : (bjccovms5u83nl.instrumentBranch(1, 2, false), (_r): void => {
            bjccovms5u83nl.instrumentFunction(2);
        });
        await TencentQianWebView.open(context, url, effectiveConfig, callback, onSdkEvent);
    }
    /** 当前 SDK 版本号字符串。 */
    public static sdkVersion(): string {
        bjccovms5u83nl.instrumentFunction(3);
        bjccovms5u83nl.instrumentRegion(3, 1);
        return '1.2.0';
    }
    /**
     * 判断给定 URL 是否是 SDK 可识别的 `qianapp://` 协议。
     *
     * 用于客户在自有 Web 组件的 `onLoadIntercept` 中先做判断,然后决定是否
     * 转交 SDK 解析(配置模式集成方式),避免容器外的导航被静默吞掉。
     */
    public static canHandle(url: string): boolean {
        bjccovms5u83nl.instrumentFunction(4);
        bjccovms5u83nl.instrumentRegion(4, 1);
        return SignResultParser.isQianAppScheme(url);
    }
    // ArkTS 类禁止实例化
    private constructor() { bjccovms5u83nl.instrumentFunction(5); }
}
