//
//	ZObjectRegistry.swift
//	ZKit
//
//	The MIT License (MIT)
//
//	Copyright (c) 2024 Electricwoods LLC, Kaz Yoshikawa.
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//

/// # ZObjectRegistry
///
/// A registry designed to facilitate the management of objects associated with UUIDs in SwiftUI applications.
///
/// Overview:
///
/// In SwiftUI development, scenarios often arise where passing objects between components, such as open windows, navigation stacks,
/// and navigation links, is necessary. ZObjectRegistry eliminates the need for creating classes solely for adopting Codable protocols in such cases.
/// Instead, developers can register objects with ZObjectRegistry, which then assigns a UUID to each object.
/// These UUIDs can then be passed between components, serving as references to the associated objects.

import Foundation

/// A registry for managing objects associated with UUIDs.
class ZObjectRegistry {
	/// The shared instance of `ZObjectRegistry`.
	static let shared = ZObjectRegistry()
	
	/// Internal storage for mapping UUIDs to objects.
	private var objectMap = NSMapTable<NSUUID, AnyObject>.strongToWeakObjects()
	
	/// Initializes a new instance of `ZObjectRegistry`.
	private init() {}
	
	/// Registers an object with the registry and associates it with a UUID.
	/// - Parameter object: The object to be registered.
	/// - Returns: The UUID associated with the registered object.
	func register<T: AnyObject>(_ object: T) -> UUID {
		let uuid = UUID()
		objectMap.setObject(object, forKey: uuid as NSUUID)
		return uuid
	}
	
	/// Retrieves the object associated with the specified UUID.
	/// - Parameter uuid: The UUID associated with the desired object.
	/// - Returns: The object associated with the specified UUID, or `nil` if not found.
	func object<T: AnyObject>(for uuid: UUID) -> T? {
		return objectMap.object(forKey: uuid as NSUUID) as? T
	}
	
	/// Unregisters the object associated with the specified UUID.
	/// - Parameter uuid: The UUID associated with the object to be unregistered.
	func unregister(uuid: UUID) {
		objectMap.removeObject(forKey: uuid as NSUUID)
	}
}