import { defineComponent } from '@vue/composition-api';
import { renderProductDetail } from '@/adapter/shopee/render/index';
import { ShopeeSpyList, getRefId, createSpy } from '@/models/shopee/spy';
import { tableProductVariation } from './Table';

export default defineComponent({
  props: {
    productId: {
      type: Number,
      require: true,
    },
    shopId: {
      type: Number,
      require: true,
    },
  },
  data() {
    return {
      productData: null,
      isAddedSpy: false,
    };
  },
  methods: {
    async addTracker() {
      this.isAddedSpy = true;

      const spyProduct = await createSpy(this.productData);

      return spyProduct;
    },
  },
  async created() {
    this.productData = await this.ShopeeGetProduct({
      productId: this.productId,
      shopId: this.shopId,
    });

    if (this.productData) {
      this.SetAppTitle(this.productData.name);

      this.isAddedSpy = !!(await ShopeeSpyList.getId(getRefId(this.productData)));
    }
  },
  computed: {
    renderProduct() {
      if (!this.productData) {
        return {
          listVariation: [],
          metaData: {},
          shopData: {},
        };
      }

      return renderProductDetail(this.productData);
    },
    metaProduct() {
      return this.renderProduct.metaData;
    },
    metaShop() {
      return this.renderProduct.shopData;
    },
    tableVariation() {
      const { listVariation } = this.renderProduct;

      return {
        columns: tableProductVariation.columns,
        data: listVariation.sort(({ sold: sold1 }, { sold: sold2 }) => sold2 - sold1),
      };
    },
  },
});
