//
//  TSMarkDownTool.swift
//  AIEmoji
//
//  Created by 100Years on 2025/2/23.
//

private let kLineSpacing = 6.0
private let KFont = UIFont.font(size: 16)
private let kSendColor = "#111111".uiColor


private let paragraphStyle:NSMutableParagraphStyle = {
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = kLineSpacing
    paragraphStyle.paragraphSpacing = 1
    return paragraphStyle
}()

///发送的Attributed
func kMDSendAttributedString(text:String) -> NSAttributedString{
    let attributes : [NSAttributedString.Key : Any] = [
        .font: KFont,
        .foregroundColor:kSendColor,
        .paragraphStyle:paragraphStyle
    ]
    
    let attributedString = NSMutableAttributedString(string: text, attributes: attributes)
    return attributedString
}


private var md: MarkdownParser = {
    let parser = MarkdownParser(font: KFont,color: .white.withAlphaComponent(0.9))//普通文本颜色和字体大小
    parser.enabledElements = .disabledAutomaticLink
    

    //代码
    let code = TSCustomMarkdownCode()
    code.textBackgroundColor = .clear
    let codeColor = UIColor.white
    code.color = codeColor
    code.textHighlightColor = codeColor
    code.font = .font(size: 16,weight: UIFont.Weight.medium)
    
    //粗体
    parser.bold.font = .font(size: 18)
    parser.bold.color = UIColor.white
    
    //标题
    parser.header.maxLevel = 3
    parser.header.font = .font(size: 22)
    parser.header.fontIncrease = 2
    parser.header.color = UIColor.white
    


    
    parser.replaceDefaultElement(parser.code, with: code)
    return parser
}()

//ai 对话的 字符串转成 markdown NSAttributedString
func kMDAttributedString(text:String) -> NSAttributedString{
    let attributedString = NSMutableAttributedString(attributedString: md.parse(text))
    attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
    return attributedString
}



open class TSCustomMarkdownCode: MarkdownCode {
  fileprivate static let regex = "(^|\\s?)(\\`{1,3})(.+?)(\\2)"
  open override var regex: String {
    return TSCustomMarkdownCode.regex
  }
}
























































//import SwiftyMarkdown
//private var md: SwiftyMarkdown = {
//
//    var characterRules = SwiftyMarkdown.characterRules
//    characterRules.append(
//        CharacterRule(
//            primaryTag: CharacterRuleTag(tag: "```", type: .repeating),
//            otherTags: [],
//            styles: [1 : CharacterStyle.code],
//            shouldCancelRemainingRules: true,
//            balancedTags: true
//        )
//    )
//    SwiftyMarkdown.characterRules = characterRules
//
//    let md = SwiftyMarkdown(string: "")
//    md.setFontColorForAllStyles(with: .white)
////    md.code.color = .red
//
////    let characterRules = [
////        CharacterRule(primaryTag: CharacterRuleTag(tag: "```", type: .repeating), otherTags: [], styles: [1 : CharacterStyle.code], shouldCancelRemainingRules: true, balancedTags: true)
////    ]
////    let processor = SwiftyTokeniser( with : characterRules)
////    let string = "The elf will speak now: %Here is my elf speaking%"
////    let tokens = processor.process(string)
//
//
//
//    return md
//}()
//
////字符串转成 markdown NSAttributedString
//func kMDAttributedString(text:String) -> NSAttributedString{
//    return md.attributedString(from: text)
//}
