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 { concatBookings, createBookingItemsByGroup } from "../../service2/Group/utils.js";
import { getMultiDay, getProductExtend } from "../addService/utils.js";
import { isBookingProduct, isDayBookingProduct, isDaysBooking, isEventBooking } from "../../service2/utils.js";
import { deleteCacheParams, getProductTotalPrice, getServiceTimes, isSessionProduct } from "../../../utils.js";
import { useCallback, useMemo } from "react";
import { getUniqueId } from "@pisell/utils";
//#region src/components/booking/info/service/serviceManager/index.tsx
let lock = false;
const useService = ({ renderMode, onEdit, state, dispatch }) => {
	var _state$service2;
	const holderId = useMemo(() => {
		var _state$activeCartId;
		return Number((state === null || state === void 0 || (_state$activeCartId = state.activeCartId) === null || _state$activeCartId === void 0 ? void 0 : _state$activeCartId[0]) || 0);
	}, [state === null || state === void 0 ? void 0 : state.activeCartId]);
	const isDayBookingMode = useMemo(() => {
		return isDaysBooking(state);
	}, [state === null || state === void 0 ? void 0 : state.renderType]);
	const isEventBookingMode = useMemo(() => {
		return isEventBooking(state);
	}, [state === null || state === void 0 ? void 0 : state.renderType]);
	const createBookingData = function() {
		var _ref = _asyncToGenerator(function* ({ product }) {
			if (lock) return;
			lock = true;
			setTimeout(() => {
				lock = false;
			}, 2e3);
			const { _params } = product;
			let cacheItem = _objectSpread2(_objectSpread2({}, product), {}, {
				product_id: product.id,
				_id: getUniqueId(),
				_key: _params.key,
				_extend: {
					start_date: state.date.value,
					quantity: 1,
					price: product.price,
					product_name: product.title,
					other: JSON.parse(JSON.stringify(_params))
				},
				new: 1
			});
			if (isDayBookingMode || isEventBookingMode) {
				cacheItem._extend.holder_id = holderId;
				cacheItem._extend.end_date = state.date.end_date;
				cacheItem._extend.sub_type = "days";
			}
			if (isSessionProduct(cacheItem) && _params.session) {
				cacheItem._extend.start_date = _params.session.start_at;
				cacheItem._extend.start_time = _params.session.start_time;
				cacheItem._extend.end_date = _params.session.end_at;
			}
			let _total = getProductTotalPrice(cacheItem);
			cacheItem._extend.total = _total;
			cacheItem._extend.origin_total = _total;
			const _isEdit = !!state.service.cacheItem;
			if (_isEdit) {
				var _state$service$cacheI;
				cacheItem._id = (_state$service$cacheI = state.service.cacheItem) === null || _state$service$cacheI === void 0 ? void 0 : _state$service$cacheI._id;
				cacheItem.new = 0;
			}
			if (renderMode === "current") cacheItem.currentDefaultTime = state.date.value.format("HH:mm");
			cacheItem._extend = getProductExtend({
				cacheItem,
				state
			});
			if (isDayBookingMode) {
				cacheItem = yield getMultiDay({
					start_date: cacheItem._extend.startDate,
					end_date: cacheItem._extend.endDate,
					state,
					cacheItem
				});
				if (isDayBookingProduct(cacheItem, state)) cacheItem = concatBookings(createBookingItemsByGroup(cacheItem));
			}
			if (isEventBookingMode) cacheItem.autoClose = false;
			else if (isDayBookingMode && !isDayBookingProduct(cacheItem, state)) cacheItem.autoClose = true;
			else {
				var _state$service;
				cacheItem.autoClose = !_isEdit && (cacheItem._extend.resource || cacheItem._extend.resource === 0 || (((_state$service = state.service) === null || _state$service === void 0 ? void 0 : _state$service.resourcesOrigin) || []).length === 1);
			}
			return cacheItem;
		});
		return function createBookingData(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	const currentResource = useCallback((useResource) => {
		return state.service.resources.find((d) => d.form_record_id == useResource) || {};
	}, [state === null || state === void 0 || (_state$service2 = state.service) === null || _state$service2 === void 0 ? void 0 : _state$service2.resources]);
	const editBookingData = function() {
		var _ref2 = _asyncToGenerator(function* ({ changeValues }) {
			var _resourceItem$_extend;
			let _item = _objectSpread2(_objectSpread2({}, state.service.cacheItem), {}, { autoClose: false });
			let _extend = _objectSpread2(_objectSpread2({}, _item._extend), changeValues);
			if (isDayBookingProduct(state.service.cacheItem, state)) {
				_extend.quantity = 1;
				_extend.startDate = _extend === null || _extend === void 0 ? void 0 : _extend.start_date;
				_extend.endDate = _extend === null || _extend === void 0 ? void 0 : _extend.end_date;
			} else {
				const { startDate, endDate } = getServiceTimes({ _extend });
				_extend.startDate = startDate;
				_extend.endDate = endDate;
			}
			const resourceItem = currentResource(changeValues.resource);
			const { relation_type, item_id, schedule_id } = (resourceItem === null || resourceItem === void 0 || (_resourceItem$_extend = resourceItem._extend) === null || _resourceItem$_extend === void 0 ? void 0 : _resourceItem$_extend.usableTime) || {};
			_extend.relation_type = relation_type || (_extend === null || _extend === void 0 ? void 0 : _extend.relation_type) || "form";
			_extend.relation_id = item_id || (resourceItem === null || resourceItem === void 0 ? void 0 : resourceItem.form_record_id) || (_extend === null || _extend === void 0 ? void 0 : _extend.relation_id) || 0;
			_extend.schedule_id = schedule_id || (_extend === null || _extend === void 0 ? void 0 : _extend.schedule_id) || 0;
			if (_extend.bundle_edit !== void 0) _extend.bundle_edit = 1;
			_item._extend = _extend;
			_item._time = (/* @__PURE__ */ new Date()).getTime();
			return _item;
		});
		return function editBookingData(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	const createBooking = function() {
		var _ref3 = _asyncToGenerator(function* ({ booking }) {
			goEditPanel({ booking });
		});
		return function createBooking(_x3) {
			return _ref3.apply(this, arguments);
		};
	}();
	const editBooking = function() {
		var _ref4 = _asyncToGenerator(function* ({ booking }) {
			var _booking$_extend, _window$closeBookingE, _window;
			let _list = state.service.value;
			let _id = booking === null || booking === void 0 ? void 0 : booking._id;
			let groupId = booking === null || booking === void 0 || (_booking$_extend = booking._extend) === null || _booking$_extend === void 0 ? void 0 : _booking$_extend.groupId;
			if (groupId) {
				_list = _list.filter((d) => {
					var _d$_extend;
					return ((_d$_extend = d._extend) === null || _d$_extend === void 0 ? void 0 : _d$_extend.groupId) !== groupId;
				});
				_list = _list.concat(createBookingItemsByGroup(booking, true));
			} else if (!_list.some((d) => d._id === _id)) {
				if (!isBookingProduct(booking === null || booking === void 0 ? void 0 : booking.extension_type) || !isDayBookingMode) _list.push(booking);
			} else _list = _list.map((d) => d._id === _id ? _objectSpread2(_objectSpread2({}, booking), {}, { new: 0 }) : d);
			deleteCacheParams();
			(_window$closeBookingE = (_window = window).closeBookingEditModal) === null || _window$closeBookingE === void 0 || _window$closeBookingE.call(_window);
			dispatch({
				type: "setService",
				payload: {
					editModal: false,
					addModal: false,
					value: _list,
					edit: true,
					cacheItem: null,
					changeService: false
				}
			});
		});
		return function editBooking(_x4) {
			return _ref4.apply(this, arguments);
		};
	}();
	const goEditPanel = ({ booking }) => {
		var _window$openBookingEd, _window2;
		(_window$openBookingEd = (_window2 = window).openBookingEditModal) === null || _window$openBookingEd === void 0 || _window$openBookingEd.call(_window2);
		dispatch({
			type: "setService",
			payload: {
				editModal: true,
				cacheItem: booking,
				addModal: !booking.autoClose,
				changeService: false
			}
		});
		onEdit === null || onEdit === void 0 || onEdit(booking);
	};
	return {
		createBookingData,
		editBookingData,
		createBooking,
		editBooking,
		goEditPanel
	};
};
//#endregion
export { useService };
