import { _objectSpread2 } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { getUsableTime } from "../addService/utils.js";
import { getDiscountTime, getEndDate } from "../../../utils.js";
import dayjs from "dayjs";
import { locales } from "@pisell/utils";
import { intersectionBy } from "lodash";
//#region src/components/booking/info2/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) => {
	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") {
		var _getUsableTime;
		let _value = getEndDate(cacheItem._extend.startDate, "flexible", (_getUsableTime = getUsableTime(cacheItem)) === null || _getUsableTime === void 0 || (_getUsableTime = _getUsableTime._extend) === null || _getUsableTime === void 0 || (_getUsableTime = _getUsableTime.usableTime) === null || _getUsableTime === void 0 ? void 0 : _getUsableTime.end_at).diff(cacheItem._extend.endDate, "minute");
		if (_value > 0) return [{
			label: locales.getText("page.booking.text.unlimited"),
			value: _value,
			price
		}];
		else return [];
	}
	const startTime = dayjs().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} ${locales.getText("pisell2.text.min")}`;
		if (durationValue >= 60) durationLabel = 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 = 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 !!intersectionBy(relationProduct.products.map((item) => _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)
		};
	}
};
//#endregion
export { addTimeRowKey, getAddTimeSplits, isUsableProduct };
