const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_mock = require("../../mock.js");
const require_index = require("../resourceUsability/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./index.less");
//#region src/components/eventBooking/components/timeResourceSelector/index.tsx
const TimeResourceSelector = ({ timeSlots = require_mock.mock.time_resource || [], showUnavailable = true, defaultExpanded = true, defaultSelectFirst = true, onChange, className, style }) => {
	const [selectedValues, setSelectedValues] = (0, react.useState)([]);
	const [error, setError] = (0, react.useState)("");
	const [expanded, setExpanded] = (0, react.useState)(defaultExpanded);
	(0, react.useEffect)(() => {
		setExpanded(defaultExpanded);
	}, [defaultExpanded]);
	const filteredTimeSlots = timeSlots === null || timeSlots === void 0 ? void 0 : timeSlots.filter((slot) => showUnavailable ? true : !slot.disabled);
	(0, react.useEffect)(() => {
		let id = [];
		if (defaultSelectFirst) {
			const firstAvailable = filteredTimeSlots.find((slot) => !slot.disabled);
			if (firstAvailable) id = [firstAvailable.id];
		} else id = [];
		setSelectedValues(id);
		onChange === null || onChange === void 0 || onChange(id);
	}, [defaultSelectFirst]);
	/**
	* @title: 点击卡片
	* @description:
	* @param {string} value
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-01-11 16:27
	*/
	const handleChange = (value) => {
		const newValue = Array.isArray(value) ? value : [value];
		setSelectedValues(newValue);
		onChange === null || onChange === void 0 || onChange(newValue);
	};
	/**
	* @title: 渲染时间卡片
	* @description:
	* @param {TimeSlot} item
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-01-10 18:08
	*/
	const renderTimeCard = (item) => {
		const obj = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { selected: selectedValues.includes(item === null || item === void 0 ? void 0 : item.id) });
		return /* @__PURE__ */ react.default.createElement(require_index.default, {
			showRemaining: true,
			showTime: true,
			item: obj,
			onTimeSlotClick: (e) => handleChange(e),
			style: { width: "100%" }
		});
	};
	(0, react.useEffect)(() => {
		setError((selectedValues === null || selectedValues === void 0 ? void 0 : selectedValues.length) === 0 ? "Please select a session time" : "");
	}, [selectedValues]);
	/**
	* @title: 展开/收起
	* @description:
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-01-10 19:00
	*/
	const toggleExpand = () => {
		setExpanded(!expanded);
	};
	/**
	* @title: 渲染内容
	* @description:
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-01-10 18:17
	*/
	const renderContent = () => {
		if ((filteredTimeSlots === null || filteredTimeSlots === void 0 ? void 0 : filteredTimeSlots.length) === 0) return /* @__PURE__ */ react.default.createElement("div", { className: "time-resource-no-data" }, "No dates are available");
		return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, error && /* @__PURE__ */ react.default.createElement("div", { className: "time-resource-error" }, error), /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellCustomCheckboxGroup, {
			dataSource: filteredTimeSlots,
			renderItem: renderTimeCard,
			columns: (0, _pisell_utils.isMobile)() ? 2 : 4,
			horizontalGap: 8,
			verticalGap: 8,
			allowAdd: false
		}));
	};
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellCard, {
		className: (0, classnames.default)("time-resource-selector", className),
		style
	}, /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("time-resource-header", { "time-resource-header-expanded": expanded }),
		onClick: toggleExpand
	}, /* @__PURE__ */ react.default.createElement("div", { className: "time-resource-title" }, "选择时间段"), /* @__PURE__ */ react.default.createElement("div", { className: "time-resource-icon" }, expanded ? /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, { type: "pisell2-chevron-up" }) : /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, { type: "pisell2-chevron-down" }))), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("time-resource-content", { "time-resource-content-expanded": expanded }) }, renderContent()));
};
//#endregion
exports.default = TimeResourceSelector;
