import webview from "@ohos:web.webview";
import type { UIContext } from "@ohos:arkui.UIContext";
import { SDK_VERSION } from "@normalized:N&&&tencentqianwebview/src/main/ets/internal/SdkVersion&1.0.0";
import { WebHostModel } from "@normalized:N&&&tencentqianwebview/src/main/ets/internal/prewarm/WebHostModel&1.0.0";
import { WebNodeController } from "@normalized:N&&&tencentqianwebview/src/main/ets/internal/prewarm/TencentQianWebNode&1.0.0";
import type { TencentQianWebViewHandlers, ScriptItemLike } from '../../TencentQianWebViewConfigurator';
import { WebSettingsApplier } from "@normalized:N&&&tencentqianwebview/src/main/ets/internal/WebSettingsApplier&1.0.0";
import { NetworkStateInjector } from "@normalized:N&&&tencentqianwebview/src/main/ets/internal/NetworkStateInjector&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 bjccovms5u83t6 = new BjcCov({ version: "bjc v1.0.0", versionCode: 10000, path: "tencentqianwebview/src/main/ets/internal/prewarm/HarmonyWebViewPool.ets", hash: "a7284e03332134fd361c878681edb37f479889698179c0bc37b56570df93c489", lineCnt: 272, count: 0, projectPath: "", functions: { 0: { name: "uaSignatureOf", count: 0, regions: { 0: { startLoc: { line: 28, col: 1 }, endLoc: { line: 30, col: 2 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 29, col: 3 }, endLoc: { line: 30, col: 2 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 29, col: 11 }, endLoc: { line: 29, col: 31 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 0 }, 1: { name: "buildPrewarmHandlers", count: 0, regions: { 0: { startLoc: { line: 36, col: 1 }, endLoc: { line: 90, col: 2 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 41, col: 3 }, endLoc: { line: 90, col: 2 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 56, col: 11 }, endLoc: { line: 57, col: 10 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 1 }, 2: { name: "anonymous_0", count: 0, regions: { 0: { startLoc: { line: 48, col: 27 }, endLoc: { line: 66, col: 6 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 49, col: 11 }, endLoc: { line: 58, col: 8 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 58, col: 9 }, endLoc: { line: 60, col: 8 }, count: 0, ignored: 0 }, 3: { startLoc: { line: 50, col: 23 }, endLoc: { line: 57, col: 10 }, count: 0, ignored: 0 }, 4: { startLoc: { line: 53, col: 35 }, endLoc: { line: 55, col: 12 }, count: 0, ignored: 0 }, 5: { startLoc: { line: 61, col: 11 }, endLoc: { line: 63, col: 8 }, count: 0, ignored: 0 }, 6: { startLoc: { line: 63, col: 9 }, endLoc: { line: 65, col: 8 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 50, col: 13 }, endLoc: { line: 50, col: 21 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 1: { startLoc: { line: 53, col: 15 }, endLoc: { line: 53, col: 33 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 2 }, 3: { name: "anonymous_1", count: 0, regions: { 0: { startLoc: { line: 67, col: 22 }, endLoc: { line: 67, col: 58 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 3 }, 4: { name: "anonymous_2", count: 0, regions: { 0: { startLoc: { line: 68, col: 18 }, endLoc: { line: 68, col: 48 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 4 }, 5: { name: "anonymous_3", count: 0, regions: { 0: { startLoc: { line: 69, col: 16 }, endLoc: { line: 69, col: 46 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 5 }, 6: { name: "anonymous_4", count: 0, regions: { 0: { startLoc: { line: 70, col: 26 }, endLoc: { line: 70, col: 56 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 6 }, 7: { name: "anonymous_5", count: 0, regions: { 0: { startLoc: { line: 71, col: 25 }, endLoc: { line: 71, col: 61 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 7 }, 8: { name: "anonymous_6", count: 0, regions: { 0: { startLoc: { line: 72, col: 14 }, endLoc: { line: 72, col: 50 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 8 }, 9: { name: "anonymous_7", count: 0, regions: { 0: { startLoc: { line: 73, col: 16 }, endLoc: { line: 73, col: 52 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 9 }, 10: { name: "anonymous_8", count: 0, regions: { 0: { startLoc: { line: 74, col: 15 }, endLoc: { line: 74, col: 51 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 10 }, 11: { name: "anonymous_9", count: 0, regions: { 0: { startLoc: { line: 75, col: 22 }, endLoc: { line: 75, col: 52 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 11 }, 12: { name: "anonymous_10", count: 0, regions: { 0: { startLoc: { line: 76, col: 21 }, endLoc: { line: 76, col: 51 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 12 }, 13: { name: "anonymous_11", count: 0, regions: { 0: { startLoc: { line: 77, col: 25 }, endLoc: { line: 77, col: 55 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 13 }, 14: { name: "anonymous_12", count: 0, regions: { 0: { startLoc: { line: 78, col: 29 }, endLoc: { line: 78, col: 59 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 14 }, 15: { name: "anonymous_13", count: 0, regions: { 0: { startLoc: { line: 84, col: 21 }, endLoc: { line: 86, col: 6 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 85, col: 7 }, endLoc: { line: 86, col: 6 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 15 }, 16: { name: "anonymous_14", count: 0, regions: { 0: { startLoc: { line: 87, col: 23 }, endLoc: { line: 87, col: 53 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 16 }, 17: { name: "HarmonyWebViewPool.setEnabled", count: 0, regions: { 0: { startLoc: { line: 107, col: 3 }, endLoc: { line: 112, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 109, col: 17 }, endLoc: { line: 111, col: 6 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 109, col: 9 }, endLoc: { line: 109, col: 15 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 17 }, 18: { name: "HarmonyWebViewPool.warm", count: 0, regions: { 0: { startLoc: { line: 117, col: 3 }, endLoc: { line: 147, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 118, col: 64 }, endLoc: { line: 120, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 121, col: 9 }, endLoc: { line: 144, col: 6 }, count: 0, ignored: 0 }, 3: { startLoc: { line: 144, col: 7 }, endLoc: { line: 146, col: 6 }, count: 0, ignored: 0 }, 4: { startLoc: { line: 122, col: 45 }, endLoc: { line: 133, col: 8 }, count: 0, ignored: 0 }, 5: { startLoc: { line: 133, col: 14 }, endLoc: { line: 142, col: 8 }, count: 0, ignored: 0 }, 6: { startLoc: { line: 133, col: 91 }, endLoc: { line: 142, col: 8 }, count: 0, ignored: 0 }, 7: { startLoc: { line: 135, col: 29 }, endLoc: { line: 141, col: 10 }, count: 0, ignored: 0 }, 8: { startLoc: { line: 136, col: 15 }, endLoc: { line: 138, col: 12 }, count: 0, ignored: 0 }, 9: { startLoc: { line: 138, col: 13 }, endLoc: { line: 140, col: 12 }, count: 0, ignored: 0 }, 10: { startLoc: { line: 143, col: 7 }, endLoc: { line: 144, col: 6 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 118, col: 9 }, endLoc: { line: 118, col: 62 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 1: { startLoc: { line: 122, col: 11 }, endLoc: { line: 122, col: 43 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 2: { startLoc: { line: 133, col: 18 }, endLoc: { line: 133, col: 89 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 3: { startLoc: { line: 135, col: 13 }, endLoc: { line: 135, col: 27 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 18 }, 19: { name: "HarmonyWebViewPool.obtain", count: 0, regions: { 0: { startLoc: { line: 152, col: 3 }, endLoc: { line: 161, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 153, col: 102 }, endLoc: { line: 155, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 156, col: 72 }, endLoc: { line: 158, col: 6 }, count: 0, ignored: 0 }, 3: { startLoc: { line: 159, col: 5 }, endLoc: { line: 161, col: 4 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 153, col: 9 }, endLoc: { line: 153, col: 100 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 1: { startLoc: { line: 156, col: 9 }, endLoc: { line: 156, col: 70 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 19 }, 20: { name: "HarmonyWebViewPool.recycle", count: 0, regions: { 0: { startLoc: { line: 171, col: 3 }, endLoc: { line: 191, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 172, col: 43 }, endLoc: { line: 174, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 176, col: 25 }, endLoc: { line: 188, col: 6 }, count: 0, ignored: 0 }, 3: { startLoc: { line: 177, col: 11 }, endLoc: { line: 180, col: 8 }, count: 0, ignored: 0 }, 4: { startLoc: { line: 180, col: 9 }, endLoc: { line: 182, col: 8 }, count: 0, ignored: 0 }, 5: { startLoc: { line: 189, col: 5 }, endLoc: { line: 191, col: 4 }, count: 0, ignored: 0 }, 6: { startLoc: { line: 185, col: 7 }, endLoc: { line: 188, col: 6 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 172, col: 9 }, endLoc: { line: 172, col: 41 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 1: { startLoc: { line: 176, col: 9 }, endLoc: { line: 176, col: 23 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 20 }, 21: { name: "HarmonyWebViewPool.isInUse", count: 0, regions: { 0: { startLoc: { line: 193, col: 3 }, endLoc: { line: 195, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 194, col: 5 }, endLoc: { line: 195, col: 4 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 21 }, 22: { name: "HarmonyWebViewPool.releaseIfIdle", count: 0, regions: { 0: { startLoc: { line: 206, col: 3 }, endLoc: { line: 212, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 207, col: 71 }, endLoc: { line: 209, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 210, col: 5 }, endLoc: { line: 212, col: 4 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 207, col: 9 }, endLoc: { line: 207, col: 69 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 22 }, 23: { name: "HarmonyWebViewPool.invalidate", count: 0, regions: { 0: { startLoc: { line: 224, col: 3 }, endLoc: { line: 226, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 225, col: 5 }, endLoc: { line: 226, col: 4 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 23 }, 24: { name: "HarmonyWebViewPool.unpoolBorrowed", count: 0, regions: { 0: { startLoc: { line: 240, col: 3 }, endLoc: { line: 247, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 241, col: 5 }, endLoc: { line: 247, col: 4 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 24 }, 25: { name: "HarmonyWebViewPool.release", count: 0, regions: { 0: { startLoc: { line: 252, col: 3 }, endLoc: { line: 267, col: 4 }, count: 0, ignored: 0 }, 1: { startLoc: { line: 253, col: 43 }, endLoc: { line: 261, col: 6 }, count: 0, ignored: 0 }, 2: { startLoc: { line: 256, col: 27 }, endLoc: { line: 258, col: 8 }, count: 0, ignored: 0 }, 3: { startLoc: { line: 262, col: 5 }, endLoc: { line: 267, col: 4 }, count: 0, ignored: 0 }, 4: { startLoc: { line: 259, col: 7 }, endLoc: { line: 261, col: 6 }, count: 0, ignored: 0 } }, branches: { 0: { startLoc: { line: 253, col: 9 }, endLoc: { line: 253, col: 41 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 }, 1: { startLoc: { line: 256, col: 11 }, endLoc: { line: 256, col: 25 }, trueCount: 0, falseCount: 0, group: {}, ignored: 0 } }, ignored: 0, index: 25 }, 26: { name: "HarmonyWebViewPool.constructor", count: 0, regions: { 0: { startLoc: { line: 270, col: 3 }, endLoc: { line: 270, col: 27 }, count: 0, ignored: 0 } }, branches: {}, ignored: 0, index: 26 } }, exeLine: { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 28, 9: 29, 10: 36, 11: 37, 12: 38, 13: 39, 14: 40, 15: 41, 16: 42, 17: 43, 18: 44, 19: 45, 20: 46, 21: 47, 22: 48, 23: 49, 24: 50, 25: 51, 26: 52, 27: 53, 28: 54, 29: 56, 30: 58, 31: 61, 32: 62, 33: 63, 34: 67, 35: 68, 36: 69, 37: 70, 38: 71, 39: 72, 40: 73, 41: 74, 42: 75, 43: 76, 44: 77, 45: 78, 46: 84, 47: 85, 48: 87, 49: 89, 50: 95, 51: 96, 52: 97, 53: 99, 54: 101, 55: 103, 56: 104, 57: 105, 58: 107, 59: 108, 60: 109, 61: 110, 62: 117, 63: 118, 64: 119, 65: 121, 66: 122, 67: 123, 68: 124, 69: 125, 70: 126, 71: 127, 72: 128, 73: 129, 74: 130, 75: 131, 76: 132, 77: 133, 78: 134, 79: 135, 80: 136, 81: 137, 82: 138, 83: 143, 84: 144, 85: 152, 86: 153, 87: 154, 88: 156, 89: 157, 90: 159, 91: 160, 92: 171, 93: 172, 94: 173, 95: 175, 96: 176, 97: 177, 98: 178, 99: 179, 100: 180, 101: 185, 102: 186, 103: 187, 104: 189, 105: 190, 106: 193, 107: 194, 108: 206, 109: 207, 110: 208, 111: 210, 112: 211, 113: 224, 114: 225, 115: 240, 116: 241, 117: 242, 118: 243, 119: 244, 120: 245, 121: 246, 122: 252, 123: 253, 124: 255, 125: 256, 126: 257, 127: 259, 128: 260, 129: 262, 130: 263, 131: 264, 132: 265, 133: 266, 134: 270 } });
/**
 * 鸿蒙 WebView 离屏复用池（容量 = 1，**借还池**，与 Android 规格一致）。
 *
 * 基于官方「离线 Web 组件」：`WebNodeController` + `BuilderNode` 命令式创建 Web 组件（离屏、
 * Hidden/Inactive、启动渲染进程），HEAVY 加载 `warmupUrl` 热网络缓存。真实页容器用
 * `NodeContainer(nodeController)` 绑定复用，通过热替换 `WebHostModel.handlers` / `url` 切换为签署态。
 *
 * **生命周期（借还池，同 Android）**：
 * - HEAVY `warm()`：离屏建组件 + load(warmupUrl)；多次 prewarm 复用同一实例，绝不重复建。
 * - `open()` → `obtain()`：借出（UA 配置匹配才复用），容器热替换 handlers/url 并 loadUrl 真实页。
 * - 容器关闭 → `recycle()`：**不销毁**，`about:blank` + `clearHistory` 后放回池，待下次复用。
 * - 客户主动 `release()`（或内存告警）：才真正 `dispose` 销毁。
 *
 * **约束**：`BuilderNode` 需 `UIContext`（`prewarm(HEAVY, bizType, uiContext)` 传入）；
 * 每组件约 200MB，容量恒 1；`dispose` 前须先从 `NodeContainer` 解绑（`onUnbind`），否则白屏。
 */
