import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { Toast } from "../../utils.js";
import { useAppointmentBookingContext } from "../../context.js";
import { scrollToFirstErrorCard } from "../Resource/utils.js";
import { useAppointmentBookingResource, useAppointmentBookingStep } from "../../hooks.js";
import SelectDate from "./components/SelectDateOS.js";
import React, { forwardRef, useEffect, useImperativeHandle, useState } from "react";
import { locales } from "@pisell/utils";
import { Iconfont } from "@pisell/materials";
import "./index.less";
//#region src/components/appointmentBooking/components/TimeSlicing/index.tsx
const TimeSlicing = forwardRef((props, ref) => {
	const [selectedResource, setSelectedResource] = useState("");
	const { times, appointmentBooking, setSelectedTime, resourceOtherParams, carts } = useAppointmentBookingContext();
	const { getResourceTimeSlot, getResourceList } = useAppointmentBookingResource();
	const { nextStep } = useAppointmentBookingStep();
	const [currentDate, setCurrentDate] = useState(void 0);
	useImperativeHandle(ref, () => ({ next: () => {
		const isUnSelectedTimes = appointmentBooking.checkCartItems("date");
		if ((isUnSelectedTimes === null || isUnSelectedTimes === void 0 ? void 0 : isUnSelectedTimes.length) > 0) {
			Toast(locales.getText("pisell2.appointment.unselect.time"));
			return;
		}
		nextStep();
	} }));
	useEffect(() => {
		getResourceTimeSlot();
	}, []);
	const getAutoSelect = function() {
		var _ref = _asyncToGenerator(function* (val) {
			setSelectedTime(val);
			scrollToFirstErrorCard(".pisell-lowcode-product-card-error");
			yield appointmentBooking.submitTimeSlot(val);
		});
		return function getAutoSelect(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	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 = _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);
		};
	}();
	useEffect(() => {
		(function() {
			var _ref3 = _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.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(SelectDate, {
		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.createElement("div", { className: "appointment-time-slicing-wrap" }, times.map((item, index) => {
		return /* @__PURE__ */ React.createElement("div", {
			key: index,
			onClick: () => onClick(item),
			className: `item ${selectedResource === (item === null || item === void 0 ? void 0 : item.start_time) ? "active" : ""}`
		}, /* @__PURE__ */ React.createElement("span", null, item === null || item === void 0 ? void 0 : item.start_time), /* @__PURE__ */ React.createElement(Iconfont, {
			type: "pisell2-chevron-right",
			className: `icon`
		}));
	})) : null);
});
//#endregion
export { TimeSlicing as default };
