import { _defineProperty } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.js";
import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { getAppointmentTimingPreference } from "./info2/utilsByBooking.js";
import { isMultiDayProduct } from "./info2/service/addService/utils.js";
import { formatDayDate, isDayBooking } from "./info/service2/utils.js";
import dayjs from "dayjs";
import { locales } from "@pisell/utils";
import Decimal from "decimal.js";
import { intersectionBy } from "lodash";
//#region src/components/booking/utils.ts
/**
* @title: 高阶函数结果缓存（Memoization）
*/
function memoize(fn) {
	const cache = /* @__PURE__ */ new Map();
	return function(...args) {
		const key = JSON.stringify(args);
		if (cache.has(key)) return cache.get(key);
		const result = fn.apply(this, args);
		cache.set(key, result);
		return result;
	};
}
var Request = class {
	constructor() {
		_defineProperty(this, "request", {});
	}
	setRequest(val) {
		this.request = val;
	}
	getRequest() {
		return this.request;
	}
};
const request = new Request();
/**
* @title: 格式化持续时长用于展示
* @description:
* @param {number} service_times
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-04-26 14:00
*/
const getDiscountTime = (service_times, isDay) => {
	if (!service_times || service_times < 0) return "";
	if (isDay) return `${service_times} ${locales.getText("pisell2.text.day")(service_times)}`;
	if (service_times < 60) return `${service_times} ${locales.getText("pisell2.text.min")}`;
	else {
		let str = "";
		const hours = Math.floor(service_times / 60);
		const minutes = service_times % 60;
		str += `${hours}${locales.getText("pisell2.text.h")}`;
		if (minutes > 0) str += ` ${minutes}${locales.getText("pisell2.text.min")}`;
		return str;
	}
};
/**
* @title: 格式化跨日预约的时长用于展示
* @description:
* @param {number} days
* @param {number} minutes
* @return {*}
*/
const getDiscountTimeByDayMinutes = (days, minutes) => {
	if (days === 0) return getDiscountTime(minutes);
	return `${days} ${locales.getText("pisell2.text.day")(days)} ${getDiscountTime(minutes)}`;
};
/**
* @title: 时间切片
* @description:
* @param {any} config
* @param {string} locale
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-04-26 13:59
*/
const _sliceDayIntoFiveMinutes = (config, locale, isDay) => {
	let _locale = locale;
	let dayjsLocale = _locale === "en-US" || _locale === "en" ? "en" : "zh-cn";
	dayjs.locale(dayjsLocale);
	const startTime = dayjs().startOf("day");
	const endTime = dayjs().endOf("day");
	const timeSlice = (config === null || config === void 0 ? void 0 : config.value) || 5;
	let timeSlices = [];
	let durationSlicesBasedOnTime = [];
	let currentTime = startTime;
	if (isDay) {
		let currentDay = 1;
		while (currentDay <= 30) {
			const timeLabel = currentTime.format("hh:mm A");
			const timeValue = currentTime.format("HH:mm");
			timeSlices.push({
				label: timeLabel,
				value: timeValue
			});
			durationSlicesBasedOnTime.push({
				label: `${currentDay} ${locales.getText("pisell2.text.day")(currentDay)}`,
				value: currentDay
			});
			currentDay++;
		}
		return {
			timeSlices,
			durationSlicesBasedOnTime
		};
	}
	while (currentTime.isBefore(endTime)) {
		const timeLabel = currentTime.format("hh:mm A");
		const timeValue = currentTime.format("HH:mm");
		timeSlices.push({
			label: timeLabel,
			value: timeValue
		});
		const nextTime = currentTime.add(timeSlice, "minute");
		const durationValue = nextTime.diff(startTime, "minute");
		if (durationValue > 60) {
			const durationLabel = getDiscountTime(durationValue);
			durationSlicesBasedOnTime.push({
				label: durationLabel,
				value: durationValue
			});
		} else {
			const durationLabel = `${durationValue} ${locales.getText("pisell2.text.min")}`;
			durationSlicesBasedOnTime.push({
				label: durationLabel,
				value: durationValue
			});
		}
		currentTime = nextTime;
	}
	return {
		timeSlices,
		durationSlicesBasedOnTime
	};
};
/**
* @title: 时间切片（带缓存版本）
* @description: 使用 memoize 高阶函数包裹 sliceDayIntoFiveMinutes，相同参数时直接返回缓存结果
*/
const sliceDayIntoFiveMinutes = memoize(_sliceDayIntoFiveMinutes);
const getNowTime = () => {
	return dayjs().format("HH:mm");
};
const isNowTime = (time) => {
	return time === "now";
};
/**
* @title: 获取服务的开始时间, 如果是now 转换为当前的时间
* @description:
* @param {string} start_time
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-04-26 14:03
*/
const getServeStartTime = (start_time) => {
	if (isNowTime(start_time)) return getNowTime();
	return start_time;
};
/**
* @title: 格式化服务的 开始时间-结束时间-时长, 用于展示
* @description:
* @param {any} item
* @param {any} currentDate
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-04-26 14:04
*/
const getServiceTime = (item, currentDate) => {
	const startDate = dayjs(currentDate.format("YYYY-MM-DD") + " " + getServeStartTime(item._extend.start_time));
	return `${startDate.format("HH:mm")} - ${startDate.add(flexibleObj.getValue(item._extend.duration), "minute").format("HH:mm")} ( ${getDiscountTime(flexibleObj.getValue(item._extend.duration))} )`;
};
const findNextSlice = ({ timeSlices = [], index = 0, date = "" }) => {
	const currentTimeValue = date || dayjs().format("HH:mm");
	for (let i = 0; i < timeSlices.length; i++) if (timeSlices[i].value !== "now") {
		if (timeSlices[i].value >= currentTimeValue) return timeSlices[i].value;
	}
	return timeSlices[index].value;
};
const findNextDuration = (current, array, defaultValue) => {
	if (defaultValue) return defaultValue;
	for (let i = 0; i < array.length; i++) if (array[i].value == (current === null || current === void 0 ? void 0 : current.value)) return array[i].value;
	return array[0].value;
};
const formatProductListByCategory = (list, filterItem) => {
	let _list = list || [];
	if (filterItem) _list = _list.filter(filterItem);
	let categoryObject = {};
	for (let i = 0; i < _list.length; i++) {
		const productItem = _list[i];
		if ((productItem.category || []).length) for (let j = 0; j < productItem.category.length; j++) {
			let categoryItem = productItem.category[j];
			if (!categoryObject[categoryItem.id]) categoryObject[categoryItem.id] = _objectSpread2(_objectSpread2({}, categoryItem), {}, { products: [] });
			categoryObject[categoryItem.id] = _objectSpread2(_objectSpread2({}, categoryItem), {}, { products: [...categoryObject[categoryItem.id].products, productItem] });
		}
		else {
			if (!categoryObject["other"]) categoryObject["other"] = {
				id: 0,
				title: locales.getText("pisell2.text.other"),
				name: locales.getText("pisell2.text.other"),
				cover: "",
				products: []
			};
			categoryObject["other"] = _objectSpread2(_objectSpread2({}, categoryObject["other"]), {}, { products: [...categoryObject["other"].products, productItem] });
		}
	}
	return Object.values(categoryObject).sort((a, b) => b.sort - a.sort);
};
const getResourceTimeIsUsable = (item, cacheItem) => {
	var _cacheItem$_extend;
	const isEdit = !!cacheItem.booking_id;
	const isCurrentRelation = item.item_id === (cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend = cacheItem._extend) === null || _cacheItem$_extend === void 0 ? void 0 : _cacheItem$_extend.relation_id);
	const startDate = dayjs(cacheItem._extend.start_date.format("YYYY-MM-DD") + " " + getServeStartTime(cacheItem._extend.start_time));
	const isFlexible = flexibleObj.isFlexible(cacheItem._extend.duration);
	let isMaxLimitRange = false;
	let isDateRange = false;
	let time = _objectSpread2({}, item);
	if (time.max_participants_limit == 0) isMaxLimitRange = true;
	else isMaxLimitRange = (isEdit && isCurrentRelation ? time.order_event_count - 1 : time.order_event_count) < time.max_participants_limit;
	const timeStartDate = dayjs(time.start_date + " " + time.start_time).subtract(1, "second");
	const timeEndDate = dayjs(time.end_date + " " + time.end_time).add(1, "second");
	let bol;
	if (isFlexible) bol = startDate.isAfter(timeStartDate) && startDate.isBefore(timeEndDate);
	else {
		let endDate = startDate.add(flexibleObj.getValue(cacheItem._extend.duration), "minute");
		bol = startDate.isAfter(timeStartDate) && endDate.isBefore(timeEndDate);
	}
	if (bol) isDateRange = true;
	time._extend = {
		isMaxLimitRange,
		isDateRange,
		isUsable: isDateRange
	};
	return time;
};
/**
* @title: 查找是否有可用的time
* @description:
* @param {any} resourceItem
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-01-09 13:57
*/
const formatTimes = (resourceItem, cacheItem) => {
	var _cacheItem$_extend2;
	if ((cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend2 = cacheItem._extend) === null || _cacheItem$_extend2 === void 0 ? void 0 : _cacheItem$_extend2.sub_type) === "days") {
		const isResourceItem = (resourceItem === null || resourceItem === void 0 ? void 0 : resourceItem.form_record_id) > 0;
		let isMaxLimitRange = (resourceItem === null || resourceItem === void 0 ? void 0 : resourceItem.sold_out) === 1;
		let isDateRange = (resourceItem === null || resourceItem === void 0 ? void 0 : resourceItem.available_status) === 1;
		const usableTime = {
			isMaxLimitRange,
			isDateRange,
			isUsable: isResourceItem ? isDateRange && !isMaxLimitRange : true
		};
		resourceItem._extend = _objectSpread2(_objectSpread2({}, resourceItem._extend), {}, { usableTime });
		return resourceItem;
	}
	let usableTimes = [];
	let _resourceItem = _objectSpread2({}, resourceItem);
	_resourceItem.times = _resourceItem.times.map((time) => {
		let _time = getResourceTimeIsUsable(time, cacheItem);
		if (_time === null || _time === void 0 ? void 0 : _time._extend.isUsable) usableTimes.push(_time);
		return _time;
	});
	let _time = (usableTimes || []).sort((a, b) => {
		return dayjs(a.end_date + " " + a.end_time).valueOf() - dayjs(b.end_date + " " + b.end_time).valueOf();
	})[0];
	resourceItem._extend = _objectSpread2(_objectSpread2({}, resourceItem._extend), {}, { usableTime: _time });
	return resourceItem;
};
/**
* @title:
* @description: 查找是否有可用的商品
* @param {any} resourceItem
* @param {any} currentItem
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-01-09 13:59
*/
const formatResourceProducts = (resourceItem, currentItem) => {
	let usableProduct = false;
	let _resourceItem = _objectSpread2({}, resourceItem);
	const type = _resourceItem.partyroom_package.type;
	if (type === "product_all") usableProduct = true;
	else if (type === "product_collection") {
		var _resourceItem$partyro;
		usableProduct = !!intersectionBy(((_resourceItem$partyro = _resourceItem.partyroom_package) === null || _resourceItem$partyro === void 0 ? void 0 : _resourceItem$partyro.products.map((item) => _objectSpread2(_objectSpread2({}, item), {}, { id: item.product_collection_id }))) || [], currentItem.collection || [], "id").length;
	} else usableProduct = _resourceItem.partyroom_package.products.some((item) => {
		if (type === "products") {
			if (item.product_id == currentItem.product_id) return true;
		}
	});
	resourceItem._extend = _objectSpread2(_objectSpread2({}, resourceItem._extend), {}, { usableProduct });
	return resourceItem;
};
/**
* @title: 获取店铺的营业结束时间
* @description:
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-02-23 10:00
*/
const getShopOpeningHours = () => {
	return localStorage.getItem("shopOpeningHours") || "23:59";
};
/**
* @title: 对灵活时长进行处理
* @description:
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-02-23 10:00
*/
const flexibleObj = {
	create: (num) => {
		return `flexible_${num}`;
	},
	getValue: (str) => {
		if (typeof str === "string" && flexibleObj.isFlexible(str)) return Number(str.replace("flexible_", ""));
		return str;
	},
	isFlexible: (str) => {
		if (typeof str !== "string") return false;
		return str.indexOf("flexible_") > -1;
	},
	add: (current, duration) => {
		if (flexibleObj.isFlexible(current)) return flexibleObj.create(flexibleObj.getValue(current) + duration);
		else return Number(current) + duration;
	}
};
/**
* @title: 获取结束时间
* @description: 取 endDate | 店铺营业时间 最小的那一个
* @param {any} startDate
* @param {string} duration
* @param {any} endDate
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-02-23 10:01
*/
const getEndDate = (startDate, duration, endDate) => {
	if (duration === "flexible") {
		let openingDate = dayjs(startDate.format("YYYY-MM-DD") + " " + getShopOpeningHours());
		if (dayjs(startDate).isAfter(openingDate)) openingDate = openingDate.add(1, "day");
		if (!endDate) return openingDate;
		endDate = dayjs(endDate);
		if (endDate.isAfter(openingDate)) return openingDate;
		return endDate;
	}
	return startDate.add(duration, "minute");
};
const getServiceTimes = (service) => {
	var _service$_extend;
	const startDate = dayjs(dayjs(service._extend.start_date).format("YYYY-MM-DD ") + getServeStartTime(service._extend.start_time));
	if (isMultiDayProduct(service)) return {
		startDate,
		endDate: service._extend.end_date
	};
	const endDate = startDate.add(flexibleObj.getValue(service._extend.duration), "minute");
	const session = service === null || service === void 0 || (_service$_extend = service._extend) === null || _service$_extend === void 0 || (_service$_extend = _service$_extend.other) === null || _service$_extend === void 0 ? void 0 : _service$_extend.session;
	if (isSessionProduct(service) && session) return {
		startDate: dayjs(session.start_at),
		endDate: dayjs(session.end_at)
	};
	return {
		startDate,
		endDate
	};
};
const getErrorList = (resource, cacheItem, isDay) => {
	if (!(cacheItem === null || cacheItem === void 0 ? void 0 : cacheItem.id)) return [{
		label: locales.getText("pisell2.text.no-product-added"),
		type: "products"
	}];
	if (!resource || !resource._extend || !cacheItem) return [];
	let list = [];
	if (!resource.usable) {
		var _resource$_extend, _resource$_extend2, _resource$_extend3;
		if (!(resource === null || resource === void 0 || (_resource$_extend = resource._extend) === null || _resource$_extend === void 0 ? void 0 : _resource$_extend.usableProduct)) list.push({
			label: locales.getText("pisell1.text.resource-error-message-1")(resource.labelText, cacheItem === null || cacheItem === void 0 ? void 0 : cacheItem.title),
			type: "product"
		});
		let startText = "";
		let endText = "";
		if (isDay) {
			const { start_date, end_date } = cacheItem === null || cacheItem === void 0 ? void 0 : cacheItem._extend;
			startText = start_date === null || start_date === void 0 ? void 0 : start_date.format("YYYY-MM-DD");
			endText = end_date ? end_date === null || end_date === void 0 ? void 0 : end_date.format("YYYY-MM-DD") : "";
		} else {
			const { startDate, endDate } = getServiceTimes(cacheItem);
			startText = startDate.format("hh:mm A");
			endText = endDate.format("hh:mm A");
			if (dayjs(endDate).isAfter(startDate, "day")) endText = locales.getText("pisell1.text.tomorrow") + endText;
		}
		if (isDay ? !((_resource$_extend2 = resource._extend) === null || _resource$_extend2 === void 0 || (_resource$_extend2 = _resource$_extend2.usableTime) === null || _resource$_extend2 === void 0 ? void 0 : _resource$_extend2.isDateRange) : !((_resource$_extend3 = resource._extend) === null || _resource$_extend3 === void 0 ? void 0 : _resource$_extend3.usableTime)) list.push({
			label: locales.getText("pisell1.text.resource-error-message-2")(resource.labelText, startText, endText),
			type: "time"
		});
		else {
			var _resource$_extend4, _resource$_extend5;
			if (isDay ? (_resource$_extend4 = resource._extend) === null || _resource$_extend4 === void 0 || (_resource$_extend4 = _resource$_extend4.usableTime) === null || _resource$_extend4 === void 0 ? void 0 : _resource$_extend4.isMaxLimitRange : !((_resource$_extend5 = resource._extend) === null || _resource$_extend5 === void 0 || (_resource$_extend5 = _resource$_extend5.usableTime) === null || _resource$_extend5 === void 0 || (_resource$_extend5 = _resource$_extend5._extend) === null || _resource$_extend5 === void 0 ? void 0 : _resource$_extend5.isMaxLimitRange)) list.push({
				label: locales.getText("pisell1.text.resource-error-message-3")(resource.labelText, startText, endText),
				type: "limit"
			});
		}
	}
	return list;
};
/**
* @title: 格式化资源, 添加自定义参数
* @description:
* @param {any} resourceItem
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-01-09 13:40
*/
const formatResource = (resourceItem, cacheItem, formatLabel) => {
	var _resourceItem$_extend, _cacheItem$_extend3;
	let _resourceItem = _objectSpread2({}, resourceItem);
	_resourceItem = formatTimes(_resourceItem, cacheItem);
	_resourceItem = formatResourceProducts(_resourceItem, cacheItem);
	_resourceItem.labelText = _resourceItem.room_name || _resourceItem.name;
	let usable = _resourceItem._extend.usableTime && ((_resourceItem$_extend = _resourceItem._extend.usableTime._extend) === null || _resourceItem$_extend === void 0 ? void 0 : _resourceItem$_extend.isMaxLimitRange) && _resourceItem._extend.usableProduct;
	if ((cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend3 = cacheItem._extend) === null || _cacheItem$_extend3 === void 0 ? void 0 : _cacheItem$_extend3.sub_type) === "days") {
		var _resourceItem$_extend2, _resourceItem$_extend3;
		usable = ((_resourceItem$_extend2 = _resourceItem._extend) === null || _resourceItem$_extend2 === void 0 || (_resourceItem$_extend2 = _resourceItem$_extend2.usableTime) === null || _resourceItem$_extend2 === void 0 ? void 0 : _resourceItem$_extend2.isUsable) && ((_resourceItem$_extend3 = _resourceItem._extend) === null || _resourceItem$_extend3 === void 0 ? void 0 : _resourceItem$_extend3.usableProduct);
	}
	_resourceItem.usable = usable;
	if (formatLabel) _resourceItem.label = formatLabel(_resourceItem);
	return _resourceItem;
};
const getLastServer = (list) => {
	return list === null || list === void 0 ? void 0 : list[0];
};
const getCacheParams = () => {
	const sessionData = sessionStorage.getItem("pisell2.booking.serviceParams");
	const data = sessionData ? JSON.parse(sessionData) : null;
	return data === null || data === void 0 ? void 0 : data.serviceParams;
};
const deleteCacheParams = () => {
	sessionStorage.removeItem("pisell2.booking.serviceParams");
};
/**
* @title: 获取跨日预约的时长
* @description: 
* @param {any} cacheItem
* @return {*}
* @Author: WangHan
* @Date: 2024-12-01 22:43
*/
const getDays = (cacheItem, type) => {
	var _cacheItem$_extend4, _cacheItem$_extend5;
	if (type === "days") {
		const { start_date, end_date } = formatDayDate(cacheItem._extend);
		return end_date.diff(start_date, "day");
	}
	let startDate = dayjs((_cacheItem$_extend4 = cacheItem._extend) === null || _cacheItem$_extend4 === void 0 ? void 0 : _cacheItem$_extend4.start_date);
	return dayjs((_cacheItem$_extend5 = cacheItem._extend) === null || _cacheItem$_extend5 === void 0 ? void 0 : _cacheItem$_extend5.end_date).diff(startDate, type);
};
const isSessionProduct = (cacheItem) => {
	return ["session_product", "session_ticket"].includes(cacheItem.extension_type);
};
const getIsEject = (item, type) => {
	const isSession = isSessionProduct(item);
	return type === "detail" || item.bundle_group_count > 0 || item.option_group_count > 0 || item.is_variant > 0 || isSession;
};
const getIsOnlySession = (item) => {
	const isSession = isSessionProduct(item);
	return !(item.bundle_group_count > 0 || item.option_group_count > 0 || item.is_variant > 0) && isSession;
};
/**
* @title: 创建一个服务商品
* @description:
* @param {any} state
* @param {any} item
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-01-26 14:09
*/
const getServerItemExtend = (state, cacheItem, timeObj) => {
	if (!cacheItem.new && cacheItem._extend.endDate) return cacheItem._extend;
	const appointmentTimingPreference = getAppointmentTimingPreference(state);
	const isSession = isSessionProduct(cacheItem);
	const isDay = isDayBooking(state);
	const setDuration = () => {
		var _cacheItem$duration;
		if (isDay) return getDays(cacheItem, "days");
		if (isSession) return getDays(cacheItem, "minutes");
		return findNextDuration(cacheItem.duration, timeObj === null || timeObj === void 0 ? void 0 : timeObj.durationSlicesBasedOnTime, cacheItem === null || cacheItem === void 0 || (_cacheItem$duration = cacheItem.duration) === null || _cacheItem$duration === void 0 ? void 0 : _cacheItem$duration.value);
	};
	let lastServer = isDay ? false : getLastServer(state.service.value);
	let _extend = { duration: setDuration() };
	if (lastServer) {
		_extend.resource = lastServer._extend.resource;
		let lastServerStartDate = lastServer._extend.startDate;
		let lastServerEndDate = lastServer._extend.endDate;
		if (appointmentTimingPreference === "parallel") _extend.start_time = lastServerStartDate.format("HH:mm");
		else if (dayjs(lastServerEndDate).isAfter(lastServerStartDate, "day")) _extend.start_time = timeObj === null || timeObj === void 0 ? void 0 : timeObj.timeSlices[(timeObj === null || timeObj === void 0 ? void 0 : timeObj.timeSlices.length) - 1].value;
		else _extend.start_time = lastServerEndDate.format("HH:mm");
	} else {
		let findNextSliceProps = {
			timeSlices: timeObj === null || timeObj === void 0 ? void 0 : timeObj.timeSlices,
			index: 0,
			date: ""
		};
		const serviceParams = getCacheParams();
		if (serviceParams) {
			let str = serviceParams.start_date;
			if (str) findNextSliceProps.date = dayjs(str).format("HH:mm");
			let resourceId = Number(serviceParams.resource_id);
			if (resourceId || resourceId === 0) _extend.resource = resourceId;
		}
		if (findNextSliceProps.date && findNextSliceProps.date !== "00:00") _extend.start_time = findNextSlice(findNextSliceProps);
		else if (cacheItem.currentDefaultTime) _extend.start_time = cacheItem.currentDefaultTime;
		else _extend.start_time = dayjs().format("HH:mm");
	}
	_extend = _objectSpread2(_objectSpread2({}, _extend), cacheItem._extend);
	if (isDay) {
		_extend.end_date = dayjs(_extend.start_date).add(_extend.duration, "day");
		const { start_date, end_date } = formatDayDate(_extend);
		_extend = _objectSpread2(_objectSpread2({}, _extend), {}, {
			start_date,
			startDate: start_date,
			end_date,
			endDate: end_date
		});
	}
	return _extend;
};
const getMainPrice = (item, price) => {
	var _item$_extend;
	let newPrice = new Decimal(price || item._extend.price || 0);
	let other = (_item$_extend = item._extend) === null || _item$_extend === void 0 ? void 0 : _item$_extend.other;
	if (other === null || other === void 0 ? void 0 : other.product_variant_id) newPrice = new Decimal((other === null || other === void 0 ? void 0 : other.price) || 0);
	return newPrice.toNumber();
};
const getProductTotalPrice = (item) => {
	var _item$_extend2, _item$_extend3;
	let price = new Decimal(item._extend.price || 0);
	let other = (_item$_extend2 = item._extend) === null || _item$_extend2 === void 0 ? void 0 : _item$_extend2.other;
	if ((_item$_extend3 = item._extend) === null || _item$_extend3 === void 0 ? void 0 : _item$_extend3.priceOverride) {
		var _item$_extend4;
		price = new Decimal(((_item$_extend4 = item._extend) === null || _item$_extend4 === void 0 ? void 0 : _item$_extend4.priceOverride) || 0);
		return price.toNumber();
	}
	if (other === null || other === void 0 ? void 0 : other.product_variant_id) price = new Decimal((other === null || other === void 0 ? void 0 : other.price) || 0);
	if (other === null || other === void 0 ? void 0 : other.bundle) price = other === null || other === void 0 ? void 0 : other.bundle.reduce((accumulator, currentValue) => {
		const currentPrice = new Decimal(currentValue.price || 0);
		const currentNum = new Decimal(currentValue.num || 0);
		return accumulator.add(currentPrice.mul(currentNum));
	}, price);
	if (other === null || other === void 0 ? void 0 : other.option) price = other === null || other === void 0 ? void 0 : other.option.reduce((accumulator, currentValue) => {
		const currentPrice = new Decimal(currentValue.price || 0);
		const currentNum = new Decimal(currentValue.num || 0);
		return accumulator.add(currentPrice.mul(currentNum));
	}, price);
	return price.toDecimalPlaces(2, Decimal.ROUND_HALF_UP).toNumber();
};
const getProductOriginTotalPrice = (item) => {
	var _ref, _ref2, _item$metadata$source, _item$metadata, _item$_extend5, _item$_extend6, _item$_extend7;
	let price = new Decimal((_ref = (_ref2 = (_item$metadata$source = item === null || item === void 0 || (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.source_product_price) !== null && _item$metadata$source !== void 0 ? _item$metadata$source : item === null || item === void 0 ? void 0 : item.price) !== null && _ref2 !== void 0 ? _ref2 : item === null || item === void 0 || (_item$_extend5 = item._extend) === null || _item$_extend5 === void 0 ? void 0 : _item$_extend5.price) !== null && _ref !== void 0 ? _ref : 0);
	let other = (_item$_extend6 = item._extend) === null || _item$_extend6 === void 0 ? void 0 : _item$_extend6.other;
	if ((_item$_extend7 = item._extend) === null || _item$_extend7 === void 0 ? void 0 : _item$_extend7.priceOverride) {
		var _item$_extend8;
		price = new Decimal(((_item$_extend8 = item._extend) === null || _item$_extend8 === void 0 ? void 0 : _item$_extend8.priceOverride) || 0);
		return price.toNumber();
	}
	if (other === null || other === void 0 ? void 0 : other.product_variant_id) price = new Decimal((other === null || other === void 0 ? void 0 : other.price) || 0);
	if (other === null || other === void 0 ? void 0 : other.bundle) price = other === null || other === void 0 ? void 0 : other.bundle.reduce((accumulator, currentValue) => {
		var _ref3, _ref4, _ref5, _currentValue$source_;
		const currentPrice = new Decimal((_ref3 = (_ref4 = (_ref5 = (_currentValue$source_ = currentValue === null || currentValue === void 0 ? void 0 : currentValue.source_product_price) !== null && _currentValue$source_ !== void 0 ? _currentValue$source_ : currentValue === null || currentValue === void 0 ? void 0 : currentValue.original_price) !== null && _ref5 !== void 0 ? _ref5 : currentValue === null || currentValue === void 0 ? void 0 : currentValue.product_price) !== null && _ref4 !== void 0 ? _ref4 : currentValue.price) !== null && _ref3 !== void 0 ? _ref3 : 0);
		const currentNum = new Decimal(currentValue.num || 0);
		return accumulator.add(currentPrice.mul(currentNum));
	}, price);
	if (other === null || other === void 0 ? void 0 : other.option) price = other === null || other === void 0 ? void 0 : other.option.reduce((accumulator, currentValue) => {
		const currentPrice = new Decimal(currentValue.price || 0);
		const currentNum = new Decimal(currentValue.num || 0);
		return accumulator.add(currentPrice.mul(currentNum));
	}, price);
	return price.toDecimalPlaces(2, Decimal.ROUND_HALF_UP).toNumber();
};
/**
* @title: 判断 duration
* @description: 用于处理不同格式的duration
* @param {number} duration
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-02-23 10:02
*/
const getDuration = (duration) => {
	if (typeof duration === "number") return duration;
	if (typeof duration === "object") {
		if (duration.type === "minutes") return duration.value;
		if (duration.type === "flexible") return "flexible";
	}
	return duration;
};
const isWalkIn = (customer_id) => {
	if (customer_id === void 0) return true;
	if (typeof customer_id === "string") return [
		"",
		"0",
		"1"
	].includes(customer_id);
	return [0, 1].includes(customer_id);
};
const getIsEdit = (state) => {
	var _state$addons, _state$client, _state$date, _state$pet, _state$service, _state$notes, _state$contacts;
	if ((_state$addons = state.addons) === null || _state$addons === void 0 ? void 0 : _state$addons.edit) return true;
	if (!state.bookingId) return true;
	if ((_state$client = state.client) === null || _state$client === void 0 ? void 0 : _state$client.edit) return true;
	if ((_state$date = state.date) === null || _state$date === void 0 ? void 0 : _state$date.edit) return true;
	if ((_state$pet = state.pet) === null || _state$pet === void 0 ? void 0 : _state$pet.edit) return true;
	if ((_state$service = state.service) === null || _state$service === void 0 ? void 0 : _state$service.edit) return true;
	if ((_state$notes = state.notes) === null || _state$notes === void 0 ? void 0 : _state$notes.edit) return true;
	if ((_state$contacts = state.contacts) === null || _state$contacts === void 0 ? void 0 : _state$contacts.edit) return true;
	return false;
};
//#endregion
export { deleteCacheParams, findNextDuration, flexibleObj, formatProductListByCategory, formatResource, getCacheParams, getDays, getDiscountTime, getDiscountTimeByDayMinutes, getDuration, getEndDate, getErrorList, getIsEdit, getIsEject, getIsOnlySession, getLastServer, getMainPrice, getNowTime, getProductOriginTotalPrice, getProductTotalPrice, getResourceTimeIsUsable, getServeStartTime, getServerItemExtend, getServiceTime, getServiceTimes, isNowTime, isSessionProduct, isWalkIn, request, sliceDayIntoFiveMinutes };
