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("../../service2/status.js");
const require_asyncToGenerator = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_serve = require("../serve.js");
const require_utils = require("../../service2/utils.js");
const require_utils$1 = require("../../../utils.js");
const require_utils$2 = require("../../utils.js");
const require_model = require("../../model.js");
const require_index = require("../../../components/drawer/index.js");
const require_index$1 = require("../../../components/addButton/index.js");
const require_useEngineContext = require("../../../../../hooks/useEngineContext.js");
const require_index$2 = require("../../../components/TabProduct/index.js");
const require_index$3 = require("../../dateRange/index.js");
const require_index$4 = require("../serviceManager/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_icon_es_SearchLg = require("@pisell/icon/es/SearchLg");
_pisell_icon_es_SearchLg = require_runtime.__toESM(_pisell_icon_es_SearchLg);
let ahooks = require("ahooks");
require("./index.less");
let lodash_debounce = require("lodash/debounce");
lodash_debounce = require_runtime.__toESM(lodash_debounce);
//#region src/components/booking/info/service/addService/index.tsx
const shopList = [
	2886,
	4422,
	5538,
	6930,
	6814,
	6813,
	6812,
	6087,
	6303,
	6080,
	6978,
	7386,
	6848,
	6305
];
const Info = (props) => {
	var _state$client2, _state$service2, _state$service5, _state$service7;
	const { onEdit, drawerProps, renderMode } = props;
	const [search, setSearch] = (0, react.useState)("");
	let { state, dispatch } = (0, react.useContext)(require_model.Context);
	const [loading, setLoading] = (0, react.useState)(false);
	const searchRef = (0, react.useRef)();
	const headerRef = (0, react.useRef)(null);
	const [headerHeight, setHeaderHeight] = (0, react.useState)(0);
	const context = require_useEngineContext.default();
	state = state || (props === null || props === void 0 ? void 0 : props.state);
	dispatch = dispatch || (props === null || props === void 0 ? void 0 : props.dispatch);
	require_utils$1.request.setRequest(context.appHelper.utils.request);
	const { createBookingData, createBooking } = require_index$4.useService({
		renderMode,
		onEdit,
		state,
		dispatch
	});
	/**
	* @title: 是否是跨日预约
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2024-12-09 15:46
	*/
	const isDay = (0, react.useMemo)(() => {
		return require_utils.isDayBooking(state);
	}, [state.renderType]);
	/**
	* @title: 监听元素高度变化的函数
	* @description:
	* @return {*}
	* @Author: WangHan
	* @Date: 2024-12-10 17:13
	*/
	const handleResize = () => {
		if (headerRef === null || headerRef === void 0 ? void 0 : headerRef.current) {
			var _headerRef$current;
			setHeaderHeight((_headerRef$current = headerRef.current) === null || _headerRef$current === void 0 ? void 0 : _headerRef$current.offsetHeight);
		}
	};
	const debouncedHandleResize = (0, lodash_debounce.default)(handleResize, 100);
	(0, react.useEffect)(() => {
		if (isDay && state.isTerminalDayBookingPage) {
			handleResize();
			window.addEventListener("resize", debouncedHandleResize);
		}
		return () => {
			window.removeEventListener("resize", debouncedHandleResize);
		};
	}, [debouncedHandleResize]);
	/**
	* @title: 客户id
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2024-12-24 10:33
	*/
	const customerId = (0, react.useMemo)(() => {
		var _state$client;
		return ((_state$client = state.client) === null || _state$client === void 0 || (_state$client = _state$client.value) === null || _state$client === void 0 ? void 0 : _state$client.id) || 1;
	}, [(_state$client2 = state.client) === null || _state$client2 === void 0 || (_state$client2 = _state$client2.value) === null || _state$client2 === void 0 ? void 0 : _state$client2.id]);
	/**
	* @title: 如果商品只有一个, 则自动设置默认商品
	* @description:
	* @param {any} list
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-05-14 13:55
	*/
	const setDefaultService = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (list) {
			if (state.renderType !== "eventBooking") return;
			if (state.bookingId) return;
			if (list.length !== 1) return;
			let _list = [...state.service.value];
			if (_list.length !== 0) return;
			setLoading(true);
			let product = list[0];
			const isBundleOrCombination = require_utils$2.getIsBundleOrCombination((yield require_serve.getProductDetail(product.id, {
				schedule_date: state.date.value.format("YYYY-MM-DD"),
				customer_id: customerId,
				open_deposit: 1
			})).data);
			setLoading(false);
			if (isBundleOrCombination) return;
			let _item = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, product), {}, {
				product_id: product.id,
				_id: (0, _pisell_utils.getUniqueId)(),
				_extend: {
					start_date: state.date.value,
					startDate: state.date.value,
					endDate: isDay ? state.date.end_date : state.date.value,
					start_time: "00:00:00",
					duration: 10,
					quantity: 1,
					price: product.price,
					total: product.price,
					origin_total: product.price,
					product_name: product.title
				}
			});
			_list.push(_item);
			dispatch({
				type: "setService",
				payload: {
					value: _list,
					edit: true
				}
			});
		});
		return function setDefaultService(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	const getServices = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (params) {
			var _state$eventOtherPara, _state$eventOtherPara2;
			setLoading(true);
			if (state.renderType === "eventBooking" && !((_state$eventOtherPara = state.eventOtherParams) === null || _state$eventOtherPara === void 0 || (_state$eventOtherPara = _state$eventOtherPara.productIds) === null || _state$eventOtherPara === void 0 ? void 0 : _state$eventOtherPara.length)) {
				dispatch({
					type: "setService",
					payload: { services: [] }
				});
				setLoading(false);
				return;
			}
			let dayParams = {};
			if (isDay && !require_utils.isEventBooking(state)) dayParams.extension_type = ["normal", "product_appointment"];
			let data = yield state.apis.getServices(require_objectSpread2._objectSpread2({
				skip: 1,
				num: 500,
				extension_type: state.renderType === "eventBooking" ? void 0 : [
					"product_appointment",
					"appointment_ticket",
					"session_product",
					"session_ticket"
				],
				with: ["category", "collection"],
				ids: state.renderType === "eventBooking" ? (_state$eventOtherPara2 = state.eventOtherParams) === null || _state$eventOtherPara2 === void 0 ? void 0 : _state$eventOtherPara2.productIds : void 0,
				open_quotation: 1,
				with_schedule: 1,
				with_count: ["bundleGroup", "optionGroup"],
				order_by_string: "sort:desc",
				status: "published",
				schedule_date: params === null || params === void 0 ? void 0 : params.schedule_date,
				customer_id: customerId
			}, dayParams));
			setLoading(false);
			let lists = (data === null || data === void 0 ? void 0 : data.list) || [];
			if (require_utils.isDaysBooking(state)) lists = require_utils.getDayProducts(require_utils.holderId(state), lists, state === null || state === void 0 ? void 0 : state.isTerminalDayBookingPage);
			if (!state.service.addModal) setDefaultService(lists);
			dispatch({
				type: "setService",
				payload: { services: lists }
			});
		});
		return function getServices(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	const handleSelectService = () => {
		dispatch({
			type: "setService",
			payload: { addModal: true }
		});
		if (!state.isTerminal) setTimeout(() => {
			var _searchRef$current, _searchRef$current$fo;
			(_searchRef$current = searchRef.current) === null || _searchRef$current === void 0 || (_searchRef$current$fo = _searchRef$current.focus) === null || _searchRef$current$fo === void 0 || _searchRef$current$fo.call(_searchRef$current);
		}, 100);
	};
	const handleSelectProduct = (0, ahooks.useMemoizedFn)((item, type) => {
		const isSession = require_utils$1.isSessionProduct(item);
		let isEject = require_utils$1.getIsEject(item, type);
		const isOnlySession = require_utils$1.getIsOnlySession(item);
		if (isDay) isEject = type === "detail" || item.bundle_group_count > 0 || item.option_group_count > 0 || item.is_variant > 0;
		state.action({
			type: "pisell1.handleOpenProductModal",
			data: {
				id: item.id,
				date: state.date.value.format("YYYY-MM-DD"),
				showConfig: {
					option: isDay ? true : false,
					session: isSession,
					variant: isDay ? true : false,
					package: true
				},
				is_eject: isEject ? 1 : 0,
				isOnlySession,
				customer_id: customerId,
				isOS: isSession
			},
			callback: function() {
				var _ref3 = require_asyncToGenerator._asyncToGenerator(function* (e, extension_type, detail) {
					createBooking({ booking: yield createBookingData({ product: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, detail || item), {}, { _params: e }) }) });
				});
				return function callback(_x3, _x4, _x5) {
					return _ref3.apply(this, arguments);
				};
			}()
		});
	});
	const renderList = (0, react.useMemo)(() => {
		var _state$service;
		let _list = ((_state$service = state.service) === null || _state$service === void 0 ? void 0 : _state$service.services) || [];
		if (require_utils.isDaysBooking(state)) {
			if (!require_utils.holderId(state)) _list = _list.filter((item) => require_utils.isNormalProductByDurationSchedule(item));
		}
		const isIncludes = (str) => {
			return str.toLowerCase().indexOf(search.toLowerCase()) !== -1;
		};
		return require_utils$1.formatProductListByCategory(_list, search ? (item) => isIncludes(item.title) : void 0);
	}, [
		search,
		(_state$service2 = state.service) === null || _state$service2 === void 0 ? void 0 : _state$service2.services,
		state === null || state === void 0 ? void 0 : state.activeCartId
	]);
	const isService = (0, react.useMemo)(() => {
		var _state$service3;
		return ((_state$service3 = state.service) === null || _state$service3 === void 0 || (_state$service3 = _state$service3.value) === null || _state$service3 === void 0 ? void 0 : _state$service3.length) > 0;
	}, [state.service.value]);
	(0, react.useEffect)(() => {
		var _context$appHelper, _context$appHelper$ge, _context$appHelper$ut, _context$appHelper$ut2, _state$service4;
		if (shopList.includes(((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 || (_context$appHelper$ge = _context$appHelper.getData) === null || _context$appHelper$ge === void 0 ? void 0 : _context$appHelper$ge.call(_context$appHelper, "shop_id")) || ((_context$appHelper$ut = context.appHelper.utils.store) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut.getState) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.call(_context$appHelper$ut)) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.global) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.globalConfig) === null || _context$appHelper$ut2 === void 0 ? void 0 : _context$appHelper$ut2.id)) || state.closeAutoOpenService) return;
		if (state.modalOpen && !state.bookingId && state.renderType !== "eventBooking" && ((_state$service4 = state.service) === null || _state$service4 === void 0 || (_state$service4 = _state$service4.value) === null || _state$service4 === void 0 ? void 0 : _state$service4.length) === 0) setTimeout(() => {
			handleSelectService();
		}, 10);
	}, [
		state.modalOpen,
		state.bookingId,
		(_state$service5 = state.service) === null || _state$service5 === void 0 ? void 0 : _state$service5.value
	]);
	const showAddButton = (0, react.useMemo)(() => {
		return !state.disabledEdit && !state.channelDisabledEdit;
	}, [state.disabledEdit, state.channelDisabledEdit]);
	const currentDate = (0, react.useMemo)(() => {
		return state.date.value.format("YYYY-MM-DD");
	}, [state.date.value]);
	(0, ahooks.useUpdateEffect)(() => {
		if (state.service.addModal) {
			getServices({ schedule_date: currentDate });
			setSearch("");
		}
	}, [state.service.addModal, currentDate]);
	(0, react.useEffect)(() => {
		if (renderMode === "current") getServices({ schedule_date: currentDate });
	}, [renderMode, currentDate]);
	(0, react.useEffect)(() => {
		if (state.renderType === "eventBooking" && state.modalOpen) getServices({ schedule_date: currentDate });
	}, [
		state.renderType,
		state.modalOpen,
		currentDate
	]);
	const addButton = (0, react.useMemo)(() => {
		if (isDay) return;
		if (state.isBookingCreatePage) return /* @__PURE__ */ react.default.createElement("div", { style: { height: 20 } });
		if (state.renderType === "eventBooking") return isService ? /* @__PURE__ */ react.default.createElement("div", { style: { height: 20 } }) : /* @__PURE__ */ react.default.createElement(require_index$1.default, {
			loading,
			showSub: false,
			title: _pisell_utils.locales.getText("pisell2.text.add-a-service"),
			subTitle: _pisell_utils.locales.getText("pisell2.text.add-another-service"),
			onClick: handleSelectService
		});
		return showAddButton ? /* @__PURE__ */ react.default.createElement(require_index$1.default, {
			showSub: isService,
			title: _pisell_utils.locales.getText("pisell2.text.add-a-service"),
			subTitle: _pisell_utils.locales.getText("pisell2.text.add-another-service"),
			onClick: handleSelectService
		}) : /* @__PURE__ */ react.default.createElement("div", { style: { height: 20 } });
	}, [
		state.renderType,
		showAddButton,
		isService,
		state.service.value,
		loading
	]);
	const isRenderCurrent = (0, react.useMemo)(() => {
		return renderMode === "current";
	}, [renderMode]);
	/**
	* @title: 日期宽度
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2024-12-09 15:46
	*/
	const colSpan = (0, react.useMemo)(() => {
		const isTerminal = state.isTerminalDayBookingPage;
		return require_status.inputDateCol(!!(isDay && require_utils.holderId(state) && !require_utils.isEventBooking(state)), isTerminal);
	}, [
		state.isTerminalDayBookingPage,
		isDay,
		require_utils.holderId(state)
	]);
	return (0, react.useMemo)(() => {
		var _state$service6;
		const header = /* @__PURE__ */ react.default.createElement("div", {
			className: "pisell-lowcode__booking-service-header",
			ref: headerRef
		}, /* @__PURE__ */ react.default.createElement(antd.Row, { gutter: [16, 16] }, isDay && require_utils.holderId(state) && !require_utils.isEventBooking(state) ? /* @__PURE__ */ react.default.createElement(antd.Col, colSpan.date, /* @__PURE__ */ react.default.createElement(require_index$3.default, {
			state,
			dispatch
		})) : null, /* @__PURE__ */ react.default.createElement(antd.Col, colSpan.input, /* @__PURE__ */ react.default.createElement(antd.Input, {
			ref: searchRef,
			prefix: /* @__PURE__ */ react.default.createElement(_pisell_icon_es_SearchLg.default, null),
			style: {
				marginTop: 16,
				height: 44
			},
			placeholder: _pisell_utils.locales.getText("pisell2.text.search"),
			allowClear: true,
			value: search,
			onChange: (e) => {
				setSearch(e.target.value);
			}
		}))));
		const content = /* @__PURE__ */ react.default.createElement("div", { className: isDay && headerHeight > 76 ? "pisell-lowcode__booking-service-date-body" : "pisell-lowcode__booking-service-body" }, /* @__PURE__ */ react.default.createElement(require_index$2.default, {
			list: renderList,
			amountSymbol: state.amountSymbol,
			onChange: handleSelectProduct,
			scrollDom: isRenderCurrent ? ".pisell-lowcode__booking-vertical-add-service-wrap" : ".booking-server-wrap .pisell-lowcode-drawer-body "
		}));
		if (renderMode === "current") return /* @__PURE__ */ react.default.createElement("div", {
			className: "pisell-lowcode__booking-vertical-add-service-wrap",
			style: {
				width: "100%",
				height: "100%",
				overflow: "auto"
			}
		}, header, content);
		return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, ((_state$service6 = state.service) === null || _state$service6 === void 0 || (_state$service6 = _state$service6.cacheItem) === null || _state$service6 === void 0 ? void 0 : _state$service6.autoClose) && /* @__PURE__ */ react.default.createElement(antd.Skeleton, {
			loading: true,
			active: true,
			round: true
		}), addButton, /* @__PURE__ */ react.default.createElement(require_index.default, require_objectSpread2._objectSpread2({
			titleNoWrap: true,
			open: state.service.addModal && !state.isBookingCreatePage && !state.isTerminalDayBookingPage,
			title: isDay ? _pisell_utils.locales.getText("pisell2.text.select.items") : _pisell_utils.locales.getText("pisell2.text.select-service"),
			rootClassName: "booking-server-wrap",
			onClose: () => {
				dispatch({
					type: "setService",
					payload: {
						addModal: false,
						changeService: false
					}
				});
			},
			header
		}, drawerProps || {}), content));
	}, [
		(_state$service7 = state.service) === null || _state$service7 === void 0 || (_state$service7 = _state$service7.cacheItem) === null || _state$service7 === void 0 ? void 0 : _state$service7.autoClose,
		addButton,
		state.service.addModal,
		_pisell_utils.locales,
		search,
		renderList,
		state.amountSymbol,
		state.renderType,
		state === null || state === void 0 ? void 0 : state.activeCartId,
		headerHeight,
		require_utils.holderId(state)
	]);
};
//#endregion
exports.default = Info;
