import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { PisellTimeNavigator } from "../pisellTimeNavigator/PisellTimeNavigator.js";
import { PisellReservationSchedule } from "../pisellReservationSchedule/PisellReservationSchedule.js";
import { formatScheduleAtLabel } from "./utils/formatScheduleAtLabel.js";
import React, { useCallback, useState } from "react";
import { Spin } from "antd";
import classNames from "classnames";
import dayjs from "dayjs";
import "./PisellReservationScheduleBand.less";
//#region src/components/pisellReservationScheduleBand/PisellReservationScheduleBand.tsx
function startOfDay(d) {
	return d.startOf("day");
}
function withTimeOnDate(dateDay, timeSource) {
	return startOfDay(dateDay).hour(timeSource.hour()).minute(timeSource.minute()).second(0).millisecond(0);
}
/**
* 预约顶栏组合：营业日切换 + 锚定到选中日的时间导航轴
*/
function PisellReservationScheduleBand(props) {
	const { value: valueProp, onChange, defaultValue, scheduleProps, timeNavigatorProps, scheduleStartSlot, scheduleEndSlot, scheduleAfterDateSlot, dataLoading = false, dataLoadingTip, hideTimeNavigator = false, className, style, scheduleRowClassName } = props;
	const isControlled = valueProp !== void 0;
	const [internal, setInternal] = useState(() => {
		var _defaultValue$date, _defaultValue$at;
		const d = (_defaultValue$date = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.date) !== null && _defaultValue$date !== void 0 ? _defaultValue$date : dayjs().startOf("day");
		const atSrc = (_defaultValue$at = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.at) !== null && _defaultValue$at !== void 0 ? _defaultValue$at : dayjs();
		return {
			date: startOfDay(dayjs(d)),
			at: withTimeOnDate(dayjs(d), dayjs(atSrc))
		};
	});
	const effective = isControlled ? valueProp : internal;
	const emit = useCallback((next) => {
		/** at 保留时间轴返回的绝对时刻（跨日营业时可能为锚定日的次日） */
		const normalized = {
			date: startOfDay(next.date),
			at: next.at
		};
		if (!isControlled) setInternal(normalized);
		onChange === null || onChange === void 0 || onChange(normalized);
	}, [isControlled, onChange]);
	const handleDateChange = useCallback((d) => {
		emit({
			date: startOfDay(d),
			at: withTimeOnDate(d, effective.at)
		});
	}, [emit, effective.at]);
	const handleTimeChange = useCallback((t) => {
		/** 顶栏 date 保持营业日锚定日；跨自然日时刻由 formatScheduleAtLabel 展示为「MM-DD HH:mm」 */
		emit({
			date: startOfDay(effective.date),
			at: t
		});
	}, [emit, effective.date]);
	const formatTimeLabel = useCallback((t) => formatScheduleAtLabel(t, effective.date), [effective.date]);
	return /* @__PURE__ */ React.createElement("div", {
		className: classNames("pisell-reservation-schedule-band", dataLoading && "pisell-reservation-schedule-band--data-loading", className),
		style
	}, dataLoading ? /* @__PURE__ */ React.createElement("div", {
		className: "pisell-reservation-schedule-band-data-loading-overlay",
		"aria-busy": "true",
		"aria-live": "polite"
	}, /* @__PURE__ */ React.createElement(Spin, {
		size: "small",
		tip: dataLoadingTip != null ? dataLoadingTip : void 0
	})) : null, /* @__PURE__ */ React.createElement("div", { className: classNames("pisell-reservation-schedule-band-schedule-row", scheduleRowClassName) }, /* @__PURE__ */ React.createElement("div", { className: "pisell-reservation-schedule-band-slot-start" }, scheduleStartSlot), /* @__PURE__ */ React.createElement("div", { className: "pisell-reservation-schedule-band-schedule" }, /* @__PURE__ */ React.createElement(PisellReservationSchedule, _objectSpread2({
		value: effective.date,
		onChange: handleDateChange
	}, scheduleProps)), scheduleAfterDateSlot ? /* @__PURE__ */ React.createElement("div", { className: "pisell-reservation-schedule-band-after-date" }, scheduleAfterDateSlot) : null), /* @__PURE__ */ React.createElement("div", { className: "pisell-reservation-schedule-band-slot-end" }, scheduleEndSlot)), hideTimeNavigator ? null : /* @__PURE__ */ React.createElement("div", { className: "pisell-reservation-schedule-band-navigator" }, /* @__PURE__ */ React.createElement(PisellTimeNavigator, _objectSpread2(_objectSpread2({
		referenceDate: effective.date,
		value: effective.at
	}, timeNavigatorProps), {}, {
		onChange: handleTimeChange,
		formatTimeLabel
	}))));
}
PisellReservationScheduleBand.displayName = "PisellReservationScheduleBand";
//#endregion
export { PisellReservationScheduleBand };
