/* globals window, $$course_id, DiscussionUtil */
function DiscussionCourseBlock(runtime, element) {
    'use strict';
    var $discussionContainer = $("#discussion-container");
    DiscussionUtil.force_async = true;

    // stop history if it is already started.
    if (Backbone.History.started) {
        Backbone.history.stop();
    }
    function defineDiscussionClasses(define) {
        'use strict';
        var discussionClasses = [
            ['Content', 'common/js/discussion/content'],
            ['Discussion', 'common/js/discussion/discussion'],
            ['DiscussionModuleView', 'common/js/discussion/discussion_module_view'],
            ['DiscussionThreadView', 'common/js/discussion/views/discussion_thread_view'],
            ['DiscussionThreadListView', 'common/js/discussion/views/discussion_thread_list_view'],
            ['DiscussionThreadProfileView', 'common/js/discussion/views/discussion_thread_profile_view'],
            ['DiscussionTopicMenuView', 'common/js/discussion/views/discussion_topic_menu_view'],
            ['DiscussionUtil', 'common/js/discussion/utils'],
            ['DiscussionCourseSettings', 'common/js/discussion/models/discussion_course_settings'],
            ['DiscussionUser', 'common/js/discussion/models/discussion_user'],
            ['NewPostView', 'common/js/discussion/views/new_post_view']
        ];

        discussionClasses.forEach(function(discussionClass) {
            define(
                discussionClass[1],
                [],
                function() {
                    return window[discussionClass[0]];
                }
            );
        });
    };

    function initializeDiscussion() {

        if (typeof RequireJS === 'undefined') {
            var vendorScript = document.createElement("script");
            vendorScript.onload = function() {
                require.config({
                    baseUrl: "/static/",
                });

                var requireConfigScript = document.createElement("script");
                requireConfigScript.onload = function() {
                    defineDiscussionClasses(define);
                    initializeDiscussionBoardFactory(require)
                };
                requireConfigScript.src = "/static/lms/js/require-config.js";
                document.body.appendChild(requireConfigScript);
            };
            vendorScript.src = "/static/common/js/vendor/require.js";
            document.body.appendChild(vendorScript);

        } else {
            defineDiscussionClasses(RequireJS.define);
            initializeDiscussionBoardFactory(RequireJS.require);
        }
    };

    function initializeDiscussionBoardFactory(require) {
        require(
            ['discussion/js/discussion_board_factory'],
            function (DiscussionBoardFactory) {
                DiscussionBoardFactory({
                    courseId: $discussionContainer.data('courseId'),
                    course_name: $discussionContainer.data('course-name'),
                    $el: $discussionContainer,
                    user_info: $discussionContainer.data('userInfo'),
                    roles: $discussionContainer.data('roles'),
                    sort_preference: $discussionContainer.data('sortPreference'),
                    threads: $discussionContainer.data('threads'),
                    thread_pages: $discussionContainer.data('threadPages'),
                    content_info: $discussionContainer.data('contentInfo'),
                    course_settings: $discussionContainer.data('courseSettings')
                });
            }
        )
    };

    initializeDiscussion();
}

@@ -369,22 +369,16 @@
 Classes(
-define
 ) %7B%0A    
@@ -391,24 +391,384 @@
 se strict';%0A
+        window.RequireJS = window.RequireJS %7C%7C %7B%7D;%0A        RequireJS.requirejs = RequireJS.requirejs %7C%7C window.requirejs;%0A        RequireJS.require = RequireJS.require %7C%7C window.require;%0A        RequireJS.define = RequireJS.define %7C%7C window.define;%0A%0A        window.require = undefined;%0A        window.define = undefined;%0A        window.requirejs = undefined;%0A%0A
         var 
@@ -1784,24 +1784,34 @@
             
+RequireJS.
 define(%0A    
@@ -2471,22 +2471,16 @@
 Classes(
-define
 );%0A     
@@ -2527,23 +2527,16 @@
 Factory(
-require
 )%0A      
@@ -2882,32 +2882,16 @@
 Classes(
-RequireJS.define
 );%0A     
@@ -2930,33 +2930,16 @@
 Factory(
-RequireJS.require
 );%0A     
@@ -2997,23 +2997,16 @@
 Factory(
-require
 ) %7B%0A    
@@ -3005,24 +3005,34 @@
 ) %7B%0A        
+RequireJS.
 require(%0A   

fix loading of discussion on Prod like environment