import { ref } from 'vue'
import { createGlobalState, useLocalStorage } from '@vueuse/core'
import axios from 'axios'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import timezone from 'dayjs/plugin/timezone'

export const useStockQuote = createGlobalState(() => {
  const stockQuote = useLocalStorage('stockQuote', {
    "Open": "",
    "Volume": "",
    "DayRange": "",
    "WeekRange": "",
    "MarketCap": "",
    "change": [
      "",
      ""
    ]
  })
  const date = new Date();
const options = {
  year: 'numeric',
  month: 'short',
  day: 'numeric',
  hour: 'numeric',
  minute: '2-digit',
  hour12: true,
  timeZone: 'America/New_York',
  timeZoneName: 'short'
};

dayjs.extend(utc)
dayjs.extend(timezone)

const getFormattedFriday = () => {
  const now = dayjs().tz('America/New_York')
  // 本周五16:00
  const thisFriday = now.day() >= 5
    ? now.day(5).hour(16).minute(0).second(0).millisecond(0)
    : now.day(5 - 7).hour(16).minute(0).second(0).millisecond(0)
  // 判断当前是否已到本周五16:00
  let showFriday
  if (now.isAfter(thisFriday)) {
    showFriday = thisFriday
  } else {
    // 上周五16:00
    showFriday = thisFriday.subtract(7, 'day')
  }
  return showFriday.format('MMM D, YYYY, h:mm A [EDT]')
}

const formatted = ref(getFormattedFriday())
const getStockQuate= async()=>{
    const res = await axios.get('https://saas-test.szjixun.cn/api/chart/forward/test')
    stockQuote.value=res.data
}
  return {
    formatted,
    getStockQuate,
    stockQuote
  }
})
