//
//  Account+Preferences.swift
//  Tusker
//
//  Created by Shadowfacts on 8/28/18.
//  Copyright © 2018 Shadowfacts. All rights reserved.
//

import Foundation
import Pachyderm

extension AccountMO {
    
    var displayOrUserName: String {
        if displayName.isEmpty {
            return username
        } else {
            return displayName
        }
    }
    
    var displayNameWithoutCustomEmoji: String {
        let stripped = stripCustomEmoji(from: displayName)
        if stripped.isEmpty {
            return username
        } else {
            return stripped
        }
    }
    
}

extension Account {
    var displayNameWithoutCustomEmoji: String {
        let stripped = stripCustomEmoji(from: displayName)
        if stripped.isEmpty {
            return username
        } else {
            return stripped
        }
    }
}
    
private let customEmojiRegex = try! NSRegularExpression(pattern: ":[a-zA-Z0-9_]+:", options: [])

private func stripCustomEmoji(from string: String) -> String {
    let range = NSRange(location: 0, length: string.utf16.count)
    return customEmojiRegex.stringByReplacingMatches(in: string, options: [], range: range, withTemplate: "")
}
