const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_status = require("../../info/service2/status.js");
const require_utilsByBooking = require("../../info2/utilsByBooking.js");
const require_utils = require("../../../../plus/clientName/utils.js");
const require_amount = require("./amount.js");
const require_utils$1 = require("../../info/service2/utils.js");
const require_utils$2 = require("../../utils.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let _pisell_utils = require("@pisell/utils");
let decimal_js = require("decimal.js");
decimal_js = require_runtime.__toESM(decimal_js);
let lodash = require("lodash");
let _pisell_materials = require("@pisell/materials");
//#region src/components/booking/components/footer/utils.ts
const formatNote = (data) => {
	var _data$notes;
	return ((_data$notes = data.notes) === null || _data$notes === void 0 ? void 0 : _data$notes.orderNote) || "";
};
const formatCustomer = (data) => {
	var _data$client, _data$client2, _data$client3, _data$client4, _data$client5, _data$client6;
	let id = (_data$client = data.client) === null || _data$client === void 0 || (_data$client = _data$client.value) === null || _data$client === void 0 ? void 0 : _data$client.id;
	if (require_utils$2.isWalkIn(id)) id = 1;
	return {
		customer_id: id,
		customer_name: require_utils.getClientName((_data$client2 = data.client) === null || _data$client2 === void 0 ? void 0 : _data$client2.value),
		email: (_data$client3 = data.client) === null || _data$client3 === void 0 || (_data$client3 = _data$client3.value) === null || _data$client3 === void 0 ? void 0 : _data$client3.email,
		phone: ((_data$client4 = data.client) === null || _data$client4 === void 0 || (_data$client4 = _data$client4.value) === null || _data$client4 === void 0 ? void 0 : _data$client4.phone) || ((_data$client5 = data.client) === null || _data$client5 === void 0 || (_data$client5 = _data$client5.value) === null || _data$client5 === void 0 ? void 0 : _data$client5.mobile),
		country_calling_code: (_data$client6 = data.client) === null || _data$client6 === void 0 || (_data$client6 = _data$client6.value) === null || _data$client6 === void 0 ? void 0 : _data$client6.country_calling_code
	};
};
/**
* @title: 套餐单规格信息
* @description:
* @param {any} option
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-02-19 09:26
*/
const formatProductOption = (option) => {
	if (!option) return [];
	return option.map((d) => {
		return {
			num: d.num,
			option_group_item_id: d.product_option_item_id || d.option_group_item_id,
			option_group_id: d.option_group_id,
			custom_add_price: Number(d === null || d === void 0 ? void 0 : d.add_price)
		};
	});
};
/**
* @title: 添加套餐警告
* @description:
* @param {any} data
* @return {*}
* @Author: WangHan
* @Date: 2025-03-05 18:23
*/
const log = (data) => {
	(0, _pisell_utils.sendWarningLog)({
		title: "预约2.0下单无效的套餐商品数据",
		content: [{
			key: "bundle",
			value: JSON.stringify(data)
		}, {
			key: "日期",
			value: (0, dayjs.default)().format("YYYY-MM-DD HH:mm")
		}]
	});
};
/**
* @title: 套餐信息
* @description:
* @param {any} bundle
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-02-19 09:26
*/
const formatProductBundle = (bundle, is_price_include_tax) => {
	if (!bundle) return;
	try {
		const errorBundles = bundle === null || bundle === void 0 ? void 0 : bundle.filter((d) => !(d === null || d === void 0 ? void 0 : d.group_id) || !(d === null || d === void 0 ? void 0 : d.id) || !(d === null || d === void 0 ? void 0 : d._bundle_product_id));
		if ((errorBundles === null || errorBundles === void 0 ? void 0 : errorBundles.length) > 0) log(errorBundles);
	} catch (error) {}
	return bundle.map((d) => {
		var _d$metadata, _d$metadata2;
		const option = formatProductOption(d.option);
		const option_price = option.reduce((temp, item) => {
			return temp + Number(item.num) * item.custom_add_price;
		}, 0);
		const product_discount_difference = ((d === null || d === void 0 ? void 0 : d.discount_list) || []).reduce((pre, cur) => {
			var _cur$metadata;
			return pre + ((cur === null || cur === void 0 || (_cur$metadata = cur.metadata) === null || _cur$metadata === void 0 ? void 0 : _cur$metadata.product_discount_difference) || 0);
		}, 0);
		const metadata = {};
		const _metadata = (_d$metadata = d === null || d === void 0 ? void 0 : d.metadata) !== null && _d$metadata !== void 0 ? _d$metadata : {};
		["custom_product_bundle_map_id", "unique_identification_number"].forEach((field) => {
			if (_metadata[field]) metadata[field] = _metadata[field];
		});
		return {
			"is_charge_tax": d.is_charge_tax,
			"tax_fee": d.tax_fee,
			"bundle_group_id": d.group_id,
			"bundle_id": d.id,
			"bundle_product_id": (d === null || d === void 0 ? void 0 : d._bundle_product_id) || (d === null || d === void 0 ? void 0 : d.product_id),
			"bundle_variant_id": d.bundle_variant_id,
			"num": d.num,
			"extension_id": d.extension_id,
			"extension_type": d.extension_type,
			"price": Number(d === null || d === void 0 ? void 0 : d.product_price),
			"price_type": d === null || d === void 0 ? void 0 : d.price_type,
			"price_type_ext": d === null || d === void 0 ? void 0 : d.price_type_ext,
			"custom_price": (d === null || d === void 0 ? void 0 : d.custom_price) || Math.abs(new decimal_js.default((d === null || d === void 0 ? void 0 : d.original_price) || (d === null || d === void 0 ? void 0 : d.product_price)).sub(option_price).toNumber()).toFixed(2),
			"custom_price_type": d === null || d === void 0 ? void 0 : d.price_type,
			"option": option,
			"bundle_selling_price": Math.abs(d === null || d === void 0 ? void 0 : d.price),
			is_price_include_tax,
			"metadata": require_objectSpread2._objectSpread2({
				custom_product_bundle_map_id: (d === null || d === void 0 || (_d$metadata2 = d.metadata) === null || _d$metadata2 === void 0 ? void 0 : _d$metadata2.custom_product_bundle_map_id) || d._id,
				"surcharge_fee": d.surcharge_fee,
				"surcharge_rounding_remainder": d.surcharge_rounding_remainder,
				"tax_fee_rounding_remainder": d.tax_fee_rounding_remainder,
				relation_surcharge_ids: d.relation_surcharge_ids || [],
				product_discount_difference
			}, metadata)
		};
	});
};
/**
* 获取子商品折扣信息
* @param item
*/
const getBundleDiscountList = (bundle) => {
	if (!bundle) return [];
	let discountList = [];
	bundle.forEach((d) => {
		if (d.discount_list && (0, _pisell_utils.isArr)(d.discount_list)) discountList.push(...d.discount_list.filter((item) => !item.id));
	});
	return discountList;
};
const getSubType = (item) => {
	var _item$duration;
	const _type = (_item$duration = item.duration) === null || _item$duration === void 0 ? void 0 : _item$duration.type;
	if (_type) return _type;
	return "minutes";
};
const formatRelationDetails = (list) => {
	return (list || []).map((item) => {
		return {
			num: item.product_quantity,
			price: item.original_price || 0,
			product_id: item.product_id,
			is_charge_tax: item.is_charge_tax,
			order_detail_id: item.order_detail_id,
			tax_fee: item.tax_fee,
			metadata: { tax_fee_rounding_remainder: item.tax_fee_rounding_remainder }
		};
	});
};
/**
* @title: 传入资源，如果有子资源，会根据组合资源的 capacity 计算修改子资源的 capacity
* @description:
* @param {object} resource
* @return {*}
* @Author: jinglin.tan
*/
const checkSubResourcesCapacity = (resource) => {
	if (resource.children && resource.children.length) {
		let countCapacity = resource.capacity;
		resource.children.forEach((child) => {
			if (child.capacity <= countCapacity) countCapacity -= child.capacity;
			else {
				child.capacity = countCapacity;
				countCapacity = 0;
			}
		});
	}
};
const checkResourcesFormat = (resources, service, isChild) => {
	return resources.map((resource) => {
		var _resource$children;
		if (!isChild) resource.capacity = require_utilsByBooking.getSumCapacity({ cacheItem: service }, false);
		checkSubResourcesCapacity(resource);
		return {
			children: ((_resource$children = resource.children) === null || _resource$children === void 0 ? void 0 : _resource$children.length) ? checkResourcesFormat(resource.children, service, true) : void 0,
			form_id: resource.form_id,
			relation_type: resource.relation_type || "form",
			relation_id: resource.relation_id || 0,
			capacity: resource.capacity,
			like_status: resource.like_status || "common",
			metadata: resource.metadata || {}
		};
	});
};
const formatResource = (service) => {
	if (!service._extend.resource) return;
	try {
		return Object.values(service._extend.resource).reduce((pre, cur) => {
			const newList = checkResourcesFormat(cur, service);
			pre = pre.concat(newList);
			return pre;
		}, []);
	} catch (err) {
		return;
	}
};
/**
* @title: 格式化子预约的holder数据
* @description:
* @param {any} state
* @param {any} item
* @param {any} values
* @return {*}
* @Author: WangHan
* @Date: 2024-12-07 14:52
*/
const formatHolders = (state, item, values) => {
	var _state$bookingConfig, _item$_extend, _item$_extend2, _item$_extend3, _item$_extend4;
	const form_id = (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.basic) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.appointment_subject) === null || _state$bookingConfig === void 0 ? void 0 : _state$bookingConfig.form_id) || 0;
	let form_record = ((_item$_extend = item._extend) === null || _item$_extend === void 0 ? void 0 : _item$_extend.holder_id) ? [(_item$_extend2 = item._extend) === null || _item$_extend2 === void 0 ? void 0 : _item$_extend2.holder_id] : [];
	if (((_item$_extend3 = item._extend) === null || _item$_extend3 === void 0 ? void 0 : _item$_extend3.holder_id) && (0, _pisell_utils.isArr)((_item$_extend4 = item._extend) === null || _item$_extend4 === void 0 ? void 0 : _item$_extend4.holder_id)) {
		var _item$_extend5;
		form_record = (_item$_extend5 = item._extend) === null || _item$_extend5 === void 0 ? void 0 : _item$_extend5.holder_id;
	}
	let obj = {
		customer_id: values.customer_id,
		form_id,
		form_record
	};
	if (state.isFranchisee) obj.is_source_shop = 1;
	return obj;
};
/**
* @title: 格式化商品单价
* @description:
* @param {any} item
* @return {*}
* @Author: WangHan
* @Date: 2024-12-23 12:05
*/
const formatProductPrice = (item) => {
	var _item$_extend6, _item$price, _item$_extend7, _item$variant;
	const other = (_item$_extend6 = item._extend) === null || _item$_extend6 === void 0 ? void 0 : _item$_extend6.other;
	let price = Number((_item$price = item === null || item === void 0 ? void 0 : item.price) !== null && _item$price !== void 0 ? _item$price : item === null || item === void 0 || (_item$_extend7 = item._extend) === null || _item$_extend7 === void 0 ? void 0 : _item$_extend7.price);
	if ((other === null || other === void 0 ? void 0 : other.product_variant_id) > 0 && (item === null || item === void 0 || (_item$variant = item.variant) === null || _item$variant === void 0 ? void 0 : _item$variant.length) > 0) {
		const variant = item.variant.find((d) => d.id === (other === null || other === void 0 ? void 0 : other.product_variant_id));
		if (variant) price = Number(variant === null || variant === void 0 ? void 0 : variant.price) || 0;
	}
	if (other === null || other === void 0 ? void 0 : other.option) {
		var _other$option;
		price = other === null || other === void 0 || (_other$option = other.option) === null || _other$option === void 0 ? void 0 : _other$option.reduce((pre, cur) => {
			return pre + Number(cur.price) * Number(cur.num);
		}, price);
	}
	return price;
};
/**
* checkout2.0: 单个数量商品总价（原价）,包含所有套餐子商品金额、报价单等等
* @param item
* @returns
*/
const formatOriginalPrice = (item) => {
	var _item$_extend8;
	return Number((_item$_extend8 = item._extend) === null || _item$_extend8 === void 0 ? void 0 : _item$_extend8.origin_total);
};
/**
* checkout2.0: 商品维度折扣后价格, original_price - 商品折扣金额（商品手动折扣等等）
* @param item
* @returns
*/
const formatSellingPrice = (item) => {
	var _item$_extend12;
	return Number((_item$_extend12 = item._extend) === null || _item$_extend12 === void 0 ? void 0 : _item$_extend12.total);
};
const formatMetaData = (item, isMultiDayPrices) => {
	var _item$_extend13, _item$_extend$holder_, _item$_extend14, _item$_extend15, _item$_extend17, _item$_extend19;
	let metadata = {};
	if ((_item$_extend13 = item._extend) === null || _item$_extend13 === void 0 ? void 0 : _item$_extend13.capacity) metadata.capacity = item._extend.capacity;
	if (item._extend.groupId) metadata.groupId = item._extend.groupId;
	metadata.holder_id = (_item$_extend$holder_ = (_item$_extend14 = item._extend) === null || _item$_extend14 === void 0 ? void 0 : _item$_extend14.holder_id) !== null && _item$_extend$holder_ !== void 0 ? _item$_extend$holder_ : null;
	if ((_item$_extend15 = item._extend) === null || _item$_extend15 === void 0 ? void 0 : _item$_extend15.unique_identification_number) {
		var _item$_extend16;
		metadata.unique_identification_number = (_item$_extend16 = item._extend) === null || _item$_extend16 === void 0 ? void 0 : _item$_extend16.unique_identification_number;
	}
	if (isMultiDayPrices && ((_item$_extend17 = item._extend) === null || _item$_extend17 === void 0 ? void 0 : _item$_extend17.multiDayPrices)) {
		var _item$_extend18;
		metadata.multiDayPrices = (_item$_extend18 = item._extend) === null || _item$_extend18 === void 0 ? void 0 : _item$_extend18.multiDayPrices;
	}
	if ((_item$_extend19 = item._extend) === null || _item$_extend19 === void 0 ? void 0 : _item$_extend19.unique_identification_number) {
		var _item$_extend20;
		metadata.unique_identification_number = (_item$_extend20 = item._extend) === null || _item$_extend20 === void 0 ? void 0 : _item$_extend20.unique_identification_number;
	}
	if (item.discount_list && (0, _pisell_utils.isArr)(item.discount_list)) metadata.product_discount_difference = item.discount_list.filter((item) => {
		var _item$metadata;
		return !(item === null || item === void 0 || (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.custom_product_bundle_map_id);
	}).reduce((pre, cur) => {
		var _cur$metadata3;
		return pre + ((cur === null || cur === void 0 || (_cur$metadata3 = cur.metadata) === null || _cur$metadata3 === void 0 ? void 0 : _cur$metadata3.product_discount_difference) || 0);
	}, 0);
	return metadata;
};
const formatProductMetadata = (item) => {
	var _item$_extend21, _item$_extend23, _item$_extend25, _item$_extend27, _item$_extend29, _item$_extend31;
	let metadata = {};
	if ((_item$_extend21 = item._extend) === null || _item$_extend21 === void 0 ? void 0 : _item$_extend21.surcharge_fee) {
		var _item$_extend22;
		metadata.main_product_attached_bundle_surcharge_fee = (_item$_extend22 = item._extend) === null || _item$_extend22 === void 0 ? void 0 : _item$_extend22.surcharge_fee;
	}
	if ((_item$_extend23 = item._extend) === null || _item$_extend23 === void 0 ? void 0 : _item$_extend23.main_product_attached_bundle_tax_fee) {
		var _item$_extend24;
		metadata.main_product_attached_bundle_tax_fee = (_item$_extend24 = item._extend) === null || _item$_extend24 === void 0 ? void 0 : _item$_extend24.main_product_attached_bundle_tax_fee;
	}
	if ((_item$_extend25 = item._extend) === null || _item$_extend25 === void 0 ? void 0 : _item$_extend25.surcharge_rounding_remainder) {
		var _item$_extend26;
		metadata.surcharge_rounding_remainder = (_item$_extend26 = item._extend) === null || _item$_extend26 === void 0 ? void 0 : _item$_extend26.surcharge_rounding_remainder;
	}
	if ((_item$_extend27 = item._extend) === null || _item$_extend27 === void 0 ? void 0 : _item$_extend27.tax_fee_rounding_remainder) {
		var _item$_extend28;
		metadata.tax_fee_rounding_remainder = (_item$_extend28 = item._extend) === null || _item$_extend28 === void 0 ? void 0 : _item$_extend28.tax_fee_rounding_remainder;
	}
	if ((_item$_extend29 = item._extend) === null || _item$_extend29 === void 0 ? void 0 : _item$_extend29.relation_surcharge_ids) {
		var _item$_extend30;
		metadata.relation_surcharge_ids = (_item$_extend30 = item._extend) === null || _item$_extend30 === void 0 ? void 0 : _item$_extend30.relation_surcharge_ids;
	}
	if (item.discount_list && (0, _pisell_utils.isArr)(item.discount_list)) metadata.product_discount_difference = item.discount_list.filter((item) => {
		var _item$metadata2;
		return !(item === null || item === void 0 || (_item$metadata2 = item.metadata) === null || _item$metadata2 === void 0 ? void 0 : _item$metadata2.custom_product_bundle_map_id);
	}).reduce((pre, cur) => {
		var _cur$metadata4;
		return pre + ((cur === null || cur === void 0 || (_cur$metadata4 = cur.metadata) === null || _cur$metadata4 === void 0 ? void 0 : _cur$metadata4.product_discount_difference) || 0);
	}, 0);
	if ((_item$_extend31 = item._extend) === null || _item$_extend31 === void 0 ? void 0 : _item$_extend31.unique_identification_number) {
		var _item$_extend32;
		metadata.unique_identification_number = (_item$_extend32 = item._extend) === null || _item$_extend32 === void 0 ? void 0 : _item$_extend32.unique_identification_number;
	}
	return metadata;
};
const getSourceProductPrice = (item) => {
	var _item$_extend33, _item$price2, _item$_extend34, _item$variant2;
	const other = (_item$_extend33 = item._extend) === null || _item$_extend33 === void 0 ? void 0 : _item$_extend33.other;
	let price = Number((_item$price2 = item === null || item === void 0 ? void 0 : item.price) !== null && _item$price2 !== void 0 ? _item$price2 : (_item$_extend34 = item._extend) === null || _item$_extend34 === void 0 ? void 0 : _item$_extend34.price);
	if ((other === null || other === void 0 ? void 0 : other.product_variant_id) > 0 && (item === null || item === void 0 || (_item$variant2 = item.variant) === null || _item$variant2 === void 0 ? void 0 : _item$variant2.length) > 0) {
		const variant = item.variant.find((d) => d.id === (other === null || other === void 0 ? void 0 : other.product_variant_id));
		if (variant) price = Number(variant === null || variant === void 0 ? void 0 : variant.price) || 0;
	}
	return price;
};
const getMainProductSellingPrice = (item) => {
	var _item$_extend35, _item$_extend36, _item$_extend$other, _item$_extend$other2, _item$_extend37;
	if (!(0, _pisell_utils.isNumber)((_item$_extend35 = item._extend) === null || _item$_extend35 === void 0 ? void 0 : _item$_extend35.main_product_selling_price) && !(0, _pisell_utils.isString)((_item$_extend36 = item._extend) === null || _item$_extend36 === void 0 ? void 0 : _item$_extend36.main_product_selling_price)) return;
	let optionPrice = new decimal_js.default(0);
	if (((_item$_extend$other = item._extend.other) === null || _item$_extend$other === void 0 ? void 0 : _item$_extend$other.option) && Array.isArray((_item$_extend$other2 = item._extend.other) === null || _item$_extend$other2 === void 0 ? void 0 : _item$_extend$other2.option)) {
		var _item$_extend$other3;
		optionPrice = (_item$_extend$other3 = item._extend.other) === null || _item$_extend$other3 === void 0 ? void 0 : _item$_extend$other3.option.reduce((accumulator, currentValue) => {
			const currentPrice = new decimal_js.default(currentValue.price || 0);
			const currentNum = new decimal_js.default(currentValue.num || 0);
			return accumulator.add(currentPrice.mul(currentNum));
		}, optionPrice);
		if (item === null || item === void 0 ? void 0 : item.booking_id) optionPrice = optionPrice.sub(require_amount.getOptionDiscountAmount((item === null || item === void 0 ? void 0 : item.discount_list) || []));
	}
	return new decimal_js.default((_item$_extend37 = item._extend) === null || _item$_extend37 === void 0 ? void 0 : _item$_extend37.main_product_selling_price).add(optionPrice).toNumber();
};
/**
* @title: 格式化商品数据
* @description:
* @param {any} item
* @return {*}
* @Author: WangHan
* @Date: 2024-12-07 14:21
*/
const formatProducts = (item, quantity, options) => {
	var _item$_extend38, _getMainProductSellin, _item$_extend39, _item$_extend40, _item$_extend41, _item$_extend42;
	const total = Number(item._extend.total);
	const discount_amount = Number(item._extend.origin_total) - total;
	const bundleDiscountList = getBundleDiscountList(((_item$_extend38 = item._extend) === null || _item$_extend38 === void 0 || (_item$_extend38 = _item$_extend38.other) === null || _item$_extend38 === void 0 ? void 0 : _item$_extend38.bundle) || []);
	const discount_list = [...(item === null || item === void 0 ? void 0 : item.discount_list) || []];
	discount_list.push(...bundleDiscountList);
	const discount_type = discount_amount < 0 ? "change_price" : "discount";
	discount_list.forEach((d) => {
		d.metadata = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, d.metadata), {}, { unique_identification_number: `${item._extend.unique_identification_number}_${d.discount.resource_id}` });
		if ((options === null || options === void 0 ? void 0 : options.isServiceEdit) && (options === null || options === void 0 ? void 0 : options.isMultiDay)) delete d.id;
	});
	const notManualDiscount = discount_list.filter((item) => item.type !== "product");
	if (item.isCustomItem) item.product_id = 0;
	return {
		name: item.name,
		num: quantity || item._extend.quantity,
		price: formatProductPrice(item),
		"main_product_selling_price": (_getMainProductSellin = getMainProductSellingPrice(item)) !== null && _getMainProductSellin !== void 0 ? _getMainProductSellin : formatProductPrice(item),
		source_product_price: getSourceProductPrice(item),
		product_id: (item === null || item === void 0 ? void 0 : item.product_id) || item.product_id === 0 ? item.product_id : item.id,
		discount_amount: notManualDiscount.length > 0 ? 0 : discount_amount,
		product_discount_reason: item._extend.discount_reason || "",
		is_charge_tax: item.is_charge_tax,
		note: ((_item$_extend39 = item._extend) === null || _item$_extend39 === void 0 ? void 0 : _item$_extend39.note) || "",
		discountway: item._extend.discountway || "num",
		discount_per: item._extend.discount_per,
		discount_type: item._extend.discount_type || discount_type,
		order_detail_id: item._extend.order_detail_id || 0,
		product_bundle: formatProductBundle((_item$_extend40 = item._extend) === null || _item$_extend40 === void 0 || (_item$_extend40 = _item$_extend40.other) === null || _item$_extend40 === void 0 ? void 0 : _item$_extend40.bundle),
		bundle_edit: item._extend.bundle_edit || bundleDiscountList.length ? 1 : 0,
		product_option_item: formatProductOption((_item$_extend41 = item._extend) === null || _item$_extend41 === void 0 || (_item$_extend41 = _item$_extend41.other) === null || _item$_extend41 === void 0 ? void 0 : _item$_extend41.option),
		product_variant_id: ((_item$_extend42 = item._extend) === null || _item$_extend42 === void 0 || (_item$_extend42 = _item$_extend42.other) === null || _item$_extend42 === void 0 ? void 0 : _item$_extend42.product_variant_id) || 0,
		discount_list: notManualDiscount,
		metadata: formatProductMetadata(item),
		calculated_original_price: formatOriginalPrice(item),
		calculated_selling_price: formatSellingPrice(item),
		tax_fee: item._extend.tax_fee,
		custom_deposit_data: item.custom_deposit_data,
		is_price_include_tax: options === null || options === void 0 ? void 0 : options.is_price_include_tax
	};
};
/**
* @title: 格式化跨日预约商品列表
* @description:
* @param {any} data
* @return {*}
* @Author: WangHan
* @Date: 2024-12-17 11:06
*/
const formatAppointmentProductLists = (data) => {
	try {
		var _Array$from;
		let list = data.service.value;
		list = list.filter((item) => {
			var _item$_extend44;
			return ((_item$_extend44 = item._extend) === null || _item$_extend44 === void 0 ? void 0 : _item$_extend44.holder_id) && !require_utils$1.isNormalProductByDurationSchedule(item);
		});
		const groupMap = /* @__PURE__ */ new Map();
		list.forEach((item) => {
			var _item$_extend45;
			const key = (_item$_extend45 = item._extend) === null || _item$_extend45 === void 0 ? void 0 : _item$_extend45.holder_id;
			if (!groupMap.has(key)) groupMap.set(key, []);
			groupMap.get(key).push(item);
		});
		(_Array$from = Array.from(groupMap.values())) === null || _Array$from === void 0 || _Array$from.reduce((acc, group) => acc.concat(group), []);
		return list || [];
	} catch (error) {}
};
const getHolderByIndex = (holder, index) => {
	var _holder$form_record;
	return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, holder), {}, { form_record: [holder === null || holder === void 0 || (_holder$form_record = holder.form_record) === null || _holder$form_record === void 0 ? void 0 : _holder$form_record[index]].filter(Boolean) });
};
/**
* @title: 是否包含跨日预约商品
* @description:
* @param {any} data
* @return {*}
*/
const getHasMultiDayProduct = (data) => {
	return data.service.value.some((item) => {
		var _item$_extend46;
		return (_item$_extend46 = item._extend) === null || _item$_extend46 === void 0 ? void 0 : _item$_extend46.groupId;
	});
};
/**
* @title: 预约商品信息
* @description:
* @param {any} data
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-02-19 09:26
*/
const formatBookings = (data, values) => {
	const isDay = require_utils$1.isDaysBooking(data);
	const isEvent = require_utils$1.isEventBooking(data);
	let list = data.service.value;
	if (isDay) list = formatAppointmentProductLists(data);
	if (isEvent) list = list.filter((d) => {
		var _d$_extend;
		return (_d$_extend = d._extend) === null || _d$_extend === void 0 ? void 0 : _d$_extend.holder_id;
	});
	if (!isDay && !isEvent) list = list.filter((item) => !require_utils$1.isNormalProductByDurationSchedule(item));
	let dayParams = {};
	const eventOtherParams = data.eventOtherParams;
	let bookings = [];
	let obj = {};
	list.forEach((item) => {
		var _item$_extend47, _item$_extend52, _item$_extend53, _item$_extend54, _data$service2;
		if (item.isCustomItem) {
			bookings.push(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item._extend), {}, { product_id: 0 }));
			return;
		}
		const isMultiDay = (_item$_extend47 = item._extend) === null || _item$_extend47 === void 0 ? void 0 : _item$_extend47.groupId;
		let start_date = item._extend.startDate.format("YYYY-MM-DD");
		let end_date = item._extend.endDate.format("YYYY-MM-DD");
		let end_time = item._extend.endDate.format("HH:mm");
		let start_time = item._extend.startDate.format("HH:mm");
		if (require_utils$2.isNowTime(item._extend.start_time)) start_time = require_utils$2.getNowTime();
		dayParams.holder = formatHolders(data, item, values);
		if (isDay) {
			var _item$_extend48, _item$_extend49, _item$_extend50, _item$_extend51;
			const { startDateTime, endDateTime } = require_utils$1.formatDayTime({
				startDate: ((_item$_extend48 = item._extend) === null || _item$_extend48 === void 0 ? void 0 : _item$_extend48.startDate) || ((_item$_extend49 = item._extend) === null || _item$_extend49 === void 0 ? void 0 : _item$_extend49.start_date),
				endDate: ((_item$_extend50 = item._extend) === null || _item$_extend50 === void 0 ? void 0 : _item$_extend50.endDate) || ((_item$_extend51 = item._extend) === null || _item$_extend51 === void 0 ? void 0 : _item$_extend51.end_date)
			});
			start_date = (0, dayjs.default)(startDateTime).format("YYYY-MM-DD");
			start_time = (0, dayjs.default)(startDateTime).format("HH:mm");
			end_date = (0, dayjs.default)(endDateTime).format("YYYY-MM-DD");
			end_time = (0, dayjs.default)(endDateTime).format("HH:mm");
			dayParams.relation_products = (item === null || item === void 0 ? void 0 : item.relation_products) || [];
			dayParams.holder = formatHolders(data, item, values);
			dayParams.duration = require_utils$2.getDays({ _extend: {
				start_date: startDateTime,
				end_date: endDateTime
			} }, "minutes");
		}
		if (!((_item$_extend52 = item._extend) === null || _item$_extend52 === void 0 ? void 0 : _item$_extend52.unique_identification_number)) {
			const unique_identification_number = (0, _pisell_utils.getUniqueId)();
			item._extend.unique_identification_number = unique_identification_number;
		}
		const isSetGroupId = obj[item === null || item === void 0 || (_item$_extend53 = item._extend) === null || _item$_extend53 === void 0 ? void 0 : _item$_extend53.groupId];
		if (!((_item$_extend54 = item._extend) === null || _item$_extend54 === void 0 ? void 0 : _item$_extend54.unique_identification_number)) {
			const unique_identification_number = (0, _pisell_utils.getUniqueId)();
			item._extend.unique_identification_number = unique_identification_number;
		}
		const booking = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
			product: formatProducts(item, void 0, {
				isServiceEdit: (_data$service2 = data.service) === null || _data$service2 === void 0 ? void 0 : _data$service2.edit,
				isMultiDay,
				is_price_include_tax: data.is_price_include_tax
			}),
			relation_products: formatRelationDetails(item._extend.relation_details),
			id: item.booking_id || 0,
			relation_id: item._extend.relation_id || 0,
			like_status: item._extend.like_status || "common",
			relation_type: item._extend.relation_type || "",
			is_all: false,
			schedule_id: item._extend.schedule_id || 0,
			end_time,
			sub_type: isDay ? "days" : getSubType(item),
			select_date: start_date,
			start_time,
			end_date,
			start_date,
			duration: isDay || isMultiDay ? 0 : require_utils$2.flexibleObj.getValue(item._extend.duration),
			number: item._extend.capacity ? require_utilsByBooking.getSumCapacity({ cacheItem: item }) : item._extend.number || 1,
			resources: formatResource(item),
			"metadata": formatMetaData(item, !isSetGroupId),
			note: item._extend.note
		}, eventOtherParams || {}), dayParams);
		if (!isSetGroupId) obj[item._extend.groupId] = true;
		if (isDay || isMultiDay) {
			bookings.push(booking);
			return;
		}
		for (let i = 0; i < (item._extend.quantity || 1); i++) {
			const newItem = (0, lodash.cloneDeep)(item);
			newItem._extend.unique_identification_number = `${item._extend.unique_identification_number}_${i}`;
			if (i > 0) {
				var _newItem$_extend;
				newItem._extend.surcharge_rounding_remainder = void 0;
				if (((_newItem$_extend = newItem._extend) === null || _newItem$_extend === void 0 || (_newItem$_extend = _newItem$_extend.other) === null || _newItem$_extend === void 0 || (_newItem$_extend = _newItem$_extend.bundle) === null || _newItem$_extend === void 0 ? void 0 : _newItem$_extend.length) > 0) newItem._extend.other.bundle.forEach((bundleItem) => {
					bundleItem.surcharge_rounding_remainder = void 0;
				});
			}
			bookings.push(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, booking), {}, {
				"product": formatProducts(newItem, 1),
				"number": item._extend.capacity ? require_utilsByBooking.getSumCapacity({ cacheItem: newItem }, false) : newItem._extend.number || 1,
				"holder": getHolderByIndex(booking.holder, i),
				"metadata": formatMetaData(newItem, false)
			}));
		}
	});
	return bookings;
};
/**
* @title: 普通商品
* @description:
* @param {any} data
* @return {*}
* @Author: WangHan
* @Date: 2024-12-06 21:26
*/
const formatRelationProducts = (data, type) => {
	var _data$service3;
	let list = ((_data$service3 = data.service) === null || _data$service3 === void 0 ? void 0 : _data$service3.value) || [];
	list = list.filter((item) => {
		var _item$_extend55;
		return type === "day" ? require_utils$1.isNormalProductByDurationSchedule(item) : ((_item$_extend55 = item._extend) === null || _item$_extend55 === void 0 ? void 0 : _item$_extend55.holder_id) && require_status.appointmentTypes.includes(item.extension_type);
	});
	let relationProducts = [];
	relationProducts = list.map((item) => {
		var _item$_extend56, _data$service4;
		if (!((_item$_extend56 = item._extend) === null || _item$_extend56 === void 0 ? void 0 : _item$_extend56.unique_identification_number)) {
			const unique_identification_number = (0, _pisell_utils.getUniqueId)();
			item._extend.unique_identification_number = unique_identification_number;
		}
		return formatProducts(item, void 0, {
			isServiceEdit: (_data$service4 = data.service) === null || _data$service4 === void 0 ? void 0 : _data$service4.edit,
			isMultiDay: false,
			is_price_include_tax: data.is_price_include_tax
		});
	});
	return relationProducts || [];
};
const formatNormalProducts = (data) => {
	var _data$service5;
	let list = ((_data$service5 = data.service) === null || _data$service5 === void 0 ? void 0 : _data$service5.value) || [];
	list = list.filter((item) => require_utils$1.isNormalProductByDurationSchedule(item));
	let relationProducts = [];
	relationProducts = list.map((item) => {
		var _item$_extend57, _data$service6;
		if (!((_item$_extend57 = item._extend) === null || _item$_extend57 === void 0 ? void 0 : _item$_extend57.unique_identification_number)) {
			const unique_identification_number = (0, _pisell_utils.getUniqueId)();
			item._extend.unique_identification_number = unique_identification_number;
		}
		return formatProducts(item, void 0, {
			isServiceEdit: (_data$service6 = data.service) === null || _data$service6 === void 0 ? void 0 : _data$service6.edit,
			isMultiDay: false,
			is_price_include_tax: data.is_price_include_tax
		});
	});
	return relationProducts || [];
};
/**
* @title: 预约主体信息
* @description:
* @param {any} state
* @param {any} values
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-02-19 09:26
*/
const formatHolder = (state, values) => {
	var _state$bookingConfig2, _state$pet$value, _state$pet;
	const form_id = (state === null || state === void 0 || (_state$bookingConfig2 = state.bookingConfig) === null || _state$bookingConfig2 === void 0 || (_state$bookingConfig2 = _state$bookingConfig2.config) === null || _state$bookingConfig2 === void 0 || (_state$bookingConfig2 = _state$bookingConfig2.basic) === null || _state$bookingConfig2 === void 0 || (_state$bookingConfig2 = _state$bookingConfig2.appointment_subject) === null || _state$bookingConfig2 === void 0 ? void 0 : _state$bookingConfig2.form_id) || 0;
	const petValue = ((_state$pet$value = state.pet.value) === null || _state$pet$value === void 0 ? void 0 : _state$pet$value[form_id]) || [];
	let obj = {
		customer_id: values.customer_id,
		form_id,
		form_record: petValue.map((d) => d.form_record_id)
	};
	if (!petValue.length && ((_state$pet = state.pet) === null || _state$pet === void 0 || (_state$pet = _state$pet.originValue) === null || _state$pet === void 0 || (_state$pet = _state$pet.form_record) === null || _state$pet === void 0 ? void 0 : _state$pet.length)) obj.form_record = state.pet.originValue.form_record;
	if (state.isFranchisee) obj.is_source_shop = 1;
	return obj;
};
const formatValues = (state) => {
	var _values$bookings, _values$bookings3, _state$contacts_info;
	let values = {
		type: state.renderType === "eventBooking" ? "event_booking_v2" : "appointment_booking",
		platform: state.isBookingTicket2 ? "pos" : "shop",
		sales_channel: "my_pisel",
		order_sales_channel: state.isBookingTicket2 ? "pos" : "online_store",
		bookings: [],
		customer_id: 1,
		customer_name: "",
		holder: {},
		relation_products: [],
		metadata: {}
	};
	const isDay = require_utils$1.isDaysBooking(state);
	const isHasMultiDay = getHasMultiDayProduct(state);
	const isEvent = require_utils$1.isEventBooking(state);
	if (!state.bookingId || isDay) values.shop_note = formatNote(state);
	if (state.shop_discount && Number(state.shop_discount) > 0) {
		values.shop_discount = state.shop_discount;
		values.shop_discount_reason = "";
	}
	if (state.contacts) {
		var _state$contacts;
		values.contacts = ((_state$contacts = state.contacts) === null || _state$contacts === void 0 ? void 0 : _state$contacts.value) || [];
	}
	const customer = formatCustomer(state);
	values = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, values), customer);
	values.holder = formatHolder(state, values);
	if (isEvent) values.relation_products = formatRelationProducts(state, "event");
	else {
		var _values$relation_prod;
		values.relation_products = formatNormalProducts(state);
		if (!((_values$relation_prod = values.relation_products) === null || _values$relation_prod === void 0 ? void 0 : _values$relation_prod.length)) values.relation_products = void 0;
	}
	values.bookings = formatBookings(state, values);
	if ((values === null || values === void 0 || (_values$bookings = values.bookings) === null || _values$bookings === void 0 || (_values$bookings = _values$bookings[0]) === null || _values$bookings === void 0 ? void 0 : _values$bookings.select_date) && !(state === null || state === void 0 ? void 0 : state.bookingId)) {
		var _values$bookings2;
		let startTime = `${values === null || values === void 0 || (_values$bookings2 = values.bookings) === null || _values$bookings2 === void 0 || (_values$bookings2 = _values$bookings2[0]) === null || _values$bookings2 === void 0 ? void 0 : _values$bookings2.start_time}`;
		startTime = `${startTime || "00:00"}:00`;
		values.schedule_date = `${values.bookings[0].select_date} ${startTime}`;
	}
	if (isDay || isHasMultiDay) values.metadata.is_multiday_appointment = 1;
	if (!(values === null || values === void 0 || (_values$bookings3 = values.bookings) === null || _values$bookings3 === void 0 ? void 0 : _values$bookings3.length)) values.type = "virtual";
	values.contacts_info = ((_state$contacts_info = state.contacts_info) === null || _state$contacts_info === void 0 ? void 0 : _state$contacts_info.value) || null;
	return values;
};
const checkHolderIsError = (state) => {
	var _state$client;
	let errorRowKey = "";
	const holderError = state.service.value.some((item) => {
		var _item$_extend$holder_2, _item$_extend59;
		if (require_utils$1.isNormalProductByDurationSchedule(item)) return false;
		if (item._extend.quantity === 1 && (!item._extend.holder_id || ((_item$_extend$holder_2 = item._extend.holder_id) === null || _item$_extend$holder_2 === void 0 ? void 0 : _item$_extend$holder_2.length) === 0)) {
			var _item$_extend58;
			errorRowKey = require_utils$1.genRowKey(item === null || item === void 0 || (_item$_extend58 = item._extend) === null || _item$_extend58 === void 0 || (_item$_extend58 = _item$_extend58.other) === null || _item$_extend58 === void 0 ? void 0 : _item$_extend58.rowKey);
			return true;
		}
		if (item._extend.quantity > 1 && ((_item$_extend59 = item._extend) === null || _item$_extend59 === void 0 || (_item$_extend59 = _item$_extend59.holder_id) === null || _item$_extend59 === void 0 ? void 0 : _item$_extend59.length) !== item._extend.quantity) {
			var _item$_extend60;
			errorRowKey = require_utils$1.genRowKey(item === null || item === void 0 || (_item$_extend60 = item._extend) === null || _item$_extend60 === void 0 || (_item$_extend60 = _item$_extend60.other) === null || _item$_extend60 === void 0 ? void 0 : _item$_extend60.rowKey);
			return true;
		}
		return false;
	});
	const clientId = (_state$client = state.client) === null || _state$client === void 0 || (_state$client = _state$client.value) === null || _state$client === void 0 ? void 0 : _state$client.id;
	if (holderError) {
		const dom = document.getElementById(errorRowKey);
		if (dom) dom.scrollIntoView({
			behavior: "smooth",
			block: "start"
		});
	}
	if (holderError && (!clientId || clientId === 0 || clientId === 1)) (0, _pisell_materials.PisellToast)({ content: _pisell_utils.locales.getText("pisell2.text.please-select-the-customer-first") });
	return holderError;
};
//#endregion
exports.checkHolderIsError = checkHolderIsError;
exports.checkResourcesFormat = checkResourcesFormat;
exports.formatOriginalPrice = formatOriginalPrice;
exports.formatProductBundle = formatProductBundle;
exports.formatProductOption = formatProductOption;
exports.formatProductPrice = formatProductPrice;
exports.formatSellingPrice = formatSellingPrice;
exports.formatValues = formatValues;
exports.getBundleDiscountList = getBundleDiscountList;
exports.getMainProductSellingPrice = getMainProductSellingPrice;
exports.getSourceProductPrice = getSourceProductPrice;
