const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_utils = require("../../utils.js");
const require_context = require("../../context.js");
const require_hooks = require("../../hooks.js");
const require_index = require("../../../eventBooking/components/resourceDisplay/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let src_components_appointmentBooking_assets_guests_svg = require("../../assets/guests.svg");
src_components_appointmentBooking_assets_guests_svg = require_runtime.__toESM(src_components_appointmentBooking_assets_guests_svg);
let src_components_appointmentBooking_assets_resource_svg = require("../../assets/resource.svg");
src_components_appointmentBooking_assets_resource_svg = require_runtime.__toESM(src_components_appointmentBooking_assets_resource_svg);
require("./index.less");
//#region src/components/appointmentBooking/components/Resource/index.tsx
const Resource = (0, react.forwardRef)((props, ref) => {
	const [selectedResource, setSelectedResource] = (0, react.useState)(0);
	const { appointmentBooking, carts, resourceOtherParams, currentStep, setResourceOtherParams, currentStepParams, currentSelectedResources } = require_context.useAppointmentBookingContext();
	const { getResourceList } = require_hooks.useAppointmentBookingResource();
	const { checkSkipResourceStep } = require_hooks.useIsSkipResourceStep();
	const { updateStep, nextStep } = require_hooks.useAppointmentBookingStep();
	const { isMobile } = require_hooks.useConfig();
	const [loading, setLoading] = (0, react.useState)(false);
	const utils = require_hooks.contextUtils();
	(0, react.useImperativeHandle)(ref, () => ({ next: function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			setResourceOtherParams(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, resourceOtherParams), {}, { errorList: null }));
			nextStep();
		});
		return function next() {
			return _ref.apply(this, arguments);
		};
	}() }));
	const resources = (0, react.useMemo)(() => {
		const { resources } = getResourceList();
		return resources || [];
	}, []);
	(0, react.useEffect)(() => {
		setLoading(true);
	}, []);
	(0, react.useEffect)(() => {
		(function() {
			var _ref2 = require_asyncToGenerator._asyncToGenerator(function* () {
				try {
					var _currentStep$step;
					getResourceList();
					const skipResourceStep = yield checkSkipResourceStep();
					updateStep(currentStep === null || currentStep === void 0 || (_currentStep$step = currentStep.step) === null || _currentStep$step === void 0 ? void 0 : _currentStep$step.key, {
						index: currentStep === null || currentStep === void 0 ? void 0 : currentStep.index,
						isSkip: skipResourceStep
					});
					if (skipResourceStep) return true;
					return false;
				} catch (error) {}
			});
			return function checkAndSkipResourceStep() {
				return _ref2.apply(this, arguments);
			};
		})()();
	}, [currentStepParams.key]);
	const anyResource = [{
		id: 0,
		image: src_components_appointmentBooking_assets_guests_svg.default,
		title: _pisell_utils.locales.getText("pisell2.appointment.any.professional"),
		subtitle: _pisell_utils.locales.getText("pisell2.appointment.for.maximum.availability")
	}];
	const appointmentBoard = (0, react.useMemo)(() => {
		return require_utils.currentAppointmentBoard(currentStepParams.key, resources, appointmentBooking);
	}, [currentStepParams.key, resources]);
	const renderLists = (0, react.useMemo)(() => {
		const appointmentCarts = carts.filter((n) => !(appointmentBooking === null || appointmentBooking === void 0 ? void 0 : appointmentBooking.isTargetCartIdNormalProduct(n._id)));
		if (appointmentCarts.length > 1 || !(appointmentCarts === null || appointmentCarts === void 0 ? void 0 : appointmentCarts.length)) return [];
		return require_utils.currentRenderLists(resources, appointmentBoard === null || appointmentBoard === void 0 ? void 0 : appointmentBoard.form_code);
	}, [
		resources,
		appointmentBoard === null || appointmentBoard === void 0 ? void 0 : appointmentBoard.form_code,
		carts
	]);
	/**
	* @title: 获取自动分派
	* @description:
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-05-26 15:10
	*/
	(0, react.useEffect)(() => {
		(function() {
			var _ref3 = require_asyncToGenerator._asyncToGenerator(function* () {
				if (!(appointmentBoard === null || appointmentBoard === void 0 ? void 0 : appointmentBoard.form_code)) return;
				const res = yield appointmentBooking.autoSelectAllProductResources(appointmentBoard === null || appointmentBoard === void 0 ? void 0 : appointmentBoard.form_code);
				setResourceOtherParams(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, resourceOtherParams), res));
				getResourceList();
				const carts = yield appointmentBooking.getCart();
				const cartItem = carts === null || carts === void 0 ? void 0 : carts[0];
				if (cartItem === null || cartItem === void 0 ? void 0 : cartItem._id) appointmentBooking.updateCart(require_objectSpread2._objectSpread2({ _id: cartItem === null || cartItem === void 0 ? void 0 : cartItem._id }, cartItem));
			});
			return function autoSelectResources() {
				return _ref3.apply(this, arguments);
			};
		})()();
	}, [appointmentBoard]);
	const resourcesResult = (0, react.useMemo)(() => {
		return ((appointmentBoard === null || appointmentBoard === void 0 ? void 0 : appointmentBoard.isRequired) ? [] : [...anyResource]).concat(renderLists || []);
	}, [renderLists, appointmentBoard]);
	const onClick = (e) => {
		setSelectedResource(e.id);
		if (e.id) {
			var _carts$index;
			const index = carts.findIndex((n) => !(appointmentBooking === null || appointmentBooking === void 0 ? void 0 : appointmentBooking.isTargetCartIdNormalProduct(n._id)));
			e.like_status = "like";
			return appointmentBooking.updateCart({
				_id: carts === null || carts === void 0 || (_carts$index = carts[index]) === null || _carts$index === void 0 ? void 0 : _carts$index._id,
				resources: require_utils.updateCartResources(carts === null || carts === void 0 ? void 0 : carts[index], e)
			});
		}
	};
	(0, react.useEffect)(() => {
		var _carts$index2;
		const index = carts.findIndex((n) => !(appointmentBooking === null || appointmentBooking === void 0 ? void 0 : appointmentBooking.isTargetCartIdNormalProduct(n._id)));
		const selectedResource = carts === null || carts === void 0 || (_carts$index2 = carts[index]) === null || _carts$index2 === void 0 || (_carts$index2 = _carts$index2._origin) === null || _carts$index2 === void 0 || (_carts$index2 = _carts$index2.resources) === null || _carts$index2 === void 0 ? void 0 : _carts$index2.find((item) => (item === null || item === void 0 ? void 0 : item.form_id) === (appointmentBoard === null || appointmentBoard === void 0 ? void 0 : appointmentBoard.form_id));
		setSelectedResource((selectedResource === null || selectedResource === void 0 ? void 0 : selectedResource.id) || 0);
	}, [carts, appointmentBoard === null || appointmentBoard === void 0 ? void 0 : appointmentBoard.form_id]);
	(0, react.useEffect)(() => {
		if (resourcesResult) setLoading(false);
	}, [resourcesResult]);
	/**
	* @title: 资源名称
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-06-09 16:25
	*/
	const currentResourceName = (0, react.useMemo)(() => {
		var _currentSelectedResou;
		const currentResource = currentSelectedResources === null || currentSelectedResources === void 0 || (_currentSelectedResou = currentSelectedResources[0]) === null || _currentSelectedResou === void 0 ? void 0 : _currentSelectedResou.currentSelectedResource;
		const resourceItem = (currentResource === null || currentResource === void 0 ? void 0 : currentResource.resourceItem) || {};
		return require_utils.getResourceTitle(appointmentBooking, resourceItem === null || resourceItem === void 0 ? void 0 : resourceItem.code, utils, resourceItem);
	}, [currentSelectedResources]);
	return /* @__PURE__ */ react.default.createElement("div", { className: "appointment-resource-wrap" }, loading ? /* @__PURE__ */ react.default.createElement(antd.Skeleton, {
		loading: true,
		active: true
	}) : !(resourcesResult === null || resourcesResult === void 0 ? void 0 : resourcesResult.length) ? /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellEmpty, {
		description: _pisell_utils.locales.getText("pisell2.appointment.resource.tip")(currentResourceName),
		maxWidth: 400,
		icon: /* @__PURE__ */ react.default.createElement(_pisell_materials.Image, {
			src: src_components_appointmentBooking_assets_resource_svg.default,
			preview: false,
			width: 44
		})
	}) : /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellCustomCheckboxGroup, {
		dataSource: resourcesResult,
		renderItem: (item) => {
			var _item$title;
			return /* @__PURE__ */ react.default.createElement(require_index.default, {
				key: item.id,
				mode: "rule",
				dataSource: item,
				direction: "vertical",
				children: !(item === null || item === void 0 ? void 0 : item.image) ? item === null || item === void 0 || (_item$title = item.title) === null || _item$title === void 0 ? void 0 : _item$title.charAt(0).toUpperCase() : null,
				selected: selectedResource === (item === null || item === void 0 ? void 0 : item.id),
				className: !item.id ? "appointment-resource-guest-card" : ""
			});
		},
		columns: isMobile ? 2 : 3,
		horizontalGap: 16,
		verticalGap: 16,
		onClick: (e) => onClick && onClick(e)
	}));
});
//#endregion
exports.default = Resource;
