import { ref } from 'vue' import { defineStore } from 'pinia' import restApi from '@/api/restApi' import { l10nObj } from '@/helper' import { ICollectionsStore, ICollections, IProductsStore, IProducts, ICollectionProducts, IUsersStore, IUsers } from '@/types' export const useCollectionsStore = defineStore('collectionsStore', (): ICollectionsStore => { const collections = ref({ total: 0, currentPage: 1, data: [], }) const getCollections = async (params: Record = {}) => { const apiParams = { posts_per_page: 12, ...params } const baseURL = `${l10nObj().restApiUrl}/collections` const response = await restApi().get('', { baseURL, params: apiParams }) collections.value = response.data return response.data } const getCollection = async (id: string | number) => { const baseURL = `${l10nObj().restApiUrl}/collection/${id}` const response = await restApi().get('', { baseURL }) return response.data } const saveCollection = async (collection: any) => { const response = await restApi().post('', collection) return response.data } const deleteCollections = async (ids: any[]) => { const baseURL = `${l10nObj().restApiUrl}` const response = await restApi().delete('', { baseURL, data: { ids: ids } }) return response.data } const deleteCollection = async (id: string | number) => { const data = { action: 'remove_wishlist', ids: [id], } const response = await restApi().post('', data) return response.data } const deleteCollectionItems = async (collectionId: string | number, productId: string | number, variantId: string | number) => { const data = { action: 'remove_wishlist_item', collection_id: collectionId, product_id: productId, variation_id: variantId, } const response = await restApi().post('', data) return response.data } const getWishlists = async (params: Record = {}) => { const apiParams = { posts_per_page: 10, ...params } const baseURL = `${l10nObj().restApiUrl}/wishlists` const response = await restApi().get('', { baseURL, params: apiParams }) collections.value = response.data return response.data } const getWishlistItems = async (params: Record = {}) => { const apiParams = { ...params } const baseURL = `${l10nObj().restApiUrl}/wishlist-items` const response = await restApi().get('', { baseURL, params: apiParams }) return response.data } const updateWishlistItem = async (data: Record) => { const response = await restApi().post('', data) return response.data } const getGuestWishlistProducts = async (products: string[]) => { const baseURL = `${l10nObj().restApiUrl}/guest-wishlist-products` const response = await restApi().post('', { products }, { baseURL }) return response.data } const updateWishlistItemPosition = async (data: Record) => { const response = await restApi().post('', data) return response.data } return { collections, getCollections, getCollection, saveCollection, deleteCollections, deleteCollection, getWishlists, getWishlistItems, deleteCollectionItems, updateWishlistItem, getGuestWishlistProducts, updateWishlistItemPosition, } }) export const useProductsStore = defineStore('productsStore', (): IProductsStore => { const productLists = ref({ data: [], }) const collectionProducts = ref({ total: 0, currentPage: 1, data: [], }) let getProductsSeq = 0 const getProducts = async (params: Record = {}) => { const seq = ++getProductsSeq const apiParams = { per_page: 20, stwlite_extended_search: 1, ...params, } const baseURL = `${l10nObj().productsApiUrl}` const response = await restApi().get('', { baseURL, params: apiParams }) const data = response.data || [] if (seq === getProductsSeq) { productLists.value = { data } } return data } const getCollectionProducts = async (params: Record = {}) => { const apiParams = { ...params } const baseURL = `${l10nObj().restApiUrl}/items` const response = await restApi().get('', { baseURL, params: apiParams }) collectionProducts.value = response.data return response.data } return { productLists, getProducts, collectionProducts, getCollectionProducts, } }) export const useUsersStore = defineStore('usersStore', (): IUsersStore => { const users = ref({ data: [], }) let getUsersSeq = 0 const getUsers = async (params: Record = {}) => { const seq = ++getUsersSeq const apiParams = { per_page: 20, ...params, } const baseURL = `${l10nObj().usersApiUrl}` const response = await restApi().get('', { baseURL, params: apiParams }) const data = response.data || [] if (seq === getUsersSeq) { users.value = { data } } return data } return { users, getUsers, } })