package com.atlassian.stash.internal.plugin;

import com.atlassian.plugin.spring.AvailableToPlugins;
import com.atlassian.plugin.util.PluginUtils;
import com.atlassian.plugin.webresource.DefaultResourceBatchingConfiguration;
import com.atlassian.plugin.webresource.ResourceBatchingConfiguration;
import com.google.common.collect.ImmutableList;
import org.springframework.stereotype.Component;

import java.util.List;

// Needs to be public for webresource-loader
@Component("resourceBatchingConfiguration")
@AvailableToPlugins(ResourceBatchingConfiguration.class)
public class StashResourceBatchingConfiguration extends DefaultResourceBatchingConfiguration {

    private static final String AUI_PLUGIN_KEY = "com.atlassian.auiplugin";
    private static final String AJS_MODULE_KEY = AUI_PLUGIN_KEY + ":ajs";
    private static final String PAGE_LAYOUT_MODULE_KEY = AUI_PLUGIN_KEY + ":aui-experimental-page-layout";
    private static final String STASH_WEB_PLUGIN_KEY = "com.atlassian.bitbucket.server.bitbucket-web";
    private static final String WEBRESOURCE_DATA_MODULE_KEY = "com.atlassian.plugins.atlassian-plugins-webresource-plugin:data";
    private static final String STASH_GLOBAL_MODULE_KEY = STASH_WEB_PLUGIN_KEY + ":global";
    private static final String STASH_REQUIRE_MODULE_KEY = STASH_WEB_PLUGIN_KEY + ":require-lite";

    private static final List<String> SUPER_BATCH_MODULES = ImmutableList.<String>builder()
            .add(STASH_REQUIRE_MODULE_KEY)
            .add(AJS_MODULE_KEY)
            .add(PAGE_LAYOUT_MODULE_KEY)
            .add(WEBRESOURCE_DATA_MODULE_KEY)
            .add(STASH_GLOBAL_MODULE_KEY)
            .build();

    @Override
    public List<String> getSuperBatchModuleCompleteKeys() {
        return SUPER_BATCH_MODULES;
    }

    @Override
    public boolean isSuperBatchingEnabled() {
        return true;
    }

    @Override
    public boolean isContextBatchingEnabled() {
        return true;
    }

    @Override
    public boolean isPluginWebResourceBatchingEnabled() {
        return true;
    }

    @Override
    public boolean isSourceMapEnabled() {
        return true;
    }

    @Override
    public boolean optimiseSourceMapsForDevelopment() {
        return Boolean.getBoolean(PluginUtils.ATLASSIAN_DEV_MODE);
    }
}
