// (C) Copyright 2020 Hewlett-Packard Enterprise Company, L.P.
// NOTE: this is deprecated, switch to jquery.hpSelect.js
define(['jquery'], function() {
    (function($) {
        // jQuery plugin definition
        $.fn.hpSimpleSelect = function(options) {

            var instance;

            function hpSimpleSelect(elem) {

                var menu;

                function choose() {
                    var value = $(this).html();

                    // replace the text node, have to do it this way
                    $(elem).contents().eq(0).wrap(
                        "<p style='display:inline' />").parent()
                        .text(value).contents().unwrap();
                    $(elem).trigger('change', value);
                }

                function deactivate() {
                    $('body').unbind('mousedown', deactivate);
                    $('li', menu).unbind('mousedown', choose);
                    $(menu).hide();
                }

                function activate() {
                    $(menu).show();
                    $('li', menu).bind('mousedown', choose);
                    $('body').bind('mousedown', deactivate);
                }

                function initialize() {
                    $(elem).addClass('hp-simple-select');
                    menu = $('.hp-menu', $(elem));
                    if (menu.length === 0) {
                        menu = $('<ol class="hp-menu"></ol>');
                        $(elem).append(menu);
                    }
                    menu.hide();
                    menu.empty();

                    $.each(options, function (index, option) {
                        var opt = $('<li>' + option + '</li>');
                        if (option === $(elem).val()) {
                            opt.addClass('hp-selected');
                        }
                        menu.append(opt);
                    });

                    $(elem).bind('click', activate);
                }

                initialize();
            }

            // pluginify
            var ret;
            this.each(function() {
                var $elem = $(this);
                instance = new hpSimpleSelect($elem[0]);
                ret = ret ? ret.add($elem) : $elem;
            });
            return ret;
        };
    }(jQuery));
});
