//
//  ErrorView.swift
//  Pods
//
//  Created by sophia on 13/1/26.
//

import SwiftUI

public struct ErrorView: View {
    public var errorMessage: String
    public var errorSpacing: Bool
    public var hintText: String
    
    public init(
        errorMessage: String = "",
        errorSpacing: Bool = false,
        hintText: String = ""
    ) {
        self.errorMessage = errorMessage
        self.errorSpacing = errorSpacing
        self.hintText = hintText
    }
    
    public var body: some View {
        if !errorMessage.isEmpty || !hintText.isEmpty {
            HStack(alignment: .top, spacing: 4) {
                Icon(source: "ic_error", size: scaleSize(16), color: displayColor())
                MomoText(
                    displayText(),
                    typography: .descriptionDefaultRegular,
                    color: displayColor()
                )
            }
            .padding(.top, Spacing.XS)
        } else if errorSpacing {
            Spacer()
                .padding(.top, Spacing.XS)
                .frame(height: 18)
        }
    }
    
    // MARK: - Helpers
    
    private func displayColor() -> Color {
        !errorMessage.isEmpty ? Colors.red03 : Colors.black12
    }
    
    private func displayText() -> String {
        if !errorMessage.isEmpty {
            return errorMessage
        }
        return hintText.isEmpty ? "Không thể chỉnh sửa" : hintText
    }
}
