package com.youchain.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

@Component
@Slf4j
public class OptionalUtils {

    public static String getTrimmedString(Map<String, Object> map, String key, String defaultValue) {
        return Optional.ofNullable(map.get(key))
                .map(Object::toString)
                .map(String::trim)
                .orElse(defaultValue);
    }

    public static Integer getTrimmedInteger(Map<String, Object> map, String key, int defaultValue) {
        return Optional.ofNullable(map.get(key))
                .map(Object::toString)
                .map(String::trim)
                .map(Integer::parseInt)
                .orElse(defaultValue);
    }

    public static boolean getBooleanValue(Map<String, Object> map, String key, boolean defaultValue) {
        return Optional.ofNullable(map.get(key))
                .map(Object::toString)
                .map(String::trim)
                .map(s -> "1".equals(s) || "true".equalsIgnoreCase(s))
                .orElse(defaultValue);
    }


}
