import SwiftUI

public func attributedString(
    for string: String,
    typography: TypographyV1? = nil,
    foregroundColor: Color? = nil,
    emphasizedColor: Color? = nil,
    emphasizedTypography: TypographyV1? = nil
) -> AttributedString {
    var attributedString = (try? AttributedString(
        markdown: string,
        options: AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace)
    )) ?? AttributedString()

    if let font = typography?.font {
        attributedString.font = font
    }

    if let foregroundColor = foregroundColor {
        attributedString.foregroundColor = foregroundColor
    }
    var sourceAttributeContainer = AttributeContainer()
    sourceAttributeContainer.inlinePresentationIntent = .stronglyEmphasized
    var targetAttributeContainer = AttributeContainer()
    if let emphasizedColor = emphasizedColor {
        targetAttributeContainer.foregroundColor = emphasizedColor
    }
    if let emphasizedFont = emphasizedTypography?.font {
        targetAttributeContainer.font = emphasizedFont
    }
    attributedString.replaceAttributes(sourceAttributeContainer, with: targetAttributeContainer)

    return attributedString
}

public func attributedString(
    for string: String,
    typography: TypographyV1? = nil,
    typographyV1: TypographyV1? = nil,
    foregroundColor: Color? = nil,
    emphasizedColor: Color? = nil,
    emphasizedTypography: TypographyV1? = nil
) -> AttributedString {
    var attributedString = (try? AttributedString(
        markdown: string,
        options: AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace)
    )) ?? AttributedString()

    if let font = typography?.font {
        attributedString.font = font
    }

    if let font = typographyV1?.font {
        attributedString.font = font
    }

    if let foregroundColor = foregroundColor {
        attributedString.foregroundColor = foregroundColor
    }
    var sourceAttributeContainer = AttributeContainer()
    sourceAttributeContainer.inlinePresentationIntent = .stronglyEmphasized
    var targetAttributeContainer = AttributeContainer()
    if let emphasizedColor = emphasizedColor {
        targetAttributeContainer.foregroundColor = emphasizedColor
    }
    if let emphasizedFont = emphasizedTypography?.font {
        targetAttributeContainer.font = emphasizedFont
    }
    attributedString.replaceAttributes(sourceAttributeContainer, with: targetAttributeContainer)

    return attributedString
}
