import Foundation

public struct CaptionUserMention: Codable, Equatable, Hashable {
    public let start: Int
    public let end: Int
    public let handle: String
    public let displayName: String?

    public init(start: Int, end: Int, handle: String, displayName: String? = nil) {
        self.start = start
        self.end = end
        self.handle = handle
        self.displayName = displayName
    }

    init(_ remote: Mention) throws {
        guard remote.start >= 0,
              remote.end >= 0,
              !remote.handle.isEmpty
        else {
            throw CaptionUserMentionError.decodingFailed
        }
        self.start = remote.start
        self.end = remote.end
        self.handle = remote.handle
        self.displayName = remote.displayName
    }

    enum CaptionUserMentionError: Error {
        case decodingFailed
    }
}
