import Taro from "@tarojs/taro";

/**
 * 判断是否在微信小程序环境
 * @returns 
 */
export const isWeapp = () => {
    return process.env.TARO_ENV === 'weapp';
}

/**
 * 判断是否在Web环境
 * @returns 
 */
export const isWeb = () => {
    return process.env.TARO_ENV === 'h5';
}

/**
 * 获取枚举
 * @param params 
 * @returns 
 */
export function getEnum(params) {
    if (app?.enumDictList) {
        return app?.enumDictList[params]
    }
    return []
}

/**
 * 分享功能
 * @param params 
 * @returns 
 */
export function share(params: { pagePath: string, bookId?: string | number }) {
    // 直接使用app中的数据，避免调用未定义的函数
    return new Promise(async (resolve, reject) => {
        try {
            let pageComponentList = app?.appInfo?.appPageComponentList
            if (pageComponentList && pageComponentList.length > 0) {
                let pageItem = pageComponentList.find(item => item.pagePath === params.pagePath)
                if (pageItem) {
                    let { sharePicUrl, shareTitles, pageName, pagePath } = pageItem
                    resolve({ sharePicUrl, shareTitles, pageName, pagePath: pagePath + (params.bookId ? `?bookId=${params.bookId}` : "") })
                }
            }
            resolve({})
        } catch (error) {
            resolve({})
        }
    })
}