import CoreFoundation import Foundation import ParcelUI @MainActor struct TourLaunchPolicy { let isFirstRun: Bool let allowsLoginDefault: Bool let allowsPresentation: Bool let holdsAtLaunch: Bool let requiresOffAtLaunch: Bool let shouldPresentAtLaunch: Bool let progressFailure: (any Error)? let recoveryRequired: Bool let recoveryNotice: String? init( defaults: UserDefaults, configFileExists: Bool, realitySession: Bool = false, firstRunRealitySession: Bool = false ) { allowsPresentation = !realitySession || firstRunRealitySession let pending: Bool let completed: Bool let readable: Bool do { let saved = try TourSetupProgressStore(defaults: defaults).load() readable = true completed = saved?.completed ?? defaults.bool(forKey: "guidedTour.v2.completed") recoveryRequired = allowsPresentation && saved?.recovery == .recoveryRequired recoveryNotice = allowsPresentation ? saved?.recoveryNotice : nil pending = saved?.shouldPresentAtLaunch ?? !defaults.bool(forKey: "guidedTour.v2.completed") let explicitAutomatic = if let value = defaults.object(forKey: "automaticTilingEnabled") as? NSNumber { CFGetTypeID(value) == CFBooleanGetTypeID() && value.boolValue } else { false } requiresOffAtLaunch = allowsPresentation && (saved.map { $0.recovery != .none || ($0.disposition == .deferred && !explicitAutomatic) } ?? false) progressFailure = nil } catch { readable = false completed = false recoveryRequired = allowsPresentation recoveryNotice = nil pending = true requiresOffAtLaunch = allowsPresentation progressFailure = error } isFirstRun = allowsPresentation && readable && !completed && !defaults.bool(forKey: "onboardingCompleted") && !configFileExists allowsLoginDefault = isFirstRun && defaults.object(forKey: "launchAtLogin") == nil shouldPresentAtLaunch = allowsPresentation && pending holdsAtLaunch = shouldPresentAtLaunch || recoveryRequired } static func requestsFirstRunReality(environment: [String: String]) -> Bool { #if DEBUG && PARCEL_REALITY environment["PARCEL_REALITY"] == "1" && environment["PARCEL_E2E"] == "1" && environment["PARCEL_E2E_REAL_APPS"] == "1" && environment["PARCEL_E2E_FIRST_RUN"] == "1" && environment["PARCEL_E2E_TOKEN"].flatMap(UUID.init(uuidString:)) != nil && environment["PARCEL_REALITY_ISOLATION_TOKEN"].flatMap(UUID.init(uuidString:)) != nil #else false #endif } static func isRealitySession(environment: [String: String]) -> Bool { #if DEBUG && PARCEL_REALITY environment["PARCEL_REALITY"] == "1" #else false #endif } }