package com.atlassian.stash.internal.plugin.hooks.allapprovers;

import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.plugin.PluginParseException;
import com.atlassian.plugin.web.ContextProvider;
import com.google.common.collect.ImmutableMap;

import java.util.Map;

public class AllApproversMergeCheckContextProvider implements ContextProvider {

    private static final String REPOSITORY = "repository";
    private final AllApproversMergeCheckConfig mergeCheckConfig;

    public AllApproversMergeCheckContextProvider(AllApproversMergeCheckConfig mergeCheckConfig) {
        this.mergeCheckConfig = mergeCheckConfig;
    }

    @Override
    public void init(Map<String, String> context) throws PluginParseException {
    }

    @Override
    public Map<String, Object> getContextMap(Map<String, Object> context) {
        boolean mergeCheckEnabled = false;

        Object repository = context.get(REPOSITORY);
        if (repository instanceof Repository) {
            mergeCheckEnabled = mergeCheckConfig.areAllApproversRequired((Repository) repository);
        }
        return ImmutableMap.of(
                "allApproversRequiredMergeCheck", ImmutableMap.of("enabled", mergeCheckEnabled)
        );
    }
}
