'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var reportStoreService = require('../../../../services/reportStoreService.js');
var logger = require('../../../../utils/logger.js');

class BookmarkService extends reportStoreService.ReportStoreService {
    enabled;
    constructor(reportDataStore, serviceFeatureConfig) {
        super(reportDataStore);
        this.enabled = Boolean(serviceFeatureConfig.bookmarking);
        if (!this.enabled)
            logger.default.info(`Bookmarking: disabled `);
    }
    async getAllBookmarks(userId) {
        if (!this.enabled)
            return [];
        const userConfig = await this.getState(userId);
        return userConfig.bookmarks;
    }
    async addBookmark(userId, reportId, id, type, automatic) {
        if (!this.enabled)
            return;
        const userConfig = await this.getState(userId);
        if (!this.isBookmarkedCheck(userConfig, id, reportId)) {
            userConfig.bookmarks.unshift({ reportId, id, type, automatic });
        }
        await this.saveState(userId, userConfig);
    }
    async removeBookmark(userId, id, reportId) {
        if (!this.enabled)
            return;
        const userConfig = await this.getState(userId);
        const index = userConfig.bookmarks.findIndex((bookmark) => {
            const bmVarId = bookmark.id || bookmark.variantId;
            return bmVarId === id && bookmark.reportId === reportId;
        });
        if (index >= 0) {
            userConfig.bookmarks.splice(index, 1);
        }
        await this.saveState(userId, userConfig);
    }
    isBookmarked = async (id, reportId, userId) => {
        if (!this.enabled)
            return false;
        const userConfig = await this.getState(userId);
        const isBookmarked = this.isBookmarkedCheck(userConfig, id, reportId);
        let bookmark;
        if (isBookmarked) {
            bookmark = this.getBookmark(userConfig, id, reportId);
        }
        return bookmark?.automatic ? undefined : isBookmarked;
    };
    isBookmarkedCheck = (userConfig, id, reportId) => {
        return userConfig.bookmarks.some((bookmark) => {
            const bmVarId = bookmark.id;
            return bmVarId === id && bookmark.reportId === reportId;
        });
    };
    getBookmark = (userConfig, id, reportId) => {
        return userConfig.bookmarks.find((bookmark) => {
            const bmVarId = bookmark.id;
            return bmVarId === id && bookmark.reportId === reportId;
        });
    };
    async createBookMarkButtonHtml({ userConfig, reportId, id, csrfToken, ctxId, reportType, isMissing, endpoint, }) {
        let linkText = 'Add bookmark';
        let automatic = false;
        let linkType = 'add';
        if (userConfig?.bookmarks) {
            const bookmark = this.getBookmark(userConfig, id, reportId);
            if (bookmark) {
                linkType = 'remove';
                linkText = 'Remove bookmark';
                automatic = Boolean(bookmark.automatic);
            }
        }
        const classes = 'govuk-link govuk-link--no-visited-state dpr-bookmark-link dpr-user-list-action';
        const linkId = `${reportId}-${id}-${ctxId}`;
        const bookmarkLinkHtml = `<a href="#" 
      class="${classes}"
      id="${linkId}"
      data-dpr-module="bookmark-button"
      data-id="${id}" 
      data-report-id="${reportId}" 
      data-report-type="${reportType}" 
      data-endpoint="${endpoint}"  
      data-csrf-token="${csrfToken}" 
      data-link-type="${linkType}"
    >${linkText}</a>`;
        return automatic || isMissing ? '' : bookmarkLinkHtml;
    }
}

exports.BookmarkService = BookmarkService;
exports.default = BookmarkService;
//# sourceMappingURL=service.js.map
