import { athenaConfig, athenaConfigOptions } from '../../global/athena-config.js';
import { handleRenderSearchSuggestions } from '../../global/helpers/athena-global-helper.js';

/**
 * Changing Autocomplete Position
 *
 * @param type
 */
function changeAutocompletePosition(type) {
    const athenaAutocomplete = document.querySelector('.athena-autocomplete');

    if(type === 'autocomplete'){
        athenaAutocomplete.removeAttribute('data-call-type');
        athenaAutocomplete.setAttribute('data-call-type', 'autocomplete');
    }
    if(type === 'first-click'){
        athenaAutocomplete.removeAttribute('data-call-type');
        athenaAutocomplete.setAttribute('data-call-type', 'first-click');
    }

    if(type === 'autocomplete' && athenaConfig.layoutType === 'athena-l3') {
        athenaAutocomplete?.classList.remove('athena-grid');
        athenaAutocomplete?.classList.add('athena-l3');
        athenaAutocomplete?.classList.remove('athena-l3-first-click');
        athenaAutocomplete?.classList.add('athena-l3-autocomplete');
    }

    if(type === 'first-click' && athenaConfig.layoutType === 'athena-l3') {
        athenaAutocomplete?.classList.remove('athena-l3');
        athenaAutocomplete?.classList.add('athena-grid');
        athenaAutocomplete?.classList.remove('athena-l3-autocomplete');
        athenaAutocomplete?.classList.add('athena-l3-first-click');
    }
}

/**
 * Show No Result Function
 *
 * @param value
 */
function showNoResult(value) {
    document.querySelector('.athena-no-result')?.classList.add('athena-helper-show');
    document.querySelector('#athena-no-result-query').innerHTML = `"${value}"`;
}

/**
 * Hide No Result Function
 */
function hideNoResult() {
    document.querySelector('.athena-no-result')?.classList.remove('athena-helper-show');
}

/**
 * Show Autocomplete Function
 */
function showAutocompleteBox() {
    athenaConfigOptions.searchForm?.classList.add('arrow');
    document.querySelector('.athena-autocomplete')?.classList.add('athena-helper-show');
}

/**
 * function to hide Close button
 */
function removeCloseButton(){
    const searchContainer = document.querySelector('.close-search-btn')
    searchContainer?.classList.remove('delete-search-terms');
}

/**
 * Show Autocomplete Function
 */
function hideAutocompleteBox() {
    athenaConfigOptions.searchForm?.classList.remove('arrow');
    document.querySelector('.athena-autocomplete')?.classList.remove('athena-helper-show');
}

/**
 * Set True Autocomplete To Upper and LowerCase
 *
 * @param value
 */
function trueAutocompleteUpperLowerCaseSet(value) {
    const formElement = document.querySelector('.search-form #search_mini_form.arrow');
    const trueAutocompleteValue = formElement.getAttribute('true-autocomplete-text');

    value && value === value.toUpperCase() ?
        formElement.setAttribute('true-autocomplete-text', trueAutocompleteValue?.toUpperCase() || '') :
        formElement.setAttribute('true-autocomplete-text', trueAutocompleteValue?.toLowerCase() || '');
}

export {
    changeAutocompletePosition,
    showNoResult,
    hideNoResult,
    showAutocompleteBox,
    hideAutocompleteBox,
    trueAutocompleteUpperLowerCaseSet,
    removeCloseButton
}