import { getUrlParam } from '../../../global/helpers/athena-global-helper.js';
import PerformQuery from '../../../global/indexed-db/indexed-queries.js';

/**
 * Check if Init Visual Search is Active
 *
 * @param data
 * @returns {boolean}
 */
async function isVisualSearchInitCheck(data) {
    let isVisualSearch = false;
    if(data.hasOwnProperty('image')) {
        delete data['service'];
        delete data['q'];
        delete data['category'];
        delete data['level'];

        localStorage.removeItem('category');

        data['service'] = 'visual-similarity-search';
        data['image'] = (await PerformQuery.get(`${data['image']}`))?.image;

        isVisualSearch = true;
    }

    return isVisualSearch;
}

/**
 * Check if Visual Search Check
 * on filter, sorter and pagination
 *
 * @param data
 * @returns {boolean}
 */
async function isVisualSearchFilterCheck(data) {
    let image = getUrlParam('image');

    let isVisualSearch = false;
    if(image) {
        delete data['service'];
        delete data['q'];
        delete data['category'];
        delete data['level'];

        localStorage.removeItem('category');

        data['service'] = 'visual-similarity-search';
        data['image'] = (await PerformQuery.get(`${image}`))?.image;

        isVisualSearch = true;
    }

    return isVisualSearch;
}

/**
 * Check If Visual Similarity Search is Active
 *
 * @param data
 * @param init
 * @returns {boolean}
 */
function isVisualSearchActive(data, init = false) {
    return init ?
        isVisualSearchInitCheck(data) :
        isVisualSearchFilterCheck(data);
}

export default isVisualSearchActive;