<template>
  <div>
    <Spin
      v-if="loading"
      size="large"
    />
    <Alert
      v-if="error"
      type="error"
    >
      {{ error }}
    </Alert>
  </div>
</template>

<script>
import { getShopeeShopByUsername } from '@/api/shopee/client/shop';

export default {
  name: 'ShopeeSpyShopByUsername',
  data() {
    return {
      loading: true,
      error: '',
    };
  },
  async created() {
    const username = this.$route.query.username;

    if (!username) {
      this.loading = false;
      this.error = 'Missing username';
      return;
    }

    const shopData = await getShopeeShopByUsername(username).catch(() => null);

    if (shopData && shopData.shopid) {
      this.$router.replace({
        path: `/shopee/spy/shop/${shopData.shopid}`,
      });
      return;
    }

    this.loading = false;
    this.error = `Không tìm thấy shop: ${username}`;
  },
};
</script>
