import Foundation
import Combine

class NewHomeTabViewModel: ObservableObject {
    @Published var posts: [Post] = []
    @Published var isLoading = true
    @Published var isRefreshing = false
    
    private var hasLoadedData = false

    func fetchDataIfNeeded() {
        guard !hasLoadedData else { return }
        refreshData()
    }
    
    func refreshData() {
        DispatchQueue.main.async {
            self.isRefreshing = true
        }
        
        PostService.shared.fetchAllPosts { [weak self] fetchedPosts in
            guard let self = self else { return }
            
            DispatchQueue.main.async {
                self.posts = fetchedPosts
                self.isLoading = false
                self.hasLoadedData = true
                self.isRefreshing = false
            }
        }
    }
}
