//
//  HelperFormat.swift
//  Gas Man
//
//  Created by Kameron Kenny on 3/19/25.
//

import SwiftUI

// Helper function to check if a string is properly formatted (e.g., "18.526")
func isProperlyFormatted(_ input: String) -> Bool {
    let pattern = #"^\d+\.\d{3}$"#
    return input.range(of: pattern, options: .regularExpression) != nil
}

// helper function to format input.
func formatInput(_ input: String) -> String {
    // Extract only numeric characters.
    let digitsOnly = input.filter { $0.isNumber }
    // If no digits, return empty.
    guard !digitsOnly.isEmpty else { return "" }
    // Convert to an integer to remove any leading zeros.
    let numberValue = Int(digitsOnly) ?? 0
    // Convert back to a string.
    let digits = String(numberValue)
    
    if digits.count > 3 {
        // Insert decimal point so that the last 3 digits are decimals.
        let integerPart = digits.dropLast(3)
        let decimalPart = digits.suffix(3)
        return "\(integerPart).\(decimalPart)"
    } else {
        // If fewer than 4 digits, pad with zeros on the left to 3 digits.
        let padded = String(repeating: "0", count: 3 - digits.count) + digits
        return "0." + padded
    }
}
