package com.seer.teach.filter.auth;

import org.springframework.http.server.PathContainer;
import org.springframework.stereotype.Component;
import org.springframework.web.util.pattern.PathPattern;
import org.springframework.web.util.pattern.PathPatternParser;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Component
public class PathPatternMatcher {

    private final PathPatternParser parser = new PathPatternParser();
    private final ConcurrentMap<String, PathPattern> patternCache = new ConcurrentHashMap<>();

    public boolean matches(String pattern, String path) {
        PathPattern compiled = patternCache.computeIfAbsent(pattern,pathPattern -> parser.parse(pathPattern));
        return compiled.matches(PathContainer.parsePath(path));
    }

    public void clearPatternCache() {
        patternCache.clear();
    }
}