//
// Copyright © 2024 Nevis Security AG. All rights reserved.
//

import NevisMobileAuthentication

struct TypedAccount: Account {
	// MARK: Properties

	let username: Username
	let server: any Server

	// MARK: Coding

	enum CodingKeys: CodingKey {
		case username
		case server
	}

	init(from decoder: any Decoder) throws {
		let values = try decoder.container(keyedBy: CodingKeys.self)
		self.username = try values.decode(Username.self, forKey: .username)
		self.server = try values.decode(TypedServer.self, forKey: .server)
	}

	func encode(to encoder: Encoder) throws {
		var container = encoder.container(keyedBy: CodingKeys.self)
		try container.encode(username, forKey: .username)
		try container.encode(server, forKey: .server)
	}
}

// MARK: - Equatable

extension TypedAccount: Equatable {
	public static func == (lhs: TypedAccount, rhs: TypedAccount) -> Bool {
		lhs.username == rhs.username
	}

	static func < (lhs: TypedAccount, rhs: TypedAccount) -> Bool {
		lhs.username < rhs.username
	}
}

// MARK: - Hashable

/// :nodoc:
extension TypedAccount: Hashable {
	func hash(into hasher: inout Hasher) {
		hasher.combine(username)
	}
}
