import SwiftUI

public extension Font {
    static func appFont(size: CGFloat) -> Font {
        let defaultScreenSize: CGFloat = 375
        let maxFontScale: CGFloat = 1.5
        let maxDeviceScale: CGFloat = 5
        
        let deviceWidth = UIScreen.main.bounds.width
        let deviceScale = deviceWidth / defaultScreenSize
        
        let defaultFont = UIFont.systemFont(ofSize: UIFont.labelFontSize)
        let scaledFont = UIFontMetrics.default.scaledFont(for: defaultFont)
        let fontScale = scaledFont.pointSize / defaultFont.pointSize

        var fontSize = size

        if deviceScale > 1 {
            fontSize = min(fontSize * deviceScale, fontSize + maxDeviceScale)
        }

        if fontScale > 1 {
            fontSize = min(fontSize * fontScale, fontSize * maxFontScale)
        }

        return Font.system(size: fontSize)
    }
    
    // New supported typography styles
    static let headline_default_bold = appFont(size: 24).weight(.bold)
    static let header_m_bold = appFont(size: 18).weight(.bold)
    static let header_default_bold = appFont(size: 16).weight(.bold)
    static let header_default_semibold = appFont(size: 16).weight(.semibold)
    static let header_s_semibold = appFont(size: 14).weight(.semibold)
    static let header_xs_semibold = appFont(size: 12).weight(.semibold)
    static let body_default_regular = appFont(size: 14).weight(.regular)
    static let description_default_regular = appFont(size: 12).weight(.regular)
    static let description_xs_regular = appFont(size: 10).weight(.regular)
    static let label_default_medium = appFont(size: 14).weight(.medium)
    static let label_s_medium = appFont(size: 12).weight(.medium)
    static let label_xs_medium = appFont(size: 10).weight(.medium)
    static let action_default_bold = appFont(size: 16).weight(.bold)
    static let action_s_bold = appFont(size: 14).weight(.bold)
    static let action_xs_bold = appFont(size: 12).weight(.bold)
    static let action_xxs_bold = appFont(size: 10).weight(.bold)
    
    // Legacy styles
    static let headline_default = appFont(size: 28).weight(.semibold)
    static let headline_s = appFont(size: 24).weight(.semibold)
    static let headline_l = appFont(size: 32).weight(.semibold)
    static let headline_xl = appFont(size: 36).weight(.semibold)
    static let title_default = appFont(size: 20).weight(.bold)
    static let title_xs = appFont(size: 16).weight(.bold)
    static let title_s = appFont(size: 18).weight(.bold)
    static let header_default = appFont(size: 15).weight(.semibold)
    static let header_xs = appFont(size: 12).weight(.semibold)
    static let header_s = appFont(size: 14).weight(.semibold)
    static let paragraph_default = appFont(size: 15).weight(.regular)
    static let paragraph_bold = appFont(size: 15).weight(.bold)
    static let description_default = appFont(size: 14).weight(.regular)
    static let description_xs = appFont(size: 10).weight(.regular)
    static let description_s = appFont(size: 12).weight(.regular)
    static let label_default = appFont(size: 14).weight(.medium)
    static let label_xxs = appFont(size: 8).weight(.medium)
    static let label_xs = appFont(size: 10).weight(.medium)
    static let label_s = appFont(size: 12).weight(.medium)
    static let action_default = appFont(size: 16).weight(.bold)
    static let action_xxs = appFont(size: 10).weight(.bold)
    static let action_xs = appFont(size: 12).weight(.bold)
    static let action_s = appFont(size: 15).weight(.bold)
    
    // deprecated
    static let h1 = appFont(size: 36).weight(.semibold) //headline_xl
    static let h2 = appFont(size: 32).weight(.semibold)
    static let h3 = appFont(size: 28).weight(.semibold)
    static let h4 = appFont(size: 24).weight(.semibold)
    static let title1 = appFont(size: 20).weight(.bold)
    static let title2 = appFont(size: 18).weight(.bold)
    static let title3 = appFont(size: 16).weight(.bold)
    static let headerText1 = appFont(size: 15).weight(.semibold) //header_default
    static let headerText2 = appFont(size: 14).weight(.semibold)
    static let headerText3 = appFont(size: 12).weight(.semibold)
    static let paragraph = appFont(size: 15).weight(.regular) //paragraph_default
    static let description1 = appFont(size: 14).weight(.regular)
    static let description2 = appFont(size: 12).weight(.regular)
    static let description3 = appFont(size: 10).weight(.regular)
    static let label1 = appFont(size: 14).weight(.medium)
    static let label2 = appFont(size: 12).weight(.medium)
    static let label3 = appFont(size: 10).weight(.medium)
    static let label4 = appFont(size: 8).weight(.medium)
    static let action1 = appFont(size: 16).weight(.bold)
    static let action2 = appFont(size: 15).weight(.bold)
    static let action3 = appFont(size: 12).weight(.bold)
    static let action4 = appFont(size: 10).weight(.bold)
}
