const require_runtime = require("../../../../_virtual/_rolldown/runtime.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_utils$1 = require("../Resource/utils.js");
const require_hooks = require("../../hooks.js");
const require_SelectDateOS = require("./components/SelectDateOS.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
require("./index.less");
//#region src/components/appointmentBooking/components/TimeSlicing/index.tsx
const TimeSlicing = (0, react.forwardRef)((props, ref) => {
	const [selectedResource, setSelectedResource] = (0, react.useState)("");
	const { times, appointmentBooking, setSelectedTime, resourceOtherParams, carts } = require_context.useAppointmentBookingContext();
	const { getResourceTimeSlot, getResourceList } = require_hooks.useAppointmentBookingResource();
	const { nextStep } = require_hooks.useAppointmentBookingStep();
	const [currentDate, setCurrentDate] = (0, react.useState)(void 0);
	(0, react.useImperativeHandle)(ref, () => ({ next: () => {
		const isUnSelectedTimes = appointmentBooking.checkCartItems("date");
		if ((isUnSelectedTimes === null || isUnSelectedTimes === void 0 ? void 0 : isUnSelectedTimes.length) > 0) {
			require_utils.Toast(_pisell_utils.locales.getText("pisell2.appointment.unselect.time"));
			return;
		}
		nextStep();
	} }));
	(0, react.useEffect)(() => {
		getResourceTimeSlot();
	}, []);
	const getAutoSelect = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (val) {
			setSelectedTime(val);
			require_utils$1.scrollToFirstErrorCard(".pisell-lowcode-product-card-error");
			yield appointmentBooking.submitTimeSlot(val);
		});
		return function getAutoSelect(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	(0, react.useEffect)(() => {
		let appoimentBookingProductCart = carts.filter((item) => !appointmentBooking.isTargetCartIdNormalProduct(item === null || item === void 0 ? void 0 : item._id));
		if ((times === null || times === void 0 ? void 0 : times.length) > 0 && (appoimentBookingProductCart === null || appoimentBookingProductCart === void 0 ? void 0 : appoimentBookingProductCart.length) > 0) {
			var _appoimentBookingProd, _times$, _appoimentBookingProd2;
			setSelectedResource((appoimentBookingProductCart === null || appoimentBookingProductCart === void 0 || (_appoimentBookingProd = appoimentBookingProductCart[0]) === null || _appoimentBookingProd === void 0 ? void 0 : _appoimentBookingProd.start_time) || (times === null || times === void 0 || (_times$ = times[0]) === null || _times$ === void 0 ? void 0 : _times$.start_time));
			if (appoimentBookingProductCart === null || appoimentBookingProductCart === void 0 || (_appoimentBookingProd2 = appoimentBookingProductCart[0]) === null || _appoimentBookingProd2 === void 0 ? void 0 : _appoimentBookingProd2.start_time) return;
			getAutoSelect(times === null || times === void 0 ? void 0 : times[0]);
		}
	}, [times, carts]);
	const onClick = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (e) {
			setSelectedResource(e === null || e === void 0 ? void 0 : e.start_time);
			getAutoSelect(e);
		});
		return function onClick(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	(0, react.useEffect)(() => {
		(function() {
			var _ref3 = require_asyncToGenerator._asyncToGenerator(function* () {
				var _res$;
				const res = yield appointmentBooking.getDateRange();
				setCurrentDate(res === null || res === void 0 || (_res$ = res[0]) === null || _res$ === void 0 ? void 0 : _res$.date);
			});
			return function getCurrentDate() {
				return _ref3.apply(this, arguments);
			};
		})()();
	}, []);
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(require_SelectDateOS.default, {
		onDateChange: (currentData) => {
			if ((times === null || times === void 0 ? void 0 : times.length) > 0) appointmentBooking.deleteCartItemInfo("time");
			setCurrentDate(currentData === null || currentData === void 0 ? void 0 : currentData.date);
			appointmentBooking.setDateRange([currentData]);
			getResourceTimeSlot();
		},
		times,
		currentDate
	}), (times === null || times === void 0 ? void 0 : times.length) > 0 ? /* @__PURE__ */ react.default.createElement("div", { className: "appointment-time-slicing-wrap" }, times.map((item, index) => {
		return /* @__PURE__ */ react.default.createElement("div", {
			key: index,
			onClick: () => onClick(item),
			className: `item ${selectedResource === (item === null || item === void 0 ? void 0 : item.start_time) ? "active" : ""}`
		}, /* @__PURE__ */ react.default.createElement("span", null, item === null || item === void 0 ? void 0 : item.start_time), /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
			type: "pisell2-chevron-right",
			className: `icon`
		}));
	})) : null);
});
//#endregion
exports.default = TimeSlicing;
