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 { currentAppointmentBoard, currentRenderLists, getResourceTitle, updateCartResources } from "../../utils.js";
import { useAppointmentBookingContext } from "../../context.js";
import { contextUtils, useAppointmentBookingResource, useAppointmentBookingStep, useConfig, useIsSkipResourceStep } from "../../hooks.js";
import PisellResourceCard from "../../../eventBooking/components/resourceDisplay/index.js";
import React, { forwardRef, useEffect, useImperativeHandle, useMemo, useState } from "react";
import { Skeleton } from "antd";
import { locales } from "@pisell/utils";
import { Image as Image$1, PisellCustomCheckboxGroup, PisellEmpty } from "@pisell/materials";
import guestsImg from "../../assets/guests.svg";
import resourceImage from "../../assets/resource.svg";
import "./index.less";
//#region src/components/appointmentBooking/components/Resource/index.tsx
const Resource = forwardRef((props, ref) => {
	const [selectedResource, setSelectedResource] = useState(0);
	const { appointmentBooking, carts, resourceOtherParams, currentStep, setResourceOtherParams, currentStepParams, currentSelectedResources } = useAppointmentBookingContext();
	const { getResourceList } = useAppointmentBookingResource();
	const { checkSkipResourceStep } = useIsSkipResourceStep();
	const { updateStep, nextStep } = useAppointmentBookingStep();
	const { isMobile } = useConfig();
	const [loading, setLoading] = useState(false);
	const utils = contextUtils();
	useImperativeHandle(ref, () => ({ next: function() {
		var _ref = _asyncToGenerator(function* () {
			setResourceOtherParams(_objectSpread2(_objectSpread2({}, resourceOtherParams), {}, { errorList: null }));
			nextStep();
		});
		return function next() {
			return _ref.apply(this, arguments);
		};
	}() }));
	const resources = useMemo(() => {
		const { resources } = getResourceList();
		return resources || [];
	}, []);
	useEffect(() => {
		setLoading(true);
	}, []);
	useEffect(() => {
		(function() {
			var _ref2 = _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: guestsImg,
		title: locales.getText("pisell2.appointment.any.professional"),
		subtitle: locales.getText("pisell2.appointment.for.maximum.availability")
	}];
	const appointmentBoard = useMemo(() => {
		return currentAppointmentBoard(currentStepParams.key, resources, appointmentBooking);
	}, [currentStepParams.key, resources]);
	const renderLists = 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 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
	*/
	useEffect(() => {
		(function() {
			var _ref3 = _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(_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(_objectSpread2({ _id: cartItem === null || cartItem === void 0 ? void 0 : cartItem._id }, cartItem));
			});
			return function autoSelectResources() {
				return _ref3.apply(this, arguments);
			};
		})()();
	}, [appointmentBoard]);
	const resourcesResult = 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: updateCartResources(carts === null || carts === void 0 ? void 0 : carts[index], e)
			});
		}
	};
	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]);
	useEffect(() => {
		if (resourcesResult) setLoading(false);
	}, [resourcesResult]);
	/**
	* @title: 资源名称
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-06-09 16:25
	*/
	const currentResourceName = 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 getResourceTitle(appointmentBooking, resourceItem === null || resourceItem === void 0 ? void 0 : resourceItem.code, utils, resourceItem);
	}, [currentSelectedResources]);
	return /* @__PURE__ */ React.createElement("div", { className: "appointment-resource-wrap" }, loading ? /* @__PURE__ */ React.createElement(Skeleton, {
		loading: true,
		active: true
	}) : !(resourcesResult === null || resourcesResult === void 0 ? void 0 : resourcesResult.length) ? /* @__PURE__ */ React.createElement(PisellEmpty, {
		description: locales.getText("pisell2.appointment.resource.tip")(currentResourceName),
		maxWidth: 400,
		icon: /* @__PURE__ */ React.createElement(Image$1, {
			src: resourceImage,
			preview: false,
			width: 44
		})
	}) : /* @__PURE__ */ React.createElement(PisellCustomCheckboxGroup, {
		dataSource: resourcesResult,
		renderItem: (item) => {
			var _item$title;
			return /* @__PURE__ */ React.createElement(PisellResourceCard, {
				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
export { Resource as default };
