require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_utils = require("../../../booking/info2/service/addService/utils.js");
const require_utils$1 = require("../../../booking/info/service2/utils.js");
const require_utils$2 = require("../../../booking/utils.js");
let _pisell_utils = require("@pisell/utils");
//#region src/components/ticketBooking/components/addServiceVariant/utils.ts
const formatVariantGroupTitle = (title, variant_group = []) => {
	try {
		const formattedTitle = variant_group === null || variant_group === void 0 ? void 0 : variant_group.map((item) => {
			const value = title[item === null || item === void 0 ? void 0 : item.position_id];
			if (value) return `${(item === null || item === void 0 ? void 0 : item.name) || "-"}: ${value}`;
			return "";
		}).filter(Boolean);
		return formattedTitle === null || formattedTitle === void 0 ? void 0 : formattedTitle.join(", ");
	} catch (error) {
		console.log(error);
	}
};
const createCacheItem = (detail, values, state) => {
	let cacheItem = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, detail), {}, {
		product_id: detail.id,
		_id: (0, _pisell_utils.getUniqueId)(),
		_key: values.key,
		_extend: {
			start_date: state.date.value,
			quantity: (values === null || values === void 0 ? void 0 : values.quantity) || 1,
			price: detail.price,
			product_name: detail.title,
			other: values
		},
		new: 1
	});
	let _total = require_utils$2.getProductTotalPrice(cacheItem);
	cacheItem._extend.total = _total;
	cacheItem._extend.origin_total = _total;
	cacheItem.currentDefaultTime = state.date.value.format("HH:mm");
	cacheItem = require_utils$1.isNormalProductByDurationSchedule(cacheItem) ? cacheItem : require_utils.getProductExtend({
		cacheItem,
		state,
		isDisabledTime: true
	});
	return cacheItem;
};
const formatDataKey = (data) => {
	var _data$option, _data$bundle;
	const _data = require_objectSpread2._objectSpread2({}, data);
	let key = `${_data.product_id}`;
	let rowKey = key;
	if (_data.product_variant_id) {
		let str = `_${_data.product_variant_id}`;
		key += str;
		rowKey += str;
	}
	if (_data === null || _data === void 0 || (_data$option = _data.option) === null || _data$option === void 0 ? void 0 : _data$option.length) for (const item of _data.option) {
		let str = `_${item.option_group_id}_${item.product_option_item_id}`;
		key += str;
		rowKey += `${str}_${item.quantity || item.num}`;
	}
	if (_data === null || _data === void 0 || (_data$bundle = _data.bundle) === null || _data$bundle === void 0 ? void 0 : _data$bundle.length) for (const item of _data.bundle) {
		let str = `_${item.group_id}_${item.bundle_product_id}`;
		key += str;
		rowKey += `${str}_${item.quantity || item.num}`;
		if (item === null || item === void 0 ? void 0 : item.option) for (const opt of item.option) {
			let str = `_${opt.option_group_id}_${opt.product_option_item_id}`;
			key += str;
			rowKey += `${str}_${opt.quantity || opt.num}`;
		}
	}
	if (_data === null || _data === void 0 ? void 0 : _data.session) {
		var _data$session;
		let str = `_${_data === null || _data === void 0 || (_data$session = _data.session) === null || _data$session === void 0 ? void 0 : _data$session.key}`;
		key += str;
		rowKey += str;
	}
	if (_data === null || _data === void 0 ? void 0 : _data.schedule) {
		var _data$schedule;
		let str = `_${_data === null || _data === void 0 || (_data$schedule = _data.schedule) === null || _data$schedule === void 0 ? void 0 : _data$schedule.key}`;
		key += str;
		rowKey += str;
	}
	_data.key = key;
	_data.rowKey = rowKey;
	return _data;
};
const isOption = (item) => {
	var _item$option_group;
	return (item === null || item === void 0 ? void 0 : item.option_group_count) > 0 || (item === null || item === void 0 || (_item$option_group = item.option_group) === null || _item$option_group === void 0 ? void 0 : _item$option_group.length) > 0;
};
const isBundle = (item) => {
	return (item === null || item === void 0 ? void 0 : item.bundle_group_count) > 0;
};
const genValues = (item, code) => {
	var _item$variant_group, _item$option_group2;
	let params = {
		bundle: [],
		option: [],
		product_id: item.id,
		product_variant_id: 0,
		quantity: 1,
		rowKey: "",
		session: null
	};
	if ((item === null || item === void 0 || (_item$variant_group = item.variant_group) === null || _item$variant_group === void 0 ? void 0 : _item$variant_group.length) || (item === null || item === void 0 || (_item$option_group2 = item.option_group) === null || _item$option_group2 === void 0 ? void 0 : _item$option_group2.length)) {
		const variantItem = item.variant.find((item) => item.code === code || item.barcode === code);
		const variant_group_title = formatVariantGroupTitle(variantItem === null || variantItem === void 0 ? void 0 : variantItem.title, (item === null || item === void 0 ? void 0 : item.variant_group) || []);
		params.price = Number((variantItem === null || variantItem === void 0 ? void 0 : variantItem.price) || 0);
		params.product_variant_id = variantItem.id;
		params.product_option_string = variant_group_title;
	}
	return formatDataKey(params);
};
let handleSetService = null;
const setHandleSetService = (fn) => {
	handleSetService = fn;
};
const getHandleSetService = () => {
	return handleSetService;
};
//#endregion
exports.createCacheItem = createCacheItem;
exports.genValues = genValues;
exports.getHandleSetService = getHandleSetService;
exports.isBundle = isBundle;
exports.isOption = isOption;
exports.setHandleSetService = setHandleSetService;
