import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { getErrorList } from "../info2/utilsByBooking.js";
import { getResourceByIds } from "../info2/service/addService/utils.js";
import { request } from "../../../utils/index.js";
import { getFormatResourceListAndMap } from "../info2/utils.js";
import { _formatBookingDetail } from "../../ticketBooking/components/ticketBooking/index.js";
import dayjs from "dayjs";
//#region src/components/booking/editBookingModal/serve.ts
/**
* @title: 获取预约详情
* @description: 获取预约详细信息
* @param {number} bookingId
* @param {any} params
* @return {*}
* @Author: cong.shao
* @Date: 2025-06-17 14:22
*/
const getBookingDetail = function() {
	var _ref = _asyncToGenerator(function* (bookingId, params) {
		let { data } = yield request.getRequest().get(`/shop/order/appointment/${bookingId}`, params);
		return data;
	});
	return function getBookingDetail(_x, _x2) {
		return _ref.apply(this, arguments);
	};
}();
/**
* @title: 获取资源列表
* @description: 资源列表
* @param {any} params
* @return {*}
* @Author: cong.shao
* @Date: 2025-06-17 14:22
*/
const getResources = function() {
	var _ref2 = _asyncToGenerator(function* (params) {
		let { data } = yield request.getRequest().get(`/shop/schedule/resource/list`, params);
		return data;
	});
	return function getResources(_x3) {
		return _ref2.apply(this, arguments);
	};
}();
/**
* @title: 更新子预约时间及资源
* @description: 更新预约信息
* @param {string} orderId
* @param {any} params
* @return {*}
* @Author: cong.shao
* @Date: 2025-06-17 14:26
*/
const updateChildBooking = function() {
	var _ref3 = _asyncToGenerator(function* (orderId, params) {
		let { data } = yield request.getRequest().put(`/shop/v1.1/order/appointment/child-booking/${orderId}`, params);
		return data;
	});
	return function updateChildBooking(_x4, _x5) {
		return _ref3.apply(this, arguments);
	};
}();
const _getBookingDetail = function() {
	var _ref4 = _asyncToGenerator(function* (bookingId) {
		return yield getBookingDetail(bookingId);
	});
	return function _getBookingDetail(_x6) {
		return _ref4.apply(this, arguments);
	};
}();
const getChildrenBookingById = (bookings, id) => {
	return bookings.find((item) => item.booking_id === id);
};
const _getResources = function() {
	var _ref5 = _asyncToGenerator(function* (params, bookingId) {
		return getFormatResourceListAndMap(yield getResources(params), bookingId);
	});
	return function _getResources(_x7, _x8) {
		return _ref5.apply(this, arguments);
	};
}();
(function() {
	var _ref6 = _asyncToGenerator(function* (data, values) {
		console.log("submit data", data);
		console.log("submit values", values);
	});
	return function _editBooking(_x9, _x10) {
		return _ref6.apply(this, arguments);
	};
})();
const updateResourceErrors = (resourceMaps, cacheItem) => {
	var _cacheItem$_extend;
	if (cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend = cacheItem._extend) === null || _cacheItem$_extend === void 0 ? void 0 : _cacheItem$_extend.resource) {
		var _cacheItem$_extend2;
		let _resource = _objectSpread2({}, cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend2 = cacheItem._extend) === null || _cacheItem$_extend2 === void 0 ? void 0 : _cacheItem$_extend2.resource);
		for (let form_id in _resource) _resource[form_id] = _resource[form_id].map((d) => {
			var _resourceMaps$form_id;
			return _objectSpread2(_objectSpread2({}, d), {}, { errors: getErrorList(resourceMaps === null || resourceMaps === void 0 || (_resourceMaps$form_id = resourceMaps[form_id]) === null || _resourceMaps$form_id === void 0 || (_resourceMaps$form_id = _resourceMaps$form_id.renderListMaps) === null || _resourceMaps$form_id === void 0 ? void 0 : _resourceMaps$form_id[d === null || d === void 0 ? void 0 : d.relation_id], cacheItem) });
		});
		return _resource;
	}
};
const init = function() {
	var _ref7 = _asyncToGenerator(function* (params) {
		var _childrenBooking$_ext;
		const { toStartDate, toEndDate, event, fromResourceId, toResourceId } = params;
		let bookingOrigin = yield _getBookingDetail(event.order.id);
		let booking = _formatBookingDetail(bookingOrigin, {});
		const resources = yield _getResources({ date: dayjs(toStartDate).format("YYYY-MM-DD") });
		let childrenBooking = getChildrenBookingById(booking.service.value, event.id);
		console.log("childrenBooking", childrenBooking);
		const fromResource = resources.listMap[fromResourceId];
		const toResource = resources.listMap[toResourceId];
		const extraParams = { extraResources: [] };
		const _eventResources = (_childrenBooking$_ext = childrenBooking._extend) === null || _childrenBooking$_ext === void 0 ? void 0 : _childrenBooking$_ext.event_resources;
		if (Array.isArray(_eventResources)) _eventResources.forEach((item) => {
			extraParams.extraResources.push({
				id: item.relation_id,
				form_id: item.form_id
			});
		});
		if (toResource) extraParams.extraResources.push({
			form_id: toResource.form_id,
			id: toResource.id
		});
		const resourcesOrigin = getResourceByIds(resources.listMap, childrenBooking, extraParams);
		return {
			bookingOrigin,
			booking,
			childrenBooking,
			dataRange: {
				end_date: childrenBooking._extend.end_date,
				start_date: childrenBooking._extend.start_date
			},
			resourcesOrigin,
			fromResource,
			toResource
		};
	});
	return function init(_x11) {
		return _ref7.apply(this, arguments);
	};
}();
const createCacheItem = (cacheItem, values) => {
	var _values$toDate;
	console.log("values", values);
	if (!values || (values === null || values === void 0 || (_values$toDate = values.toDate) === null || _values$toDate === void 0 ? void 0 : _values$toDate.length) !== 2 || !(values === null || values === void 0 ? void 0 : values.resource)) return cacheItem;
	const { toDate, resource } = values || {};
	return _objectSpread2(_objectSpread2({}, cacheItem), {}, { _extend: _objectSpread2(_objectSpread2({}, cacheItem._extend), {}, {
		endDate: toDate[1],
		end_date: toDate[1],
		end_time: toDate[1].format("HH:mm"),
		startDate: toDate[0],
		start_date: toDate[0],
		start_time: toDate[0].format("HH:mm"),
		resource
	}) });
};
//#endregion
export { createCacheItem, init, updateChildBooking, updateResourceErrors };
