import { isMultiDayProduct } from "../info2/service/addService/utils.js";
import { getDays, getIsEdit, isSessionProduct } from "../utils.js";
import { isUsableProduct } from "../info2/service/addTimeModal/utils.js";
import dayjs from "dayjs";
//#region src/components/booking/info/utils.ts
const getIsBundleOrCombination = (productDetail) => {
	var _productDetail$bundle;
	if (productDetail === null || productDetail === void 0 || (_productDetail$bundle = productDetail.bundle_group) === null || _productDetail$bundle === void 0 ? void 0 : _productDetail$bundle.length) return true;
	return false;
};
const uniqueByField = (arr, field) => {
	const seen = /* @__PURE__ */ new Set();
	return arr.filter((item) => {
		const fieldValue = item[field];
		if (seen.has(fieldValue)) return false;
		else {
			seen.add(fieldValue);
			return true;
		}
	});
};
const moveItemToFront = (arr, targetId) => {
	const index = arr.findIndex((item) => item.id === targetId);
	if (index !== -1) {
		const targetItem = arr[index];
		arr.splice(index, 1);
		arr.unshift(targetItem);
	}
	return arr;
};
/**
* @title:  判断两个时间是否在同一个时间段内
* @description: 
* @param {number} time1 开始时间
* @param {number} time2 结束时间
* @param {number} interval 时间间隔
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-13 19:37
*/
const isInterval = (time1, time2, interval = 1e3) => {
	if (time2 === time1) return false;
	return Math.abs(time2 - time1) < interval;
};
/**
* @title: 根据时间间隔格式化服务列表
* @description: 防止短时间内添加多个相同服务, 如果两个服务的时间间隔小于1秒，则合并为一个服务
* @param {any} list 服务列表
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-13 19:36
*/
const formatServiceListByTime = (list) => {
	try {
		let _list = [...list];
		const length = _list.length;
		if (length > 1) {
			var _list2, _list3;
			let start = (_list2 = _list[length - 2]) === null || _list2 === void 0 ? void 0 : _list2._time;
			let end = (_list3 = _list[length - 1]) === null || _list3 === void 0 ? void 0 : _list3._time;
			if (start && end && isInterval(start, end)) {
				_list.pop();
				return _list;
			} else return list;
		} else return list;
	} catch (err) {
		return list;
	}
};
const isBooking4Shop = (shopId, renderType, businessUtils) => {
	if (renderType === "eventBooking") return false;
	return [
		8390,
		1350,
		1401,
		3013,
		7154,
		7572,
		7007,
		8119,
		7320,
		7362,
		6497,
		6087,
		6303,
		6726,
		8191,
		6812,
		6813,
		6814,
		7651,
		8586,
		8581,
		9,
		2155,
		8030,
		1749,
		8194,
		2327,
		8609,
		8610,
		8782,
		1285,
		6124,
		8727,
		5538,
		6988,
		8872,
		6930,
		8824,
		8913,
		8918,
		6413,
		8943
	].includes(shopId) || (businessUtils === null || businessUtils === void 0 ? void 0 : businessUtils.getAppByModal("resourcekit"));
};
function generateDateRange(start, end) {
	const dateRange = [];
	let currentDate = dayjs(start);
	while (currentDate.isBefore(dayjs(end).add(1, "day"))) {
		dateRange.push(currentDate.format("YYYY-MM-DD"));
		currentDate = currentDate.add(1, "day");
	}
	return dateRange;
}
const getMultiDayPrices = (data) => {
	const result = {};
	data.forEach((entry) => {
		generateDateRange(entry.start_date, entry.end_date).forEach((date) => {
			result[date] = entry;
		});
	});
	return result;
};
const getAddTimeProducts = (state, currentService) => {
	return (state.addTimeProducts || []).some((item) => isUsableProduct(item.add_time, currentService));
};
const isOpenAddTime = (state) => {
	var _state$bookingConfig;
	const appointment_card = (state === null || state === void 0 || (_state$bookingConfig = state.bookingConfig) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.config) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.information_tab) === null || _state$bookingConfig === void 0 ? void 0 : _state$bookingConfig.appointment_card) || { quick_time: 1 };
	if ((state === null || state === void 0 ? void 0 : state.renderType) === "eventBooking") return false;
	if (state === null || state === void 0 ? void 0 : state.disabledEdit) return false;
	if (appointment_card.quick_time !== 1) return false;
	return true;
};
const isShowAddTime = (state, currentService) => {
	if (isMultiDayProduct(currentService)) return false;
	if (!isOpenAddTime(state)) return false;
	if (getIsEdit(state)) return false;
	if (!getAddTimeProducts(state, currentService)) return false;
	return true;
};
const editSessionProduct = (cacheItem, session) => {
	if (isSessionProduct(cacheItem) && session) {
		cacheItem._extend.start_date = session.start_at;
		cacheItem._extend.startDate = session.start_at;
		cacheItem._extend.start_time = session.start_time;
		cacheItem._extend.end_time = session.end_time;
		cacheItem._extend.end_date = session.end_at;
		cacheItem._extend.endDate = session.end_at;
		cacheItem._extend.duration = getDays({ _extend: cacheItem._extend }, "minutes");
		return {
			duration: cacheItem._extend.duration,
			start_time: session.start_time
		};
	}
};
//#endregion
export { editSessionProduct, formatServiceListByTime, getIsBundleOrCombination, getMultiDayPrices, isBooking4Shop, isOpenAddTime, isShowAddTime, moveItemToFront, uniqueByField };
