import { _objectSpread2 } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { distributeDiscountOrIncrease } from "../../service/serviceManager/distributeDiscount.js";
import { setProductPrice } from "../../../info2/service/editService/utils.js";
import { genServiceKey } from "../../../../ticketBooking/utils/index.js";
import { getProductTotalPrice } from "../../../utils.js";
import { getUniqueId } from "@pisell/utils";
import Decimal from "decimal.js";
//#region src/components/booking/info/service2/Group/utils.ts
const getGroupTotalPrice = (items) => {
	return items.reduce((prev, next) => {
		prev.duration += next._extend.duration;
		prev.total += next._extend.total;
		prev.origin_total += next._extend.origin_total;
		return prev;
	}, {
		total: 0,
		origin_total: 0
	});
};
/**
* @title: 创建一个虚拟的组
* @description: 
* @param {any} items
* @return {*}
* @Author: zhiwei.Wang
*/
const concatBookings = (items) => {
	let firstItem = items[0];
	let lastItem = items[items.length - 1];
	let { total, duration, startDate, endDate, origin_total } = items.reduce((prev, next) => {
		prev.duration += next._extend.duration;
		prev.total += next._extend.total;
		prev.origin_total += next._extend.origin_total;
		return prev;
	}, {
		price: 0,
		total: 0,
		origin_total: 0,
		duration: 0,
		startDate: firstItem._extend.startDate,
		endDate: lastItem._extend.endDate
	});
	return _objectSpread2(_objectSpread2({}, firstItem), {}, {
		_id: firstItem._extend.groupId,
		_extend: _objectSpread2(_objectSpread2({}, firstItem._extend), {}, {
			items,
			duration,
			total,
			origin_total,
			startDate,
			endDate,
			start_date: startDate,
			end_date: endDate
		})
	});
};
const createBookingItemsByGroup = (item, isBatchEdit) => {
	const groupId = item._extend.groupId || getUniqueId("bookingGroup_");
	let bookings = [];
	for (let i = 0; i < item._extend.duration; i++) {
		var _extend$multiDayPrice, _item$_extend;
		const _extend = _objectSpread2({}, item._extend);
		const startDate = _extend.start_date.add(i, "day");
		const endDate = _extend.start_date.add(i + 1, "day");
		const price = ((_extend$multiDayPrice = _extend.multiDayPrices) === null || _extend$multiDayPrice === void 0 || (_extend$multiDayPrice = _extend$multiDayPrice[startDate.format("YYYY-MM-DD")]) === null || _extend$multiDayPrice === void 0 ? void 0 : _extend$multiDayPrice.total) || item._extend.price;
		const cacheItem = _objectSpread2(_objectSpread2({}, item), {}, {
			booking_id: isBatchEdit ? 0 : item.booking_id,
			_id: `${item._id}-${i + 1}`,
			_extend: _objectSpread2(_objectSpread2({}, _extend), {}, {
				order_detail_id: isBatchEdit ? 0 : item.order_detail_id,
				groupId,
				duration: 1,
				price,
				startDate,
				endDate,
				start_date: startDate,
				end_date: endDate,
				dateKey: `${startDate.format("YYYY-MM-DD")}~${endDate.format("YYYY-MM-DD")}}`
			})
		});
		const totalPrice = getProductTotalPrice(cacheItem);
		const origin_total = (_item$_extend = item._extend) === null || _item$_extend === void 0 || (_item$_extend = _item$_extend.items) === null || _item$_extend === void 0 || (_item$_extend = _item$_extend[0]) === null || _item$_extend === void 0 || (_item$_extend = _item$_extend._extend) === null || _item$_extend === void 0 ? void 0 : _item$_extend.origin_total;
		cacheItem._extend.total = totalPrice;
		cacheItem._extend.origin_total = origin_total !== null && origin_total !== void 0 ? origin_total : totalPrice;
		cacheItem._extend.items = void 0;
		cacheItem._serviceKey = genServiceKey(cacheItem);
		bookings.push(cacheItem);
	}
	if (isBatchEdit) {
		const groupTotalPrice = item._extend.total;
		const groupOriginTotalPrice = item._extend.origin_total;
		const discountAmount = (groupOriginTotalPrice - groupTotalPrice) * -1;
		if (discountAmount !== 0) if (groupOriginTotalPrice !== 0) {
			bookings = distributeDiscountOrIncrease(bookings, discountAmount, groupOriginTotalPrice);
			bookings = bookings.map((booking) => {
				var _booking$_extend, _booking$_extend2;
				return setProductPrice(booking, {
					newTotal: booking === null || booking === void 0 || (_booking$_extend = booking._extend) === null || _booking$_extend === void 0 ? void 0 : _booking$_extend.total,
					origin_total: booking === null || booking === void 0 || (_booking$_extend2 = booking._extend) === null || _booking$_extend2 === void 0 ? void 0 : _booking$_extend2.origin_total
				});
			});
		} else {
			const count = bookings.length;
			const avgTotal = new Decimal(groupTotalPrice).div(count).toDecimalPlaces(2, Decimal.ROUND_HALF_UP);
			let assignedSum = new Decimal(0);
			bookings = bookings.map((booking, i) => {
				const newTotal = i === count - 1 ? new Decimal(groupTotalPrice).sub(assignedSum).toNumber() : avgTotal.toNumber();
				if (i !== count - 1) assignedSum = assignedSum.add(avgTotal);
				return _objectSpread2(_objectSpread2({}, booking), {}, { _extend: _objectSpread2(_objectSpread2({}, booking._extend), {}, {
					total: newTotal,
					price: newTotal
				}) });
			});
		}
	}
	console.log("bookings", bookings);
	return bookings;
};
const isGroupBooking = (_id) => {
	var _id$indexOf;
	return (_id === null || _id === void 0 || (_id$indexOf = _id.indexOf) === null || _id$indexOf === void 0 ? void 0 : _id$indexOf.call(_id, "bookingGroup_")) === 0;
};
const getProductByGroupId = (groupId, items) => {
	return items.filter((item) => {
		var _item$_extend2;
		return ((_item$_extend2 = item._extend) === null || _item$_extend2 === void 0 ? void 0 : _item$_extend2.groupId) === groupId;
	}).map((item) => item._id);
};
const filterItemByGroupId = (groupId, items) => {
	return items.filter((item) => {
		var _item$_extend3;
		return ((_item$_extend3 = item._extend) === null || _item$_extend3 === void 0 ? void 0 : _item$_extend3.groupId) !== groupId;
	});
};
//#endregion
export { concatBookings, createBookingItemsByGroup, filterItemByGroupId, getGroupTotalPrice, getProductByGroupId, isGroupBooking };
