import {
    getConfig
} from '@/common/server/interface/common'
import {
    ref
} from 'vue';


export const configData = ref(null);

export const preloadConfigData = async () => {
    const res = await getConfig();
    configData.value = {
        config: res
    };
    console.log("configData.value", configData.value);
};

export const getConfigData = async () => {
    if (configData.value == null) {
        await preloadConfigData();
    }
    return configData.value;
}

/**
 * 获取订阅消息模板ID列表
 * @param {Number} depositFee 押金费用
 * @returns {Array|null} 返回模板ID数组，失败时返回null
 */
export const getSubscribeMessage = async (depositFee) => {
    try {
        debugger
        const config = await getConfigData();
        if (!config || !config.config || !config.config.subscribeMessage) {
            return null;
        }

        const templateIds = config.config.subscribeMessage
            .filter(element => {
                // 如果没有押金，过滤掉扣费通知和退款通知
                if (element.type == "reservation_success" || element.type == "reservation_change" || element.type == "reservation_reminder") {
                    return true;
                }
                return false;
                // if (depositFee === 0) {
                //     return element.type !== "deduction_notice" && element.type !== "refund";
                // }
                // return true;
            })
            .map(element => element.templateId);

        return templateIds;
    } catch (error) {
        console.error('获取订阅消息配置失败：', error);
        return null;
    }
}

/**
 * 
 * @param {*} path 
 * @returns 
 */
export const getShareConfig = async (path) => {
    if (configData.value == null) {
        return null;
    }
    var config = configData.value.config;
    return {
        title: config.shareTitle,
        path: path,
        imageUrl: config.shareImage
    };
}

/**
 * 获取悬浮球配置
 * @returns {Object|null}  返回浮动配置，失败时返回null {
      "enabled": true,
      "image": "https://admin-1308826010.cos.ap-shanghai.myqcloud.com/upload/20251006/20251006174940_1752.jpg",
      "popupImage": "https://admin-1308826010.cos.ap-shanghai.myqcloud.com/upload/20251006/20251006175053_9954.jpg"
    }
 */
export const getFloatConfig = async () => {
    if (configData.value == null) {
        return null;
    }
    var config = configData.value.config;
    var floatConfig = config.floatConfig;
    if (!floatConfig || !floatConfig.enabled) {
        return null;
    }
    return floatConfig;
}
export const getPopupShow = ref(true);
/**
 * 获取首页弹窗配置
 * @returns {Object|null}  返回弹窗配置，失败时返回null {
      "enabled": true,
      "image": "https://admin-1308826010.cos.ap-shanghai.myqcloud.com/upload/20251006/20251006151324_4800.jpg"
    }
 */
export const getPopupConfig = async () => {
    if (configData.value == null) {
        return null;
    }
    var config = configData.value.config;
    var popupConfig = config.popupConfig;
    if (!popupConfig || !popupConfig.enabled) {
        return null;
    }
    return popupConfig;
}



