//
//  MainTabBadgeViewModel.swift
//  suixinkan
//
//  Created by Codex on 2026/6/23.
//

import Foundation
import Observation

@MainActor
@Observable
/// 主 Tab 角标 ViewModel，负责获取订单 Tab 待核销数量。
final class MainTabBadgeViewModel {
    private(set) var pendingWriteOffCount: Int?

    /// 刷新待核销订单数量，缺少景区或接口失败时清空角标。
    func refreshPendingWriteOffCount(api: OrderServing, scenicId: Int?, storeId: Int?) async {
        guard let scenicId, scenicId > 0 else {
            pendingWriteOffCount = nil
            return
        }

        do {
            let result = try await api.writeOffList(scenicId: scenicId, storeId: storeId, page: 1, pageSize: 1)
            pendingWriteOffCount = min(result.total, 99)
        } catch {
            pendingWriteOffCount = nil
        }
    }
}