/** UA 签名：复用门控——预热 UA 配置需与 `open()` config UA 一致，否则不复用（避免 warmupUrl 缓存 miss）。 */
function uaSignatureOf(injectUA: boolean, uaExtra: string): string {
    bjccovms5u83t6.instrumentFunction(0);
    bjccovms5u83t6.instrumentRegion(0, 1);
    return (injectUA ? (bjccovms5u83t6.instrumentBranch(0, 0, true), '1') : (bjccovms5u83t6.instrumentBranch(0, 0, false), '0')) + '|' + uaExtra;
}
/**
 * 构造预热用最小 handlers：DEFAULTS settings + UA 注入 + no-op 事件。
 * 真实页复用时会被 `Configurator.apply` 生成的真实 handlers 热替换（见 Page/Component）。
 */
function buildPrewarmHandlers(controller: webview.WebviewController, injectUA: boolean, uaExtra: string): TencentQianWebViewHandlers {
    bjccovms5u83t6.instrumentFunction(1);
    bjccovms5u83t6.instrumentRegion(1, 1);
    const injector: NetworkStateInjector = new NetworkStateInjector();
    const emptyScripts: Array<ScriptItemLike> = [];
    const handlers: TencentQianWebViewHandlers = {
        webSettings: WebSettingsApplier.DEFAULTS,
        mixedMode: 'None',
        documentStartScripts: emptyScripts,
        networkStateInjector: injector,
        onControllerAttached: (): void => {
            bjccovms5u83t6.instrumentFunction(2);
            try {
                bjccovms5u83t6.instrumentRegion(2, 1);
                if (injectUA) {
                    bjccovms5u83t6.instrumentBranch(2, 0, true);
                    bjccovms5u83t6.instrumentRegion(2, 3);
                    const base: string = controller.getUserAgent();
                    let ua: string = base + ' qianwv/' + SDK_VERSION;
                    if (uaExtra.length > 0) {
                        bjccovms5u83t6.instrumentBranch(2, 1, true);
                        bjccovms5u83t6.instrumentRegion(2, 4);
                        ua = ua + ' ' + uaExtra;
                    }
                    else {
                        bjccovms5u83t6.instrumentBranch(2, 1, false);
                    }
                    bjccovms5u83t6.instrumentRegion(1, 2);
                    controller.setCustomUserAgent(ua);
                }
                else {
                    bjccovms5u83t6.instrumentBranch(2, 0, false);
                }
            }
            catch (_e) {
                bjccovms5u83t6.instrumentRegion(2, 2);
            }
            try {
                bjccovms5u83t6.instrumentRegion(2, 5);
                injector.attach(controller);
            }
            catch (_e) {
                bjccovms5u83t6.instrumentRegion(2, 6);
            }
        },
        onLoadIntercept: (_event: any): boolean => { bjccovms5u83t6.instrumentFunction(3); return false; },
        onPageBegin: (_event: any): void => { bjccovms5u83t6.instrumentFunction(4); },
        onPageEnd: (_event: any): void => { bjccovms5u83t6.instrumentFunction(5); },
        onPermissionRequest: (_event: any): void => { bjccovms5u83t6.instrumentFunction(6); },
        onShowFileSelector: (_event: any): boolean => { bjccovms5u83t6.instrumentFunction(7); return false; },
        onAlert: (_event: any): boolean => { bjccovms5u83t6.instrumentFunction(8); return false; },
        onConfirm: (_event: any): boolean => { bjccovms5u83t6.instrumentFunction(9); return false; },
        onPrompt: (_event: any): boolean => { bjccovms5u83t6.instrumentFunction(10); return false; },
        onDownloadStart: (_event: any): void => { bjccovms5u83t6.instrumentFunction(11); },
        onErrorReceive: (_event: any): void => { bjccovms5u83t6.instrumentFunction(12); },
        onHttpErrorReceive: (_event: any): void => { bjccovms5u83t6.instrumentFunction(13); },
        onSslErrorEventReceive: (_event: any): void => { bjccovms5u83t6.instrumentFunction(14); },
        // 预热态 / 归还态守卫：此时节点未绑定在可见容器上，渲染进程崩溃直接销毁并作废池实例
        // （避免"渲染进程已死的僵尸实例"被后续 obtain 复用导致持久白屏）。对齐 Android PoolGuardClient。
        // 借出态不走这里：借出后 handlers 被 Configurator 真实 handlers 热替换（其 onRenderExited 仅发事件、
        // 不作废池），作废由统一 Builder 的 onPoolInvalidateRequest → unpoolBorrowed() 完成
        // （只脱池、不销毁正在显示的活节点）。
        onRenderExited: (_event: any): void => {
            bjccovms5u83t6.instrumentFunction(15);
            bjccovms5u83t6.instrumentRegion(15, 1);
            HarmonyWebViewPool.invalidate();
        },
        onProgressChange: (_event: any): void => { bjccovms5u83t6.instrumentFunction(16); }
    };
    return handlers;
}
/**
 * 鸿蒙离屏借还池（单例，容量 1）。
 */
