products.ts 1007 Bytes
import { defineStore } from 'pinia'
import { store } from '..'
import type { ProductsDetailWithThingsModelType, Tsl } from '@/api/device/model'

interface ProductsStoreType {
  products: Record<string, ProductsDetailWithThingsModelType>
}

export const useProductsStore = defineStore('app-products', {
  state: (): ProductsStoreType => {
    return {
      products: {},
    }
  },
  actions: {
    setProducts(products: Record<string, ProductsDetailWithThingsModelType>) {
      this.products = products
    },

    getProductDetailById(id: string) {
      return this.products[id]
    },

    getObjectModelsById(id: string) {
      return this.products[id]
    },

    getObjectModelByIdWithIdentifier(id: string, identifier: string): Nullable<Tsl> {
      const product = this.getObjectModelsById(id)
      if (!product) return null
      return product.tsl.filter(item => item.identifier === identifier)[0]
    },
  },
})

export function useProductsStoreWithOut() {
  return useProductsStore(store)
}