import { FC } from 'react'
import ContextBlueTooth from "@/use/contextBlueTooth"
import { Provider } from 'react-redux'
import configStore from './store'
import './app.scss'
import Taro, { useDidShow} from '@tarojs/taro'
import { analysisShortCodeApi } from './common/shortCode'

const store = configStore()
const App:FC = (params) => {

  Taro.showShareMenu({
    withShareTicket: true
  })

  useDidShow(() => {
    console.log('params:',params)
  })

  const page = Taro.getCurrentInstance().page
  console.log('res:::', page)
  if(page && page.onShareAppMessage) {
    //当有分享参数时，绑定上下级
    if(page.options?.share) {
      analysisShortCodeApi(page.options.share)
    }
    page.onShareAppMessage = (res) => {
      let path = ''
      let title = ''
      let imageUrl = ''
      let sortCode = Taro.getStorageSync('sort_code')?JSON.parse(Taro.getStorageSync('sort_code')):''
      if(res.from === 'button') {
        path = `/pages/details/index?share=${sortCode.shareShortDetail.code}`
        title = sortCode.shareShortDetail.title
      } else {
        path = `/pages/index/index?share=${sortCode.shareShortPage.code}`
        title = sortCode.shareShortPage.title
      }
      return {
        title,
        path,
        imageUrl,
      }
    }
  }

  return (
        <ContextBlueTooth>
          <Provider store={store}>
            {params.children}
          </Provider>
        </ContextBlueTooth>
  )
}

export default App