const bookmarkUIUtils = {

    MIN_WIDTH_BOOKMARK_FEATURES: 736, // if viewport is narrower than this value, don't show bookmark features

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

        const show = (viewWidth >= this.MIN_WIDTH_BOOKMARK_FEATURES);
        // show bookmark features if viewport width is at least this minimum value        

        log('showBookmarks: show=' + show);
        log('showBookmarks: enter');

        return show;
    },

    handleBookmarkClickAsync: async function handleBookmarkClickAsync(elemId, reloadURL) {
        // handle a click in a bookmark image
        // reloadURL: the URL of the parent page which we may need to reload
        //
        // reads the scoped variable
        //  viewWide

        const logHdr = 'bookmarkUIUtils.handleBookmarkClickAsync: ';
        log(logHdr + 'enter');
        log(logHdr + 'elemId=' + elemId);
        log(logHdr + 'reloadURL=' + reloadURL);
        log(logHdr + 'viewWide=' + viewWide);

        displayCurrentLensesCache.clear();
        // clear the cache of HTML to display for the current set of lenses

        const findAll = findWhichGroupStateUtils.getFindAll();
        // are we currently all lenses? (vs bookmarked)

        log(logHdr + 'findAll=' + findAll);

        const imgElem = $('#' + elemId);
        // get the clicked bookmark img element

        const src = imgElem.attr('src');
        // get the image URL

        log(logHdr + 'src=' + src);

        const filePath = urlUtils.getFilePath(src);
        // get the file path from the URL

        var emptyWasClicked = ((filePath === '/bookmarkempty-sm.png') || (filePath === '/bookmarkempty.png'));
        // was an empty bookmark clicked?

        log(logHdr + 'emptyWasClicked=' + emptyWasClicked);

        if (true) {
            // toggle the bookmark image between filled and empty

            if (viewWide) {
                if (emptyWasClicked) {
                    // if the bookmark was empty

                    log(logHdr + 'view is wide, bookmark was empty, so we show the filled bookmark');

                    imgElem.attr('src', urlUtils.getUrl('/images/bookmarkfilled-sm.png'));
                    // set the image source to the filled bookmark
                }
                else {
                    // if the bookmark was full

                    log(logHdr + 'view is wide, bookmark was full, so we show the empty bookmark');

                    imgElem.attr('src', urlUtils.getUrl('/images/bookmarkempty-sm.png'));
                    // set the image source to the empty bookmark
                }
            }
            else {
                if (emptyWasClicked) {
                    // if the bookmark was empty

                    log(logHdr + 'view is narrow, bookmark was empty, so we show the filled bookmark');

                    imgElem.attr('src', urlUtils.getUrl('/images/bookmarkfilled.png'));
                    // set the image source to the filled bookmark
                }
                else {
                    // if the bookmark was full

                    log(logHdr + 'view is narrow, bookmark was full, so we show the empty bookmark');

                    imgElem.attr('src', urlUtils.getUrl('/images/bookmarkempty.png'));
                    // set the image source to the empty bookmark
                }
            }
        }

        const lensId = lensUIUtils.getLensIdFromElemId(elemId);
        // extract the lens id from the image element id

        if (emptyWasClicked) {
            // if the user clicked an empty bookmark

            log(logHdr + 'add lens id ' + lensId + ' to the stored list of bookmarks');

            bookmarkStorageUtils.addToLensIds(lensId);
            // add the lens id to the list of bookmarked lens ids

            const bkCount = bookmarkStorageUtils.count();
            // how many lenses are bookmarked now?

            log(logHdr + 'bkCount=' + bkCount);

            if (bkCount === 1) {
                // if this is the first bookmark clicked

                findWhichGroupUtils.showGroup();
                // show the "find which" group
            }
        }
        else {
            // if a filled bookmark was clicked

            log(logHdr + 'remove lens id ' + lensId + ' from the stored list of bookmarks');

            bookmarkStorageUtils.removeFromLensIds(lensId);
            // remove the lens id from the list

            if (!findAll) {
                // if we are currently finding bookmarked lenses

                log(logHdr + 'we are currently finding bookmarked lenses, so we will update the UI');

                const lensDivId = elemIdUtils.getElemId(lensId, elemIdUtils.CARD_PREFIX, undefined);
                // get the id of the parent div for the lens

                $('#' + lensDivId).hide();
                // hide the div for the lens

                log(logHdr + 'hid the parent div for the lens');

                const showCount = showHideLensUtils.lensesToShowCount();
                // how many lenses will be displayed?

                log(logHdr + 'showCount=' + showCount);

                if (showCount < 1) {
                    // if there are no lenses visible and we are finding bookmarked lenses

                    log(logHdr + 'there are no lenses visible and we are finding bookmarked lenses, we we will switch to finding all lenses and reload the page');

                    findWhichGroupStateUtils.setFindAll(true);
                    // swith from finding bookmarked to finding all lenses

                    log(logHdr + 'about to set document.location to ' + reloadURL);

                    document.location = reloadURL;
                    // reload the page

                    log(logHdr + 'after setting document.location to ' + reloadURL);
                    log(logHdr + 'leave');

                    return;
                }

                var byAlpha = displayByStateUtils.getDisplayAlpha();

                log(logHdr + 'byAlpha=' + byAlpha);

                if (!byAlpha) {
                    // if we are displaying lenses by group and the user just cleared a bookmark

                    const lensDTO = lensUtils.getLensById(lensId);

                    if (lensDTO !== null) {
                        // if we got the lens DTO

                        const catId = lensDTO.CategoryId;

                        const lensesToDisplay = lensUtils.getLensDTOsToDisplay();
                        // get the DTOs of the lenses to display

                        const lensCount = lensesToDisplay.length;

                        const findResult = lensesToDisplay.find(x => { return x.CategoryId === catId });
                        // find the first lens in the set of lenses to display with the same category as the lens we just hid
                        // or undefined if not found

                        if (typeof findResult === "undefined") {
                            // if the lens we hid was the last of its category displayed

                            const catDivId = 'divCategory' + catId;
                            // get the id of the parent div

                            $('#' + catDivId).remove();
                            // remove the div from the page
                            // it's a bootstrap div, so hiding it won't work

                            const catCaptionId = 'catName' + catId;
                            // get the name of the category name element

                            $('#' + catCaptionId).hide();
                            // hide the category name

                            const hrId = 'hrCategory' + catId;
                            // get the name of the <hr> below the category

                            $('#' + hrId).hide();
                            // hide the horizontal rule

                            if (lensCount < 1) {
                                $('#hrAboveFirstCat').hide();
                                // hide the top horizontal rule
                            }
                        }
                    }
                }
            }
        }

        log(logHdr + 'atc uiUtils.handleLensCountChangedAsync');

        await uiUtils.handleLensCountChangedAsync();

        log(logHdr + 'bf uiUtils.handleLensCountChangedAsync');

        log(logHdr + 'leave');
    }
};