(function ($) {
    function getRecommendationSets($control) {
        var encoded = $control.attr('data-recommendation-sets');
        if (!encoded) {
            return {};
        }
        try {
            return JSON.parse(encoded);
        } catch (error) {
            return {};
        }
    }

    function renderRecommendations($control, type) {
        var recommendations = getRecommendationSets($control)[type];
        var $list = $control.closest('.brapf-one-click-card').find('.brapf-one-click-recommendations');
        if (!$.isArray(recommendations) || !$list.length) {
            return;
        }
        $list.empty();
        $.each(recommendations, function (index, label) {
            $('<li>').append(
                $('<i>', {'class': 'fa fa-check', 'aria-hidden': 'true'}),
                document.createTextNode(label)
            ).appendTo($list);
        });
    }

    $(document).on('change', '.brapf-store-type-input', function () {
        var $input = $(this);
        var $control = $input.closest('.brapf-store-type-control');
        renderRecommendations($control, $input.val());
    });
})(jQuery);
