import { defineStore } from 'pinia'
import { Profile } from '/@/components/shopSelector/types'
import { request } from '../utils/service'
import { ref, Ref } from 'vue'
import { Session } from '/@/utils/storage';


export const useShopInfo = defineStore('shopInfo', () => {
  const profile: Ref<Profile> = ref({
    id: 0,
    profile_id: "",
    account_name: "",
    time_zone: "",
    advertiser_id: "",
    country_code: "",
    currency_code: "",
    currency_symbol: "",
    marketplace_str_id: ""
  })

  function updateShopInfo(obj: Profile) {
    profile.value.id = obj.id
    profile.value.profile_id = obj.profile_id
    profile.value.account_name = obj.account_name
    profile.value.time_zone = obj.time_zone
    profile.value.advertiser_id = obj.advertiser_id
    profile.value.country_code = obj.country_code
    profile.value.currency_code = obj.currency_code
    profile.value.currency_symbol = obj.currency_symbol
    profile.value.marketplace_str_id = obj.marketplace_str_id
    Session.set('shopInfo', profile.value);
  }

  async function reqShopInfo() {
    return request({
      url: '/api/ad_manage/profiles/',
      method: 'GET',
      params: { limit: 1 }
    })
  }

  async function initShopInfo() {
    const data = Session.get('shopInfo')
    if (data?.profile_id) {
      profile.value = data
    } else {
      const resp: any = await reqShopInfo();
      updateShopInfo(resp.data[0])
    }
  }

  return { profile, updateShopInfo, initShopInfo }
})
