#if TUANJIE_1_8_OR_NEWER && PLATFORM_OPENHARMONY
using System.IO;
using System.Text;
using UnityEditor;
using UnityEditor.OpenHarmony;

// 仅 TUANJIE_1_8_OR_NEWER：用 CrashSightBridge.etslib 覆盖导出桥接文件，并把 GetFromGlobalThis 改为 classesLib。低版本不编译任何逻辑。
public sealed class CrashSightOpenHarmonyBridgePostExport : IPostGenerateOpenHarmonyProject
{
    public int callbackOrder => 900;
    public void OnPostGenerateOpenHarmonyProject(string exportRoot)
    {
        if (string.IsNullOrEmpty(exportRoot)) return;
        var tail = Path.Combine("src", "main", "ets", "CrashSight", "CrashSightBridge.ets");
        string target = null;
        foreach (var p in new[] { Path.Combine(exportRoot, "tuanjieLib", tail), Path.Combine(exportRoot, tail) })
            if (File.Exists(p)) { target = p; break; }
        if (target == null) return;
        var src = Path.Combine(UnityEngine.Application.dataPath, "Plugins", "OpenHarmony", "CrashSight", "CrashSightBridge.etslib");
        if (!File.Exists(src)) return;
        const string legacy = "import { GetFromGlobalThis } from 'crashsight/../../src/main/ets/common/GlobalThisUtil'";
        var t = File.ReadAllText(src, Encoding.UTF8);
        if (t.Contains(legacy)) t = t.Replace(legacy, "import { GetFromGlobalThis } from 'classesLib';");
        File.WriteAllText(target, t, new UTF8Encoding(false));
    }
}
#endif