/*!
 * Custom elements for Nanopore-Digital Web applications
 */
'use strict';

let searchInsights;
const searchInsight = {
    /**
     * Initialise algolia search insights.
     * Will asynchronously import the search-insight library
     * @param appId
     * @param apiKey
     */
    init: async (appId, apiKey) => {
        if (searchInsights)
            return;
        searchInsights = (await Promise.resolve().then(function () { return require('./index-browser-3bwVgSYS.js'); })).default;
        searchInsights('init', {
            appId,
            apiKey,
        });
    },
    /**
     * Sends a `clickedObjectIDsAfterSearch` event to algolia search-insights.
     * `init(...)` Must have been called first.
     * @param params
     */
    sendClick: (params) => {
        if (!searchInsights) {
            console.error('You must call `init()` first');
            return;
        }
        searchInsights('clickedObjectIDsAfterSearch', { ...params }).catch((e) => {
            console.error('Error sending click event', e);
        });
    },
    /**
     * Associates algolia analytics with a user id
     * @param userToken
     */
    setUser: (userToken) => {
        if (!searchInsights) {
            console.error('You must call `init()` first');
            return;
        }
        searchInsights('setUserToken', userToken);
    },
};

exports.searchInsight = searchInsight;
