const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_utils = require("../../../utils.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let _pisell_utils = require("@pisell/utils");
let decimal_js = require("decimal.js");
let lodash = require("lodash");
//#region src/components/booking/info/service/addTimeModal/utils.ts
/**
* @title: 根据商品生成加时列表
* @description: 
* @param {any} config
* @param {number} price
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-07-01 09:34
*/
const getAddTimeSplits = (product, cacheItem, currentResourceTimeEndDate) => {
	const { add_time = {
		calculate_type: "select_quantity",
		max_add_time: 30,
		min_add_time: 30,
		relation_product: {
			type: "product_all",
			products: []
		},
		unit_duration: 30
	}, price = 0 } = product;
	if (add_time.additional_time_options === "unlimited") {
		let _value = require_utils.getEndDate(cacheItem._extend.startDate, "flexible", currentResourceTimeEndDate).diff(cacheItem._extend.endDate, "minute");
		if (_value > 0) return [{
			label: _pisell_utils.locales.getText("page.booking.text.unlimited"),
			value: _value,
			price
		}];
		else return [];
	}
	const startTime = (0, dayjs.default)().startOf("day");
	const endTime = startTime.add(add_time.max_add_time, "minute");
	const timeSlice = (add_time === null || add_time === void 0 ? void 0 : add_time.unit_duration) || 5;
	let arr = [];
	let currentTime = startTime;
	let lastDuration = 0;
	while (currentTime.isBefore(endTime)) {
		let nextTime = currentTime;
		if (lastDuration === 0) nextTime = nextTime.add(add_time.min_add_time, "minute");
		else nextTime = currentTime.add(timeSlice, "minute");
		const durationValue = nextTime.diff(startTime, "minute");
		let durationLabel = `${durationValue} ${_pisell_utils.locales.getText("pisell2.text.min")}`;
		if (durationValue >= 60) durationLabel = require_utils.getDiscountTime(durationValue);
		arr.push({
			label: durationLabel,
			value: durationValue,
			price: durationValue / timeSlice * price
		});
		lastDuration = durationValue;
		currentTime = nextTime;
	}
	return arr;
};
/**
* @title: 判断当前商品是否可用
* @description: 
* @param {any} relationProduct
* @param {any} currentItem
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-07-01 09:33
*/
const isUsableProduct = (addTime, currentItem) => {
	var _addTime$relation_pro;
	const type = (_addTime$relation_pro = addTime.relation_product) === null || _addTime$relation_pro === void 0 ? void 0 : _addTime$relation_pro.type;
	const relationProduct = addTime.relation_product || [];
	if (!currentItem) return false;
	if (addTime.additional_time_options === "unlimited") {
		let maxEndDate = require_utils.getEndDate(currentItem._extend.startDate, "flexible", "");
		if (currentItem._extend.endDate.isAfter(maxEndDate)) return false;
	}
	if (type === "product_all") return true;
	else if (type === "product_collection") return !!(0, lodash.intersectionBy)(relationProduct.products.map((item) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { id: item.product_collection_id })) || [], currentItem.collection || [], "id").length;
	else return relationProduct.products.some((item) => {
		if (type === "products") {
			if (item.product_id == currentItem.product_id) return true;
		}
	});
};
const addTimeRowKey = {
	create: (product_id, time) => {
		return `${product_id}_${time}`;
	},
	getValue: (str) => {
		let arr = str.split("_");
		let id = arr[0] || 0;
		let time = arr[1] || 0;
		return {
			product_id: Number(id),
			time: time === "flexible" ? time : Number(arr[1] || 0)
		};
	}
};
/**
* @title: 根据价格和数量
* @description: 
* @param {number} discountAmount
* @param {number} number
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-09-03 14:48
*/
const calculateDiscount = (discountAmount, number) => {
	const price = new decimal_js.Decimal(discountAmount);
	const qty = new decimal_js.Decimal(number);
	const discountPerItem = price.dividedBy(qty).toDecimalPlaces(2);
	const totalDiscount = discountPerItem.times(qty);
	const difference = price.minus(totalDiscount);
	const discounts = [];
	for (let i = 0; i < number; i++) {
		const discountAmount = i === number - 1 ? discountPerItem.plus(difference) : discountPerItem;
		const existing = discounts.find((d) => new decimal_js.Decimal(d.discount_amount).equals(discountAmount));
		if (existing) existing.number += 1;
		else discounts.push({
			number: 1,
			discount_amount: +discountAmount
		});
	}
	return discounts;
};
/**
* @title: 根据折扣金额生成商品列表
* @description: 
* @param {number} number 商品数量
* @param {number} newPrice 商品最新总价格
* @param {any} productItem 商品
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-09-03 14:56
*/
const createRelationProducts = (number, newPrice, productItem) => {
	try {
		return calculateDiscount(new decimal_js.Decimal(String(productItem.price)).minus(new decimal_js.Decimal(String(newPrice))).toNumber(), number).map((item) => {
			return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, productItem), {}, {
				num: item.number,
				discount_amount: item.discount_amount
			});
		});
	} catch (err) {
		return [productItem];
	}
};
//#endregion
exports.addTimeRowKey = addTimeRowKey;
exports.createRelationProducts = createRelationProducts;
exports.getAddTimeSplits = getAddTimeSplits;
exports.isUsableProduct = isUsableProduct;
