WRMCB=function(e){var c=console;if(c&&c.log&&c.error){c.log('Error running batched script.');c.error(e);}}
;
try {
/* module-key = 'com.tempoplugin.tempo-teams:tempo-teams-atl-resources', location = 'com/tempoplugin/team/ui/customfield/edit_team_customfield.js' */
require(['jquery', 'tempo/teams/ajs-meta', 'tempo/teams/ajs-single-select', 'tempo/teams/ajs-item-descriptor',
        'tempo/teams/ajs-group-descriptor', 'tempo/teams/context-path', 'tempo/teams/jira-events'],
    function ($, Meta, SingleSelect, ItemDescriptor, GroupDescriptor, contextPath, Events) {
        addTeamPicker();
        JIRA.bind(Events.NEW_CONTENT_ADDED, function(e, context) {
            addTeamPicker();
        });
        function addTeamPicker() {
            var teamPicker;
            var inheritItemDescriptor = new ItemDescriptor({
                value: "INHERIT",
                label: "\u0423\u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u043e \u043e\u0442 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f",
                allowDuplicate: false
            });

            function selectInheritedValue() {
                if (teamPicker) {
                    teamPicker.setSelection(inheritItemDescriptor);
                }
            }

            function hasNoTeamFieldValue(teamFieldValue) {
                return !teamFieldValue || !teamFieldValue.length || !teamFieldValue[0];
            }

            _.each($("select.tempo-team-customfield"), function (field) {
                var $jiraForm = $('form.aui[name=jiraform]');
                var projectPickerVal;
                if($jiraForm.length) {
                    projectPickerVal = $('.issue-setup-fields input.project-field').val();
                } else {
                    projectPickerVal = Meta.get('projectId');
                }

                var epicCfId = Meta.get("epicLinkCFId");
                if (epicCfId) {
                    var $epicLinkField = $("#" + epicCfId + "-single-select");
                    $epicLinkField.on('DOMNodeInserted change', function (e) {
                        var teamFieldValue = $('#' + Meta.get("teamCFId")).val();
                        if(e.type === 'change' && hasNoTeamFieldValue(teamFieldValue)){
                            // Epic Link field was changed, and no team had been assigned, i.e. Create issue button from top nav
                            selectInheritedValue();
                        } else if ($epicLinkField.hasClass('aui-disabled') && $("#" + epicCfId + " option").length) {
                            // Epic Link pre-selected, i.e. Create Issue in Epic from Scrum board
                            selectInheritedValue();
                        }
                    });
                }

                var selectedProject = projectPickerVal ? parseFloat(projectPickerVal) : null;
                var renderedField = $("#" + field.id + '-single-select');
                if (renderedField.length === 0) {
                    var data = {};
                    teamPicker = new SingleSelect({
                        element: field,
                        showDropdownButton: true,
                        removeOnUnSelect: false,
                        submitInputVal: true,
                        noQueryNoRequest: false,
                        ajaxOptions: {
                            url: contextPath() + "/rest/tempo-teams/2/picker/team?expand=projects",
                            query: true, // keep going back to the sever for each keystroke
                            minQueryLength: 1,
                            data: data,
                            formatResponse: function (response) {
                                var ret = [];
                                $(response).each(function (i, suggestions) {
                                    var teamsInScope = new GroupDescriptor({
                                        weight: i, // order or groups in suggestions dropdown
                                        id: "tempo-team-search-in-scope",
                                        label: "\u041a\u043e\u043c\u0430\u043d\u0434\u044b, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u043c",
                                        replace: false
                                    });
                                    var otherTeams = new GroupDescriptor({
                                        weight: i + 1, // order or groups in suggestions dropdown
                                        id: "tempo-team-search",
                                        label: "\u0414\u0440\u0443\u0433\u0438\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b",
                                        replace: false
                                    });
                                    var emptyOptions = new GroupDescriptor({
                                        weight: i + 1, // order or groups in suggestions dropdown
                                        id: "tempo-team-empty-options",
                                        replace: false
                                    });
                                    if (Meta.get('isNoneOptionAvailable')) {
                                      emptyOptions.addItem(new ItemDescriptor({
                                        value: "",
                                        label: "\u041f\u0443\u0441\u0442\u043e",
                                        allowDuplicate: false
                                      }));
                                    }
                                    // N.b. we need this second epicCfId check for when you open dialog to create issue in epic
                                    if(Meta.get('isInheritedOptionAvailable') || (epicCfId && $("#" + epicCfId + "-field").val())) {
                                        emptyOptions.addItem(inheritItemDescriptor);
                                    }
                                    $(suggestions.objects).each(function () {
                                        if (_.contains(this.subIds, selectedProject)) { //check if team id is in linked teams
                                            teamsInScope.addItem(new ItemDescriptor({
                                                value: "" + this.id, // must be a string for proper selection
                                                label: this.name, // title of lozenge
                                                allowDuplicate: false
                                            }));
                                        } else {
                                            otherTeams.addItem(new ItemDescriptor({
                                                value: "" + this.id, // must be a string for proper selection
                                                label: this.name, // title of lozenge
                                                allowDuplicate: false
                                            }));
                                        }
                                    });
                                    var noLinkedProject = teamsInScope.properties.items.length == 0;
                                    ret.push(emptyOptions);
                                    if (noLinkedProject) {
                                        // remove dropdown section header
                                        otherTeams.properties.label = "";
                                        ret.push(otherTeams);
                                    } else {
                                        ret.push(teamsInScope);
                                        ret.push(otherTeams);
                                    }
                                });
                                return ret;
                            }
                        }
                    });
                }
            });
        }

    });

}catch(e){WRMCB(e)};