import { getCallToBookStatus, getStatus } from "../state.js";
import WeekItem from "./WeekItem/index.js";
import CallToBook from "./CallToBook/index.js";
import dayjs from "dayjs";
import { useEffect, useRef, useState } from "react";
import { Iconfont } from "@pisell/materials";
import classNames from "classnames";
import "./index.less";
//#region src/components/eventBooking/components/bookingAvailabilityCalendar/components/CalendarDate/CalenDateWeek/index.tsx
const CalenDateWeek = (props) => {
	const { currentDate, dateChange, productId, timeList, dateLimitData, getLimitData, cutTime } = props;
	const [weekList, setWeekList] = useState([]);
	const [showCallToBook, setShowCallToBook] = useState(false);
	const weekListRef = useRef(null);
	const [selectStatus, setSelectStatus] = useState("");
	const [selectDisabled, setSelectDisabled] = useState(false);
	/**
	* @title: 根据时间生成本周数据
	* @description:
	* @return {*}
	* @Author: xiangxiang.shao
	* @Date: 2024-02-07 15:13
	*/
	const initCurrentWeekData = (dateText) => {
		let startOfWeek = dayjs(dateText).startOf("week");
		if (startOfWeek.day() !== 0) startOfWeek.subtract(startOfWeek.day(), "days");
		let weekDates = [];
		for (let i = 0; i < 7; i++) {
			let date = startOfWeek.clone().add(i, "days");
			weekDates.push(date.format("YYYY-MM-DD"));
		}
		return weekDates;
	};
	/**
	* @title: 构造初始化周数据
	* @description:
	* @return {*}
	* @Author: xiangxiang.shao
	* @Date: 2024-02-07 15:13
	*/
	const initWeekData = () => {
		setWeekList(initCurrentWeekData(currentDate));
	};
	/**
	* @title: 判断是否重新生成还是滚动到指定位置
	* @description:
	* @return {*}
	* @Author: xiangxiang.shao
	* @Date: 2024-02-07 15:13
	*/
	const init = () => {
		initWeekData();
	};
	useEffect(() => {
		init();
	}, [currentDate]);
	useEffect(() => {
		const { status, disabled } = getStatus(dateLimitData, currentDate, cutTime);
		if (getCallToBookStatus(currentDate, dateLimitData, cutTime) && (status === "lots_of_space" || status === "filling_up_fast")) setShowCallToBook(true);
		else setShowCallToBook(false);
		setSelectStatus(status);
		setSelectDisabled(disabled);
	}, [
		currentDate,
		timeList,
		cutTime
	]);
	useEffect(() => {
		const index = weekList.findIndex((date) => date === currentDate);
		if ((weekListRef === null || weekListRef === void 0 ? void 0 : weekListRef.current) && index !== -1) weekListRef.current.scrollLeft = (index - 1) * 50;
	}, [currentDate, weekList]);
	return /* @__PURE__ */ React.createElement("div", { className: "calendar-week-wrap" }, /* @__PURE__ */ React.createElement("div", {
		ref: weekListRef,
		className: "calendar-week-list"
	}, /* @__PURE__ */ React.createElement(WeekItem, {
		weekList,
		currentDate,
		dateChange,
		isActive: true,
		productId,
		setCurrentDate: dateChange,
		timeList,
		dateLimitData,
		cutTime
	})), selectStatus === "sold_out" || selectDisabled ? /* @__PURE__ */ React.createElement("div", { className: "disabled-status" }, /* @__PURE__ */ React.createElement(Iconfont, { className: classNames("disabled-icon", "iconhourglass_disabled_wght300_24px") }), /* @__PURE__ */ React.createElement("div", { className: "disabled-status-text" }, selectDisabled ? 2 : 5)) : null, /* @__PURE__ */ React.createElement(CallToBook, { show: showCallToBook }));
};
//#endregion
export { CalenDateWeek as default };
