const descrUtils = {

    showHideDescrAsNec: function showHideDescrAsNec() {
        log('showHideDescrAsNec: enter');

        const lensCount = showHideLensUtils.lensesToShowCount();
        // get the number of lens cards that will be displayed

        log('showHideDescrAsNec: lensCount=' + lensCount);

        const show = (lensCount > 1);
        // only show the description if at least two lenses are visible

        const divJQ = $('#divDescr');
        // get a jQuery object for the description

        const displayValue = divJQ.css("display");
        // get the value of the CSS display attribute

        if (show) {
            // if the description should be visible

            if (displayValue === 'none') {

                divJQ.css('display', 'block');
                // show it
            }
        }
        else {
            // if the description should be hidden

            if (displayValue === 'block') {

                divJQ.css('display', 'none');
                // show it
            }
        }

        log('showHideDescrAsNec: leave');
    },

    updateDescriptionAsync: async function updateDescriptionAsync(lensCount) {
        // update the description of the found lenses

        const logHeader = 'descrUtils.updateDescriptionAsync: ';

        log(logHeader + 'enter');
        log(logHeader + 'lensCount=' + lensCount);

        const foundAll = findWhichGroupStateUtils.getFindAll();
        // did we find all lenses?
        //  true => find all
        //  false => find bookmarked

        log(logHeader + 'foundAll=' + foundAll);

        var descrHTML = '';

        if (foundAll) {
            descrHTML = await this.getHTMLForDescrAsync(lensCount);
        }
        else {
            // if we found bookmarked lenses

            var theDescr = '';

            if (lensCount === 1) {
                // if precisely one

                theDescr = 'This is the lens you bookmarked.';
            }
            else {
                theDescr = 'These are the ' + lensCount + ' lenses you bookmarked.';
            }

            descrHTML = '<h6 id="myDescription" class="mt-3 mb-3">' + theDescr + '</h6>';
        }

        log(logHeader + 'descrHTML=' + descrHTML);

        $('#divDescr').html(descrHTML);

        log(logHeader + 'leave');
    },

    getHTMLForDescrAsync: async function getHTMLForDescrAsync(lensCount) {
        log('getHTMLForDescrAsync: enter');
        log('getHTMLForDescrAsync: lensCount= ' + lensCount);

        let theDescr = await this.getSearchResultsDescrAsync(lensCount);

        let theHTML = '<h6 id="myDescription" class="mt-3 mb-3">' + theDescr + '</h6>';

        log('getHTMLForDescrAsync: leave');

        return theHTML;
    },

    getSearchResultsDescrAsync: async function getSearchResultsDescrAsync(lensCount) {
        const logHeader = 'getSearchResultsDescr: ';
        log(logHeader + 'enter');
        log(logHeader + 'lensCount=' + lensCount);
        log(logHeader + 'enableLoginFeatures=' + enableLoginFeatures);

        var user = null;
        // default to the user not being authenticated

        if (enableLoginFeatures) {

            log(logHeader + 'login features are enabled, so we will call isUserAuthenticatedAsync');

            log(logHeader + 'atc authUtils.getUserAsync');

            user = await authUtils.getUserAsync();
            // get the current user or null if not found

            log(logHeader + 'bf authUtils.getUserAsync');
        }

        let prefix = '';

        if (user !== null) {
            // if we have a user object

            log(logHeader + 'we have a user object, so we will prepend a name to the description');

            const givenName = user.given_name;

            if (typeof givenName === 'undefined') {
                // if we don't have a given name
                // this may happen if the user authenticates via email

                let emailAddress = user.email;

                if (typeof emailAddress !== 'undefined') {

                    prefix = emailAddress + ', ';
                }
            }
            else {
                // if we have given name

                prefix = givenName + ', ';
            }
        }

        var sOrNot = 'es';

        if (lensCount === 1)
            sOrNot = '';

        var allOrNot = '';

        if (lensCount === 25)
            allOrNot = ' all ';

        var descr = prefix + 'Currently showing ' + allOrNot + lensCount + ' lens' + sOrNot + '.';

        log(logHeader + 'descr: ' + descr);
        log(logHeader + 'leave');

        return descr;
    }
};