//
//  BackNavigator.swift
//  suixinkanUITests
//
//  Created by Codex on 2026/6/26.
//

import XCTest

/// 导航返回封装。
enum BackNavigator {
    /// 通过导航栏返回按钮回到上一页。
    static func goBack(app: SuixinkanApp, file: StaticString = #filePath, line: UInt = #line) {
        let navigationBar = app.application.navigationBars.element(boundBy: 0)
        guard navigationBar.exists else {
            XCTFail("当前页面没有可返回的导航栏。", file: file, line: line)
            return
        }

        let backButton = navigationBar.buttons.element(boundBy: 0)
        XCTAssertTrue(backButton.waitForExistence(timeout: 4), "未找到返回按钮。", file: file, line: line)
        backButton.tap()
        app.waitForGlobalLoadingToDisappear()
    }

    /// 关闭仍使用自定义顶栏返回按钮的页面。
    static func dismissCustomTopBarIfNeeded(app: SuixinkanApp) {
        let backCandidates = app.application.buttons.matching(NSPredicate(format: "label CONTAINS 'chevron.left'"))
        if backCandidates.count > 0 {
            backCandidates.element(boundBy: 0).tap()
            app.waitForGlobalLoadingToDisappear()
        }
    }
}