export class HarmonyWebViewPool {
    private static node: WebNodeController | null = null;
    private static warmedUrl: string = '';
    /** 是否已被容器借出（借出中不可再 obtain）。 */
    private static inUse: boolean = false;
    /** 预热时的 UA 签名，供复用门控比对。 */
    private static uaSig: string = '';
    /** 预热时的原始 UA 配置，供归还时重建预热守卫 handlers（避免从 uaSig 反解）。 */
    private static warmedInjectUA: boolean = false;
    private static warmedUaExtra: string = '';
    private static enabled: boolean = true;
    public static setEnabled(value: boolean): void {
        bjccovms5u83t6.instrumentFunction(17);
        HarmonyWebViewPool.enabled = value;
        if (!value) {
            bjccovms5u83t6.instrumentBranch(17, 0, true);
            bjccovms5u83t6.instrumentRegion(17, 1);
            HarmonyWebViewPool.release();
        }
        else /**
         * HEAVY：离屏建 Web 组件并加载 `warmupUrl`。多次 prewarm 复用同一实例（按需切 URL），绝不重复建。
         */ {
            bjccovms5u83t6.instrumentBranch(17, 0, false);
        }
    }
    /**
     * HEAVY：离屏建 Web 组件并加载 `warmupUrl`。多次 prewarm 复用同一实例（按需切 URL），绝不重复建。
     */
    public static warm(warmupUrl: string, uiContext: UIContext, injectUA: boolean, uaExtra: string): void {
        bjccovms5u83t6.instrumentFunction(18);
        if (!HarmonyWebViewPool.enabled || warmupUrl.length === 0) {
            bjccovms5u83t6.instrumentBranch(18, 0, true);
            bjccovms5u83t6.instrumentRegion(18, 1);
            return;
        }
        else {
            bjccovms5u83t6.instrumentBranch(18, 0, false);
        }
        try {
            bjccovms5u83t6.instrumentRegion(18, 2);
            if (HarmonyWebViewPool.node === null) {
                bjccovms5u83t6.instrumentBranch(18, 1, true);
                bjccovms5u83t6.instrumentRegion(18, 4);
                const controller: webview.WebviewController = new webview.WebviewController();
                const model: WebHostModel = new WebHostModel(controller);
                model.url = warmupUrl;
                model.handlers = buildPrewarmHandlers(controller, injectUA, uaExtra);
                const ctrl: WebNodeController = new WebNodeController();
                ctrl.init(uiContext, model);
                HarmonyWebViewPool.node = ctrl;
                HarmonyWebViewPool.uaSig = uaSignatureOf(injectUA, uaExtra);
                HarmonyWebViewPool.warmedInjectUA = injectUA;
                HarmonyWebViewPool.warmedUaExtra = uaExtra;
            }
            else {
                bjccovms5u83t6.instrumentBranch(18, 1, false);
                bjccovms5u83t6.instrumentRegion(18, 5);
                if (warmupUrl !== HarmonyWebViewPool.warmedUrl && !HarmonyWebViewPool.inUse) {
                    bjccovms5u83t6.instrumentBranch(18, 2, true);
                    bjccovms5u83t6.instrumentRegion(18, 6);
                    const model: WebHostModel | null = HarmonyWebViewPool.node.getModel();
                    if (model !== null) {
                        bjccovms5u83t6.instrumentBranch(18, 3, true);
                        bjccovms5u83t6.instrumentRegion(18, 7);
                        try {
                            bjccovms5u83t6.instrumentRegion(18, 8);
                            model.controller.loadUrl(warmupUrl);
                        }
                        catch (_e) {
                            bjccovms5u83t6.instrumentRegion(18, 9);
                        }
                    }
                    else {
                        bjccovms5u83t6.instrumentBranch(18, 3, false);
                    }
                }
                else {
                    bjccovms5u83t6.instrumentBranch(18, 2, false);
                }
            }
            bjccovms5u83t6.instrumentRegion(18, 10);
            HarmonyWebViewPool.warmedUrl = warmupUrl;
        }
        catch (_e) {
            bjccovms5u83t6.instrumentRegion(18, 3);
        }
    }
    /**
     * 容器复用：取出池 node（未借出、已就绪、且 UA 配置匹配时返回；否则 null → 容器自建）。
     */
    public static obtain(injectUA: boolean, uaExtra: string): WebNodeController | null {
        bjccovms5u83t6.instrumentFunction(19);
        if (!HarmonyWebViewPool.enabled || HarmonyWebViewPool.node === null || HarmonyWebViewPool.inUse) {
            bjccovms5u83t6.instrumentBranch(19, 0, true);
            bjccovms5u83t6.instrumentRegion(19, 1);
            return null;
        }
        else {
            bjccovms5u83t6.instrumentBranch(19, 0, false);
        }
        if (HarmonyWebViewPool.uaSig !== uaSignatureOf(injectUA, uaExtra)) {
            bjccovms5u83t6.instrumentBranch(19, 1, true);
            bjccovms5u83t6.instrumentRegion(19, 2);
            return null;
        }
        else {
            bjccovms5u83t6.instrumentBranch(19, 1, false);
        }
        bjccovms5u83t6.instrumentRegion(19, 3);
        HarmonyWebViewPool.inUse = true;
        return HarmonyWebViewPool.node;
    }
    /**
     * 归还（借还池，**不销毁**）：清空内容 + 清历史，保留实例待下次复用（同 Android）。
     * 容器须已先从 `NodeContainer` 解绑。
     *
     * **归还态守卫（对齐 Android QianFallbackWebViewClient）**：把 `model.handlers` 复位为
     * 预热守卫 handlers，其 `onRenderExited` 会作废池——覆盖"归还后 ~ 下次借出前"窗口内渲染
     * 进程崩溃的场景，避免僵尸实例被后续 `obtain` 复用。同时重置 `model.fromPool=false`。
     */
    public static recycle(): void {
        bjccovms5u83t6.instrumentFunction(20);
        if (HarmonyWebViewPool.node === null) {
            bjccovms5u83t6.instrumentBranch(20, 0, true);
            bjccovms5u83t6.instrumentRegion(20, 1);
            return;
        }
        else {
            bjccovms5u83t6.instrumentBranch(20, 0, false);
        }
        const model: WebHostModel | null = HarmonyWebViewPool.node.getModel();
        if (model !== null) {
            bjccovms5u83t6.instrumentBranch(20, 1, true);
            bjccovms5u83t6.instrumentRegion(20, 2);
            try {
                bjccovms5u83t6.instrumentRegion(20, 3);
                model.controller.loadUrl('about:blank');
                model.controller.clearHistory();
            }
            catch (_e) {
                bjccovms5u83t6.instrumentRegion(20, 4);
            }
            bjccovms5u83t6.instrumentRegion(20, 6);
            // 复位为预热态守卫 handlers：归还态渲染进程崩溃时作废池（三态闭环）。
            // 直接复用预热时保存的原始 UA 配置，避免从 uaSig 反解。
            model.handlers = buildPrewarmHandlers(model.controller, HarmonyWebViewPool.warmedInjectUA, HarmonyWebViewPool.warmedUaExtra);
            model.fromPool = false;
        }
        else {
            bjccovms5u83t6.instrumentBranch(20, 1, false);
        }
        bjccovms5u83t6.instrumentRegion(20, 5);
        HarmonyWebViewPool.warmedUrl = 'about:blank';
        HarmonyWebViewPool.inUse = false;
    }
    public static isInUse(): boolean {
        bjccovms5u83t6.instrumentFunction(21);
        bjccovms5u83t6.instrumentRegion(21, 1);
        return HarmonyWebViewPool.inUse;
    }
    /**
     * 内存压力回收：**仅在实例未被借出（`!inUse`）且存在时**才 dispose 销毁，
     * 避免打断正在使用的签署容器。
     *
     * 借出中（`inUse === true`）则跳过——等容器关闭 `recycle()` 归还回空闲态后，
     * 下次内存告警再回收。
     *
     * @returns 是否真正释放了实例（供上层据此决定是否重置去重状态，使后续 prewarm 可重新预热）
     */
    public static releaseIfIdle(): boolean {
        bjccovms5u83t6.instrumentFunction(22);
        if (HarmonyWebViewPool.inUse || HarmonyWebViewPool.node === null) {
            bjccovms5u83t6.instrumentBranch(22, 0, true);
            bjccovms5u83t6.instrumentRegion(22, 1);
            return false;
        }
        else {
            bjccovms5u83t6.instrumentBranch(22, 0, false);
        }
        bjccovms5u83t6.instrumentRegion(22, 2);
        HarmonyWebViewPool.release();
        return true;
    }
    /**
     * 作废池实例（**预热态 / 归还态**渲染进程崩溃场景，对齐 Android `invalidateIfPooled`）。
     *
     * 此时 node 未绑定在任何可见容器上，销毁当前池 node 并清空全部池状态是安全的，
     * 避免"渲染进程已死的僵尸实例"被后续 `obtain` 反复借出导致持久白屏。容量恒 1，
     * 故直接作废当前实例即可；下次 `obtain` 返回 null → 容器自建。
     *
     * ⚠️ **借出态**（node 正绑在容器 `NodeContainer` 上显示）崩溃时**不要**用本方法：
     * 销毁正在显示、且本可原地 `refresh()` 恢复的活节点会导致白屏/闭页，请用 {@link unpoolBorrowed}。
     */
    public static invalidate(): void {
        bjccovms5u83t6.instrumentFunction(23);
        bjccovms5u83t6.instrumentRegion(23, 1);
        HarmonyWebViewPool.release();
    }
    /**
     * 借出态渲染进程崩溃专用：**只脱池、不销毁正在显示的活节点**
     * （对齐 Android `invalidateIfPooled`——只清池槽位、不销毁 Activity 正用的 WebView）。
     *
     * 借出后 node 仍绑在容器 `NodeContainer` 上，`WebviewController` 渲染进程崩溃后依旧有效，
     * 容器会在同一 controller 上 `refresh()` 原地恢复（鸿蒙无需与界面节点分离 / 重建）。
     * 故这里仅清空池账本，使该崩溃实例**不再被 `obtain` 借出、也不再被 `recycle` 收回**；
     * 活节点由容器继续持有，最终在容器关闭（`aboutToDisappear`，`fromPool=false` → `disposeNode()`）
     * 时销毁，生命周期闭环不变。下次 `obtain` 因 `node=null` 返回 null → 新 `open()` 自建新实例，不复用僵尸。
     *
     * **刻意不做** `disposeNode()` / `cancelPendingRecover()`——避免打断正在进行的原地崩溃恢复。
     */
    public static unpoolBorrowed(): void {
        bjccovms5u83t6.instrumentFunction(24);
        bjccovms5u83t6.instrumentRegion(24, 1);
        HarmonyWebViewPool.node = null;
        HarmonyWebViewPool.warmedUrl = '';
        HarmonyWebViewPool.inUse = false;
        HarmonyWebViewPool.uaSig = '';
        HarmonyWebViewPool.warmedInjectUA = false;
        HarmonyWebViewPool.warmedUaExtra = '';
    }
    /**
     * 客户主动释放池实例（销毁）。签署流程结束、不再需要时调用。
     */
    public static release(): void {
        bjccovms5u83t6.instrumentFunction(25);
        if (HarmonyWebViewPool.node !== null) {
            bjccovms5u83t6.instrumentBranch(25, 0, true);
            bjccovms5u83t6.instrumentRegion(25, 1);
            // 取消可能挂起的延迟恢复，避免回调访问即将 dispose 的 controller（野指针）。
            const model: WebHostModel | null = HarmonyWebViewPool.node.getModel();
            if (model !== null) {
                bjccovms5u83t6.instrumentBranch(25, 1, true);
                bjccovms5u83t6.instrumentRegion(25, 2);
                model.cancelPendingRecover();
            }
            else {
                bjccovms5u83t6.instrumentBranch(25, 1, false);
            }
            bjccovms5u83t6.instrumentRegion(25, 4);
            HarmonyWebViewPool.node.disposeNode();
            HarmonyWebViewPool.node = null;
        }
        else {
            bjccovms5u83t6.instrumentBranch(25, 0, false);
        }
        bjccovms5u83t6.instrumentRegion(25, 3);
        HarmonyWebViewPool.warmedUrl = '';
        HarmonyWebViewPool.inUse = false;
        HarmonyWebViewPool.uaSig = '';
        HarmonyWebViewPool.warmedInjectUA = false;
        HarmonyWebViewPool.warmedUaExtra = '';
    }
    // ArkTS 类禁止实例化
    private constructor() { bjccovms5u83t6.instrumentFunction(26); }
}
