
// const vueAppMagenotApiUrl = process.env.VUE_APP_MAGENTO_API_URL;
//const vueAppCCUrl = process.env.VUE_APP_CC_URL;
// const retailorId = process.env.VUE_APP_RETAILOR_ID;
// const vueAppNodeslsApiUrl = process.env.VUE_APP_NODESLS_API_URL;
// const assetNodeslsUrl = process.env.VUE_APP_ASSETS_NODESLS_URL;

/* eslint-disable no-alert, no-console ,  no-debugger */
export const doIncreaseCountsForFilter = ({ product, availableFilter }) => {
    
    const pv = product[availableFilter.prop]
    if(pv)
    {
        const existingCount = availableFilter.counts.find(element => element.value == pv)
        if (existingCount !== undefined && existingCount != null) {
            existingCount.count = existingCount.count + 1
        }
        else {
            availableFilter.counts.push({value:pv, count:1})
        }
    }

}

export const doCheckProductForEnabledFilters = ({ product, enabledFilters }) => {
    if(enabledFilters.length){
    var filteredCount = 0;
    for (var f = 0; f < enabledFilters.length; f++) {
        const pv = product[enabledFilters[f].prop]
        if (enabledFilters[f].label == 'Category') {
            filteredCount++;
            continue // for now
        }
        if (pv == enabledFilters[f].value) // hardcoded to equals 
        {
            filteredCount++;
            //break
        }
    }
    return (filteredCount==enabledFilters.length?true:false);
    }
    else 
        return true;
}

export const doCountProductAttributes = ({ product, availableFilters, attributesMap }) => {
    
  //  console.log('attributesMap:', attributesMap)
    for (var a = 0; a < attributesMap.length; a++) {
        var avAttribute = attributesMap[a]
        var avFilter = availableFilters.find(element => element.label == avAttribute.label)
        if (avFilter === undefined || avFilter == null) {
            avFilter = {
                label: avAttribute.label,
                prop: avAttribute.prop,
                counts: [],
                options: avAttribute.options
            }
            availableFilters.push(avFilter)
        }
        doIncreaseCountsForFilter( { product, availableFilter: avFilter })
    }
}

export const doFilterItems = ({ items, enabledFilters, availableFilters, attributesMap }) => {
    const matchingItems = []
    for (var i = 0; i < items.length; i++) {
        var product = items[i]
        const isValid = doCheckProductForEnabledFilters({ product, enabledFilters })
        if (isValid && product.per =='No') { 
            doCountProductAttributes({ product, availableFilters, attributesMap })

            matchingItems.push(product)
        }        
    }
    return matchingItems
}
