import { routes } from '@/router/routes'
import { useAppStore } from '@/stores/app'
import wechatOa from './wechat'
import { useUserStore } from '@/stores/user'
import { paramsToStr } from '@/utils/util'
import { SHARE_CODE } from '@/enums/cacheEnums'

export default {
    async onShareAppMessage(res: any) {
        const { getWebsiteConfig } = useAppStore()
        // const path = '/pages/index/index'
        if (res.from === 'button') {
            return {
                title: getWebsiteConfig.shop_name,
                path: await getSharePath()
            }
        }
        if (res.from === 'menu') {
            return {
                title: getWebsiteConfig.shop_name,
                path: await getSharePath()
            }
        }
    },
    // 分享到朋友圈
    onShareTimeline() {
        const { getWebsiteConfig } = useAppStore()
        return {
            title: getWebsiteConfig.shop_name,
            path: '/pages/index/index',
            imageUrl: ''
        }
    },
    onShow() {
        const menuList = [
            'menuItem:share:appMessage',
            'menuItem:share:timeline',
            'menuItem:share:qq',
            'menuItem:share:weiboApp',
            'menuItem:share:QZone'
        ]
        const pageList = getCurrentPages()
        const currentPage = pageList[pageList.length - 1].route
        const pageMate = routes.find((item) => {
            return item.path == `/${currentPage}`
        })
        if (pageMate.unshare == true) {
            // #ifdef MP-WEIXIN
            uni.hideShareMenu({
                hideShareItems: []
            })
            // #endif
            // #ifdef H5
            wechatOa.hideMenuItems(menuList)
            // #endif
        }
    },
    async onLoad(option: any) {
        query = option
    }
}
let query: any = {}

const getSharePath = async () => {
    const pageList = getCurrentPages()
    const currentPage = pageList[pageList.length - 1].route
    let origin = ''
    //#ifdef H5
    origin = `${window.location.origin}/mobile`
    //#endif
    const path = `${origin}/${currentPage}`

    try {
        const userStore = useUserStore()
        if (!userStore?.userInfo?.code) {
            await userStore.getUser()
        }
        if (userStore?.userInfo?.code) {
            query[SHARE_CODE] = userStore?.userInfo?.code
        }
    } catch (error) {
        console.log(error)
    }
    console.log(`${path}${paramsToStr(query)}`)

    return `${path}${paramsToStr(query)}`
}
