"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.utils = void 0;
exports.canUseLimitTimeDiscount = canUseLimitTimeDiscount;
exports.returnPrice = returnPrice;
exports.returnLimitPrice = returnLimitPrice;
exports.canReturnMemberPrice = canReturnMemberPrice;
exports.returnMemberPrice = returnMemberPrice;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
/**
 * 判断商品是否可以使用限时折扣
 * @param goods 商品对象
 * @param limitTimeDiscountRes 限时折扣配置
 * @param shopInfo  店铺信息
 * @param shopUserInfo 店铺用户信息
 * @param idKey 商品ID键名，默认"id"
 * @returns
 */
function canUseLimitTimeDiscount(goods, limitTimeDiscountRes, shopInfo, shopUserInfo, idKey = "id") {
    shopInfo = shopInfo || {};
    shopUserInfo = shopUserInfo || {};
    if (!limitTimeDiscountRes || !limitTimeDiscountRes.id) {
        return false;
    }
    const canUseFoods = (limitTimeDiscountRes.foods || "").split(",");
    const goodsCanUse = limitTimeDiscountRes.foodType == 1 ||
        canUseFoods.includes(`${goods[idKey]}`);
    if (!goodsCanUse) {
        return false;
    }
    if (limitTimeDiscountRes.discountPriority == "limit-time") {
        return true;
    }
    if (limitTimeDiscountRes.discountPriority == "vip-price") {
        if (shopUserInfo.isVip != 1 || shopUserInfo.isMemberPrice != 1) {
            return true;
        }
        if (shopUserInfo.isVip == 1 &&
            shopUserInfo.isMemberPrice == 1 &&
            goods.memberPrice * 1 <= 0) {
            return true;
        }
    }
    return false;
}
/**
 * 返回商品显示价格
 * @params {*} args 参数对象
 * @params {*} args.goods 商品对象
 * @params {*} args.shopInfo 店铺信息
 * @params {*} args.limitTimeDiscountRes 限时折扣信息
 * @params {*} args.shopUserInfo 店铺用户信息
 * @returns
 */
function returnPrice(args) {
    let { goods, shopInfo, limitTimeDiscountRes, shopUserInfo, idKey = "product_id", } = args;

    console.log('limits.returnPrice===', args);


    limitTimeDiscountRes = limitTimeDiscountRes || {
        foods: "",
        foodType: 2,
        discountPriority: "",
        discountRate: 0,
        id: 0,
        shopId: 0,
        useType: "",
    };
    const canUseFoods = (limitTimeDiscountRes.foods || "").split(",");
    const includesGoods = limitTimeDiscountRes.foodType == 1 ||
        canUseFoods.includes("" + goods[idKey]);
    shopInfo = shopInfo || {};
    shopUserInfo = shopUserInfo || {};
    if (shopUserInfo.isMemberPrice == 1 &&
        shopUserInfo.isVip == 1 &&
        shopInfo.isMemberPrice == 1) {
        const memberPrice = goods.memberPrice || goods.salePrice;
        //是会员而且启用会员价
        if (limitTimeDiscountRes) {
            //使用限时折扣
            //限时折扣优先
            if (limitTimeDiscountRes.discountPriority == "limit-time") {
                if (includesGoods) {
                    return returnLimitPrice({
                        price: goods.salePrice,
                        limitTimeDiscountRes,
                    });
                }
                else {
                    return memberPrice;
                }
            }
            if (limitTimeDiscountRes.discountPriority == "vip-price" &&
                includesGoods) {
                if (goods.memberPrice * 1 > 0) {
                    //会员优先
                    return memberPrice;
                }
                else {
                    const price = returnLimitPrice({
                        price: goods.salePrice,
                        limitTimeDiscountRes,
                        goods: goods,
                    });
                    return price;
                }
            }
            else {
                return memberPrice;
            }
        }
        else {
            //是会员没有限时折扣
            return memberPrice;
        }
    }
    else {
        // console.log('不是会员或者没有启用会员价',goods,limitTimeDiscountRes);
        //不是会员或者没有启用会员价
        if (limitTimeDiscountRes && limitTimeDiscountRes.id && includesGoods) {
            const price = returnLimitPrice({
                price: goods.salePrice,
                limitTimeDiscountRes,
                goods: goods,
            });
            return price;
        }
        else {
            return goods.salePrice;
        }
    }
}
/**
 * 返回限时折扣价格
 * @params {*} args 参数对象
 * @params {*} args.limitTimeDiscountRes 限时折扣信息
 * @params {*} args.price 商品价格
 * @param  {*} args.goods 商品对象
 * @returns
 */
function returnLimitPrice(args) {
    const { limitTimeDiscountRes, price, goods } = args;
    const discountRate = new bignumber_js_1.default(limitTimeDiscountRes ? limitTimeDiscountRes.discountRate : 100).dividedBy(100);
    const result = (0, bignumber_js_1.default)(price)
        .times(discountRate)
        .decimalPlaces(2, bignumber_js_1.default.ROUND_UP)
        .toNumber();
    return result;
}
/**
 * 判断是否返回会员价
 * @param {*} args 参数对象
 * @param {*} args.shopInfo 店铺信息
 * @param {*} args.shopUserInfo 店铺用户信息
 * @returns
 */
function canReturnMemberPrice(args) {
    const { shopInfo, shopUserInfo } = args;
    if (shopUserInfo.isMemberPrice == 1 && shopUserInfo.isVip == 1) {
        return true;
    }
    else {
        return false;
    }
}
/**
 * 返回会员价格
 * @param {*} goods
 * @returns
 */
function returnMemberPrice(goods) {
    return goods.memberPrice || goods.salePrice;
}
exports.utils = {
    returnPrice,
    canUseLimitTimeDiscount,
    returnLimitPrice,
    canReturnMemberPrice,
    returnMemberPrice,
};
exports.default = exports.utils;
