define('bitbucket/internal/feature/all-approvers/pull-request', [
    'bitbucket/internal/feature/pull-request/can-merge',
    'bitbucket/internal/model/page-state',
    'bitbucket/internal/util/events',
    'exports'
], function (
    canMerge,
    pageState,
    events,
    exports
) {

    'use strict';

    function _approvalHandler(data) {
        var pullRequest = pageState.getPullRequest();
        if (!pullRequest.get('closed')) {
            canMerge(pullRequest);
        }
    }

    exports.onReady = function() {
        // Use client side page data to hook up events if the all approvers required merge check is enabled for this repository
        _PageDataPlugin.ready('com.atlassian.bitbucket.server.bitbucket-bundled-hooks:all-approvers-merge-check-context-provider',
            'bitbucket.layout.pullRequest', function (data) {
            if (data && data.allApproversRequiredMergeCheck.enabled) {
                events.on('bitbucket.internal.widget.approve-button.added', _approvalHandler);
                events.on('bitbucket.internal.widget.approve-button.removed', _approvalHandler);
                events.on('bitbucket.internal.feature.pullRequest.self.removed', _approvalHandler);
            }
        });
    };
});

jQuery(document).ready(function() {
    require('bitbucket/internal/feature/all-approvers/pull-request').onReady();
});
