import { defineStore } from "pinia";

export const useStore = defineStore("maindev", {
  state: () => ({
    viewport: undefined,
    login: undefined,
    token: undefined,
    common: undefined,
    settings: [],
    showmodal: undefined,
    snackbar: undefined,
    productdocument: undefined,
    applicationstatus: undefined,
    applicationproduct: undefined,
    country: undefined,
    lang: "vi",
    branch: {},
    rights: [],
    product: [],
  }),
  actions: {
    commit(name, data) {
      // console.trace("commit", name, data);
      this[name] = data;
    },

    updateProduct(products) {
      this.product = products;
    },

    updateSingleProduct(updatedProduct) {
      const index = this.product.findIndex((p) => p.id === updatedProduct.id);
      if (index !== -1) {
        this.product[index] = updatedProduct;
      } else {
        this.product.push(updatedProduct);
      }
    },

    removeProduct(productId) {
      this.product = this.product.filter((p) => p.id !== productId);
    },
  },

  persist: {
    pick: ["token", "login", "lang"],
    storage: piniaPluginPersistedstate.localStorage(),
  },
});
