define('bitbucket/internal/feature/jira/jira-issues-dialog', [
    'jira-integration/jira-issues-dialog',
    'jira-integration/jira-triggers',
    'jquery',
    'lodash',
    'bitbucket/internal/util/ajax',
    'bitbucket/internal/util/events',
    'bitbucket/internal/util/oauth'
], function(
    JiraIssuesDialog,
    jiraTriggers,
    $,
    _,
    ajax,
    events,
    oAuth
) {

    var oAuthTriggerClass = 'jira-ual-authenticate';
    oAuth.interceptLinks('.' + oAuthTriggerClass);

    function StashJiraIssuesDialog(options) {
        _.bindAll(this, 'show', 'hide', 'showDialog', 'closeDialog', 'destroy');
        options = $.extend({}, this.defaults, options);
        if (options.triggerSelector) {
            this._triggerInfo = jiraTriggers.init({
                selector : options.triggerSelector,
                headerTitleFn : typeof options.headerTitle === 'function' ?
                    options.headerTitle :
                    undefined
            }, options);
            this._dialog = this._triggerInfo.getDialog();
        } else {
            this._dialog = new JiraIssuesDialog(options);
        }
        events.on('bitbucket.internal.util.oauth.authorizationSucceeded', this._dialog.onAuthorizationSucceeded);
    }

    StashJiraIssuesDialog.prototype.defaults = $.extend({}, JiraIssuesDialog.prototype.defaults, {
        oauthTriggerClass : oAuthTriggerClass,
        ajax : function(options) {
            return ajax.rest($.extend({
                statusCode : { // these are handled by plugin
                    200: false,
                    500 : false
                }
            }, options));
        }
    });

    StashJiraIssuesDialog.prototype.destroy = function() {
        events.off('bitbucket.internal.util.oauth.authorizationSucceeded', this._dialog.onAuthorizationSucceeded);
        this._dialog.destroy();
        this._dialog = null;
        if (this._triggerInfo) {
            this._triggerInfo.unbind();
            this._triggerInfo = null;
        }
    };

    StashJiraIssuesDialog.prototype.showDialog = StashJiraIssuesDialog.prototype.show = function() {
        this._dialog.show();
    };

    StashJiraIssuesDialog.prototype.closeDialog = StashJiraIssuesDialog.prototype.hide = function() {
        this._dialog.hide();
    };

    StashJiraIssuesDialog.prototype.setIssueKeys = function(keys) {
        this._dialog.setIssueKeys(keys);
    };

    return StashJiraIssuesDialog;
});