public class NotifyAction {

    public let identifier: Swift.String

    public var onAction: ((RealityKit.Entity?) -> Swift.Void)?

    private weak var root: RealityKit.Entity?

    fileprivate init(identifier: Swift.String, root: RealityKit.Entity?) {
        self.identifier = identifier
        self.root = root

        Foundation.NotificationCenter.default.addObserver(self, selector: #selector(actionDidFire(notification:)), name: Foundation.NSNotification.Name(rawValue: "RealityKit.NotifyAction"), object: nil)
    }

    deinit {
        Foundation.NotificationCenter.default.removeObserver(self, name: Foundation.NSNotification.Name(rawValue: "RealityKit.NotifyAction"), object: nil)
    }

    @objc
    private func actionDidFire(notification: Foundation.Notification) {
        guard let onAction = onAction else {
            return
        }

        guard let userInfo = notification.userInfo as? [Swift.String: Any] else {
            return
        }

        guard let scene = userInfo["RealityKit.NotifyAction.Scene"] as? RealityKit.Scene,
            root?.scene == scene else {
                return
        }

        guard let identifier = userInfo["RealityKit.NotifyAction.Identifier"] as? Swift.String,
            identifier == self.identifier else {
                return
        }

        let entity = userInfo["RealityKit.NotifyAction.Entity"] as? RealityKit.Entity

        onAction(entity)
    }

}
