import api from '../request'
// import config from "@/apis/config"
import { store } from "@/store"
import { mapSkuNames } from '@/utils'

export default {
    /**
     * 获取门店分类
     */
    getCat: async (storeID) => {
        try {
            let res = await api('v2/store/GetStoreCategoryMap', {
                data: {
                    storeID,
                    parentID: -1
                }
            })

            // #ifdef MP-KUAISHOU || MP-ALIPAY || MP-TOUTIAO
            // if (GetStoreCategoryMap === null) {
            //     data = config.JXCateroy
            // }
            // #endif

            return {
                catL1: mapCats(res, 1),
                catL2: mapCats(res, 2),
            }
        } catch (e) {
            console.error('getCat', e)
            throw (e)
        }
    },

    /**
    * 获取推荐分类
    */
    getTopCats: async (data) => {
        try {
            let res = await api('v2/store/sku/GetTopCategoriesByStoreIDs', {
                data
            })
            // let res = config.topCats

            if (res !== null) {
                return res.map(item => ({
                    id: item.id,
                    level: item.level,
                    name: item.name.substring(0, 4),
                    parentID: item.parentID,
                    // img: item.img.includes('https') ? item.img : item.img.substring(0, 4) + 's' + item.img.substring(4)
                    img: item.img
                }))
            } else {
                return []
            }
        } catch (e) {
            console.error('getTopCats', e)
            throw (e)
        }
    },

    /**
     * 获取分类商品
     */
    getStoresSkus: async (params = { pageSize: -1, offset: 0 }) => {
        let json = {
            storeIDs: params.storeID ? JSON.stringify([params.storeID]) : JSON.stringify(store.getters['indexPage/storeID']),
            isBySku:true,
            pageSize: params.pageSize ? params.pageSize : -1,
            offset: params.offset ? params.offset : 0
        }

        if (params.categoryID) {
            if (params.categoryID === 'act') {
                json.isAct = true
            } else if (params.categoryID === 'one') {
                json.skuIDs = JSON.stringify([Number(params.skuID)])
                json.pageSize = 1
            } else {
                json.categoryID = params.categoryID
            }
        }

        if (params.keyword) json.keyword = params.keyword
        if (params.skuIDs) json.skuIDs = JSON.stringify(params.skuIDs)
        if(!params.isBySku)  json.isBySku = false

        try {
            let res = await api('v2/store/sku/GetStoresSkus', {
                data: {
                    isFocus: true,
                    actVendorID: 9,
                    // isBySku: true,

                    fromStatus: 1,
                    toStatus: 1,
                    ...json
                }
            })
            return {
                totalCount: res.totalCount,
                skuNames: mapSkuNames(res.skuNames || [])
            }
        } catch (e) {
            console.error('getStoresSkus', e)
            throw (e)
        }
    },

    /**
     * GetCategories
     */
    getCategories: async (storeID) => {
        try {
            let res = await api('v2/store/sku/GetStoreCategories', {
                storeID
            })
            return res
        } catch (e) {
            console.error('getCategories', e)
            throw (e)
        }
    },

    /**
     * 获取热销商品
     */
    getTopGoods: async (storeID) => {
        try {
            let res = await api('v2/store/sku/GetTopSkusByStoreIDs', {
                data: {
                    storeIDs: JSON.stringify(storeID)
                }
            })
            return res
        } catch (e) {
            console.error('getTopGoods', e)
            throw (e)
        }
    },

}


// 过滤分类
const mapCats = (arr, level) => {
    return arr.filter(item => item.level === level && (item.isHidden === 1 || item.isHidden === 0)).map((item, index) => ({
        id: item.categoryID,
        level: item.level,
        name: item.name.substring(0, 4),
        parentID: item.parentID,
        index: index + 1
    }))
}