import SwiftUI

public struct CharCount: View {
    private var text: String
    private let limit: Int
    private let background: AnyView?

    public init(text: String, limit: Int, background: Color? = nil) {
        self.text = text
        self.limit = limit
        self.background = AnyView(background ?? Color.clear)
    }

    public init<S: ShapeStyle>(text: String, limit: Int, background: S?) {
        self.text = text
        self.limit = limit
        if let bg = background {
            self.background = AnyView(Rectangle().fill(bg))
        } else {
            self.background = nil
        }
    }

    public var body: some View {
        Text(verbatim: "\(text.count) / \(limit)")
            .typographyV1(.body3)
            .monospacedDigit()
            .foregroundStyle(Color.SemanticV1.textBrand)
            .padding(.horizontal, background == nil ? 0 : 16)
            .padding(.vertical, background == nil ? 0 : 4)
            .background(background)
            .cornerRadius(24)
    }
}
