import Foundation import Observation @MainActor @Observable final class ResumeAction { var connection: ResumeConnection? var readFailed = false static func refused(_ error: Error) -> Bool { guard let error = error as? EquipoAPIError else { return false } return error.endsSession || error.status.map { (400..<500).contains($0) && $0 != 429 } == true } func load(client: EquipoClient, step: ThinStep) async { if let connection, StoppedWorkActions.connection(for: step, in: [connection]) == nil { self.connection = nil } do { let list = try await client.resumeConnections() readFailed = false connection = StoppedWorkActions.connection(for: step, in: list.items ?? []) } catch { readFailed = true if Self.refused(error) { connection = nil } } } func perform(client: EquipoClient, step: ThinStep) async -> String? { guard let connection else { return nil } do { try await client.resumeConnection(connection); return nil } catch { // A stale pause revision must never be offered for the next tap. if Self.refused(error) { self.connection = nil } await load(client: client, step: step) return EquipoStrings.text("card.failed") } } }