package xtools.boot.core.utils;

import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import xtools.core.ArrUtils;
import xtools.core.CollectionUtils;

import java.util.List;
import java.util.Set;

/**
 * <p>Title : PathPatternUtils</p>
 * <p>Description : PathPatternUtils</p>
 * <p>DevelopTools : Idea_x64_v2026.1</p>
 * <p>DevelopSystem : macOS Sequoia 15.7.5</p>
 * <p>Company : org.xujun</p>
 *
 * @author : XuJun
 * @version : 5.0.0
 * @date : 2026/2/8 09:05
 */
public class PathPatternUtils {

    /**
     * 路径匹配
     *
     * @param pathPatterns 路径规则
     * @param path         路径
     * @return 结果
     */
    public static boolean match(String[] pathPatterns, String path) {
        if (ArrUtils.isEmpty(pathPatterns)) {
            return false;
        }
        PathMatcher pm = new AntPathMatcher();
        for (String pathPattern : pathPatterns) {
            if (pm.match(pathPattern, path)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 路径匹配
     *
     * @param pathPatterns 路径规则
     * @param path         路径
     * @return 结果
     */
    public static boolean match(List<String> pathPatterns, String path) {
        if (CollectionUtils.isEmpty(pathPatterns)) {
            return false;
        }
        PathMatcher pm = new AntPathMatcher();
        for (String pathPattern : pathPatterns) {
            if (pm.match(pathPattern, path)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 路径匹配
     *
     * @param pathPatterns 路径规则
     * @param path         路径
     * @return 结果
     */
    public static boolean match(Set<String> pathPatterns, String path) {
        if (CollectionUtils.isEmpty(pathPatterns)) {
            return false;
        }
        PathMatcher pm = new AntPathMatcher();
        for (String pathPattern : pathPatterns) {
            if (pm.match(pathPattern, path)) {
                return true;
            }
        }
        return false;
    }

}
