package cclotus.ruoyi.ext.web.spa;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ConditionalOnProperty(prefix = "ext.web.spa", name = "enabled", havingValue = "true", matchIfMissing = true)
public class SpaWebConfig implements WebMvcConfigurer {

    @Autowired
    private GafSpaConfigure config;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (config == null || !config.isEnabled() || config.getIndexPaths() == null) {
            return;
        }

        config.getIndexPaths().forEach(path -> {
            if (!path.endsWith("/")) path += "/";
            registry.addResourceHandler(path, path + "**")
                    .addResourceLocations("file:public" + path,"file:dist/public" + path)
                    .resourceChain(true)
                    .addResolver(new SpaPathResourceResolver());
        });
    }
}