import SwiftUI
import Utilities

/// A TextEditor wrapper with character limiting and optional placeholder support.
/// Use this for multiline text input, especially when nested in ScrollViews.
public struct CharacterLimitedTextEditor: View {
    let placeholder: String
    @Binding var text: String
    let characterLimit: Int

    public init(
        placeholder: String = "",
        text: Binding<String>,
        characterLimit: Int
    ) {
        self.placeholder = placeholder
        self._text = text
        self.characterLimit = characterLimit
    }

    public var body: some View {
        ZStack(alignment: .topLeading) {
            TextEditor(text: $text.limit(characterLimit))
                .scrollContentBackground(.hidden)
                .scrollIndicators(.hidden)
                .padding(.horizontal, -5)
                .padding(.vertical, -8)

            if text.isEmpty && !placeholder.isEmpty {
                Text(placeholder)
                    .foregroundStyle(Color.SemanticV2.foregroundTertiary)
                    .allowsHitTesting(false)
            }
        }
    }
}
