//
//  ActionCollapseNotificationHandler.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 7/18/22.
//  Copyright © 2022 Verizon Wireless. All rights reserved.
//

import Foundation
import MVMCore

/// Notifications that conform are collapsable and can collapse.
public protocol CollapsableNotificationProtocol {
    /// Collapses the notification.
    @MainActor
    func collapse()
}

/// Collapses the current notification if it can collapse, otherwise dismisses it.
open class ActionCollapseNotificationHandler: MVMCoreActionHandlerProtocol {
    required public init() {}
    
    open func execute(with model: ActionModelProtocol, delegateObject: DelegateObject?, additionalData: [AnyHashable : Any]?) async throws {
        guard let notification = await NotificationHandler.shared()?.getCurrentNotification() else { return }
        guard let notification = notification.0 as? CollapsableNotificationProtocol else {
            NotificationHandler.shared()?.hideNotification()
            return
        }
        await notification.collapse()
    }
}
