<commit_before>import * as types from './mutation-types'
import { quickSearchByQuery } from '@vue-storefront/core/lib/search'
import { StorageManager } from '@vue-storefront/core/lib/storage-manager'
import AttributeState from '../../types/AttributeState'
import RootState from '@vue-storefront/core/types/RootState'
import { ActionTree } from 'vuex'
import config from 'config'
import { Logger } from '@vue-storefront/core/lib/logger'
import { entityKeyName } from '@vue-storefront/core/lib/store/entities'
import { prefetchCachedAttributes } from '../../helpers/prefetchCachedAttributes'
import areAttributesAlreadyLoaded from './../../helpers/areAttributesAlreadyLoaded'
import createAttributesListQuery from './../../helpers/createAttributesListQuery'
import reduceAttributesLists from './../../helpers/reduceAttributesLists'
import e from 'express'

const actions: ActionTree<AttributeState, RootState> = {
  async updateAttributes ({ commit, getters }, { attributes }) {
    const idsList = getters.getAttributeListById
    const codesList = getters.getAttributeListByCode

    for (let attr of attributes) {
      if (attr && !config.attributes.disablePersistentAttributesCache) {
        const attrCollection = StorageManager.get('attributes')

        try {
          await attrCollection.setItem(entityKeyName('attribute_code', attr.attribute_code.toLowerCase()), attr)
          await attrCollection.setItem(entityKeyName('attribute_id', attr.attribute_id.toString()), attr)
        } catch (e) {
          Logger.error(e, 'mutations')()
        }
      }
    }

    commit(types.ATTRIBUTE_UPD_ATTRIBUTES, reduceAttributesLists({ codesList, idsList, attributes }))
  },
  async loadCachedAttributes ({ dispatch }, { filterField, filterValues }) {
    if (!filterValues) {
      return
    }

    const attributes = await prefetchCachedAttributes(filterField, filterValues)

    if (attributes) {
      await dispatch('updateAttributes', { attributes })
    }
  },
  updateBlacklist ({ commit, getters }, { filterValues, filterField, attributes }) {
    if (attributes && filterValues.length > 0) {
      const foundValues = attributes.map(attr => attr[filterField])
      const toBlackList = filterValues.filter(ofv => !foundValues.includes(ofv) && !getters.getBlacklist.includes(ofv))
      commit(types.ATTRIBUTE_UPD_BLACKLIST, toBlackList)
    }
  },
  /**
   * Load attributes with specific codes
   * @param {Object} context
   * @param {Array} attrCodes attribute codes to load
   */
  async list ({ getters, dispatch }, { filterValues = null, filterField = 'attribute_code', only_user_defined = false, only_visible = false, size = 150, start = 0, includeFields = config.entities.optimize ? config.entities.attribute.includeFields : null }) {
    const blacklist = getters.getBlacklist
    const idsList = getters.getAttributeListById
    const codesList = getters.getAttributeListByCode
    const orgFilterValues = filterValues || []

    await dispatch('loadCachedAttributes', { filterField, filterValues })

    if (areAttributesAlreadyLoaded({ filterValues, filterField, blacklist, idsList, codesList })) {
      Logger.info('Skipping attribute load - attributes already loaded', 'attr', { orgFilterValues, filterField })()
      return { items: Object.values(codesList) }
    }

    const query = createAttributesListQuery({
      filterValues,
      filterField,
      onlyDefinedByUser: only_user_defined,
      onlyVisible: only_visible
    })
    const resp = await quickSearchByQuery({ entityType: 'attribute', query, includeFields, start, size })
    const attributes = resp && orgFilterValues.length > 0 ? resp.items : null

    dispatch('updateBlacklist', { filterValues, filterField, attributes })
    await dispatch('updateAttributes', { attributes })

    return resp
  },
  async loadAttributesFromProducts (context, { products }) {
    const attributes = products
      .filter(product => product.attributes_metadata)
      .map(product => product.attributes_metadata)
      .reduce((prev, curr) => ([ ...prev, ...curr ]), [])
      .reduce((prev, curr) => ({
        attrHashByCode: {
          ...(prev.attrHashByCode || {}),
          [curr.attribute_code]: curr
        },
        attrHashById: {
          ...(prev.attrHashById || {}),
          [curr.attribute_id]: curr
        }
      }), { attrHashByCode: {}, attrHashById: {} })<commit_msg>attributes<commit_after>import * as types from './mutation-types'
import { quickSearchByQuery } from '@vue-storefront/core/lib/search'
import { StorageManager } from '@vue-storefront/core/lib/storage-manager'
import AttributeState from '../../types/AttributeState'
import RootState from '@vue-storefront/core/types/RootState'
import { ActionTree } from 'vuex'
import config from 'config'
import { Logger } from '@vue-storefront/core/lib/logger'
import uniqBy from 'lodash-es/uniqBy'
import { entityKeyName } from '@vue-storefront/core/lib/store/entities'
import { prefetchCachedAttributes } from '../../helpers/prefetchCachedAttributes'
import areAttributesAlreadyLoaded from './../../helpers/areAttributesAlreadyLoaded'
import createAttributesListQuery from './../../helpers/createAttributesListQuery'
import reduceAttributesLists from './../../helpers/reduceAttributesLists'
import e from 'express'

const actions: ActionTree<AttributeState, RootState> = {
  async updateAttributes ({ commit, getters }, { attributes }) {
    const idsList = getters.getAttributeListById
    const codesList = getters.getAttributeListByCode

    for (let attr of attributes) {
      if (attr && !config.attributes.disablePersistentAttributesCache) {
        const attrCollection = StorageManager.get('attributes')

        try {
          await attrCollection.setItem(entityKeyName('attribute_code', attr.attribute_code.toLowerCase()), attr)
          await attrCollection.setItem(entityKeyName('attribute_id', attr.attribute_id.toString()), attr)
        } catch (e) {
          Logger.error(e, 'mutations')()
        }
      }
    }

    commit(types.ATTRIBUTE_UPD_ATTRIBUTES, reduceAttributesLists({ codesList, idsList, attributes }))
  },
  async loadCachedAttributes ({ dispatch }, { filterField, filterValues }) {
    if (!filterValues) {
      return
    }

    const attributes = await prefetchCachedAttributes(filterField, filterValues)

    if (attributes) {
      await dispatch('updateAttributes', { attributes })
    }
  },
  updateBlacklist ({ commit, getters }, { filterValues, filterField, attributes }) {
    if (attributes && filterValues.length > 0) {
      const foundValues = attributes.map(attr => attr[filterField])
      const toBlackList = filterValues.filter(ofv => !foundValues.includes(ofv) && !getters.getBlacklist.includes(ofv))
      commit(types.ATTRIBUTE_UPD_BLACKLIST, toBlackList)
    }
  },
  /**
   * Load attributes with specific codes
   * @param {Object} context
   * @param {Array} attrCodes attribute codes to load
   */
  async list ({ getters, dispatch }, { filterValues = null, filterField = 'attribute_code', only_user_defined = false, only_visible = false, size = 150, start = 0, includeFields = config.entities.optimize ? config.entities.attribute.includeFields : null }) {
    const blacklist = getters.getBlacklist
    const idsList = getters.getAttributeListById
    const codesList = getters.getAttributeListByCode
    const orgFilterValues = filterValues || []

    await dispatch('loadCachedAttributes', { filterField, filterValues })

    if (areAttributesAlreadyLoaded({ filterValues, filterField, blacklist, idsList, codesList })) {
      Logger.info('Skipping attribute load - attributes already loaded', 'attr', { orgFilterValues, filterField })()
      return { items: Object.values(codesList) }
    }

    const query = createAttributesListQuery({
      filterValues,
      filterField,
      onlyDefinedByUser: only_user_defined,
      onlyVisible: only_visible
    })
    const resp = await quickSearchByQuery({ entityType: 'attribute', query, includeFields, start, size })
    const attributes = resp && orgFilterValues.length > 0 ? resp.items : null

    dispatch('updateBlacklist', { filterValues, filterField, attributes })
    await dispatch('updateAttributes', { attributes })

    return resp
  },
  async loadAttributesFromProducts (context, { products }) {
    const attributes = products
      .filter(product => product.attributes_metadata)
      .map(product => product.attributes_metadata)
      .reduce((prev, curr) => ([ ...prev, ...curr ]), [])
      .reduce((prev, curr) => {
        const attribute = prev.find(a => a.attribute_id === curr.attribute_id && a.options)

        if (attribute) {
          return prev.map(attr => {
            if (attr.attribute_id === curr.attribute_id) {
              return {
                ...attr,
                options: uniqBy([...attr.options, ...curr.options], (obj) => `${obj.label}_${obj.value}`)
              }
            }

            return attr
          })
        }

        return [...prev, curr]
      }, [])
      .reduce((prev, curr) => ({
        attrHashByCode: {
          ...(prev.attrHashByCode || {}),
          [curr.attribute_code]: curr
        },
        attrHashById: {
          ...(prev.attrHashById || {}),
          [curr.attribute_id]: curr
        }
      }), { attrHashByCode: {}, attrHashById: {} })
{'fraction_non_alphanumeric': 0.12372681462420929, 'fraction_numerical': 0.001286587327114828, 'mean_word_length': 3.4717162032598274, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2486691', 'n_tokens_mistral': 2518, 'n_tokens_neox': 2525, 'n_words': 575}