const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_locales = require("./locales.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let _pisell_date_picker = require("@pisell/date-picker");
let _pisell_icon_es_ChevronLeft = require("@pisell/icon/es/ChevronLeft");
_pisell_icon_es_ChevronLeft = require_runtime.__toESM(_pisell_icon_es_ChevronLeft);
let _pisell_icon_es_ChevronRight = require("@pisell/icon/es/ChevronRight");
_pisell_icon_es_ChevronRight = require_runtime.__toESM(_pisell_icon_es_ChevronRight);
require("./PisellReservationSchedule.less");
//#region src/components/pisellReservationSchedule/PisellReservationSchedule.tsx
const I18N_TODAY = "pisell-reservation-schedule.today";
const I18N_PREV = "pisell-reservation-schedule.prev";
const I18N_NEXT = "pisell-reservation-schedule.next";
const I18N_PREV_WEEK = "pisell-reservation-schedule.prev-week";
const I18N_NEXT_WEEK = "pisell-reservation-schedule.next-week";
const I18N_PREV_MONTH = "pisell-reservation-schedule.prev-month";
const I18N_NEXT_MONTH = "pisell-reservation-schedule.next-month";
const I18N_PICK_DATE = "pisell-reservation-schedule.pick-date";
const I18N_PICK_DATE_TITLE = "pisell-reservation-schedule.pick-date-title";
const I18N_PICK_WEEK_TITLE = "pisell-reservation-schedule.pick-week-title";
const I18N_PICK_MONTH_TITLE = "pisell-reservation-schedule.pick-month-title";
function parseDay(input, fallback) {
	if (input == null) return fallback.startOf("day");
	const d = (0, dayjs.default)(input);
	return (d.isValid() ? d : fallback).startOf("day");
}
function toDayjsLocale(engineLocale) {
	if (!engineLocale || engineLocale === "en") return "en";
	if (engineLocale === "zh-HK") return "zh-hk";
	return "zh-cn";
}
/** 与 RecordBoard 日历 `startOfWeek` 一致：周一起算 */
function startOfWeekMonday(d) {
	const day = d.day();
	const diff = day === 0 ? -6 : 1 - day;
	return d.add(diff, "day").startOf("day");
}
/**
* 营业日导航条：上一日、中间日期文案、下一日（深色条样式，适配预约顶栏）
*/
function PisellReservationSchedule(props) {
	var _context$engine;
	const { value: valueProp, onChange, defaultValue, minDate: minDateProp, maxDate: maxDateProp, navigationLocked = false, navigationLockedDisplay = "full", navigationUnit = "day", className, style } = props;
	const context = require_useEngineContext.default();
	const engineLocale = (context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.locale) || "en";
	_pisell_utils.locales.init(require_locales.default, engineLocale);
	const today = (0, react.useMemo)(() => (0, dayjs.default)().startOf("day"), []);
	const isControlled = valueProp !== void 0;
	const [internal, setInternal] = (0, react.useState)(() => parseDay(valueProp !== null && valueProp !== void 0 ? valueProp : defaultValue, today));
	const value = isControlled ? parseDay(valueProp, today) : internal;
	const minDate = (0, react.useMemo)(() => minDateProp != null ? parseDay(minDateProp, today) : void 0, [minDateProp, today]);
	const maxDate = (0, react.useMemo)(() => maxDateProp != null ? parseDay(maxDateProp, today) : void 0, [maxDateProp, today]);
	const emit = (0, react.useCallback)((next) => {
		let d = next.startOf("day");
		if (minDate && d.isBefore(minDate)) d = minDate;
		if (maxDate && d.isAfter(maxDate)) d = maxDate;
		if (!isControlled) setInternal(d);
		onChange === null || onChange === void 0 || onChange(d);
	}, [
		isControlled,
		onChange,
		minDate,
		maxDate
	]);
	const prevTarget = (0, react.useMemo)(() => {
		if (navigationUnit === "month") return value.subtract(1, "month");
		if (navigationUnit === "week") return value.subtract(7, "day");
		return value.subtract(1, "day");
	}, [navigationUnit, value]);
	const nextTarget = (0, react.useMemo)(() => {
		if (navigationUnit === "month") return value.add(1, "month");
		if (navigationUnit === "week") return value.add(7, "day");
		return value.add(1, "day");
	}, [navigationUnit, value]);
	const prevDisabled = navigationLocked || (minDate ? prevTarget.isBefore(minDate, "day") : false);
	const nextDisabled = navigationLocked || (maxDate ? nextTarget.isAfter(maxDate, "day") : false);
	const [calendarOpen, setCalendarOpen] = (0, react.useState)(false);
	const centerLabel = (0, react.useMemo)(() => {
		const djLoc = toDayjsLocale(engineLocale);
		const dl = value.locale(djLoc);
		if (navigationUnit === "month") return dl.format(djLoc === "en" ? "MMM YYYY" : "YYYY年M月");
		if (navigationUnit === "week") {
			const ws = startOfWeekMonday(value);
			const we = ws.add(6, "day");
			if (djLoc === "en") return `${ws.format("MMM D")} – ${we.format("MMM D, YYYY")}`;
			return `${ws.format("M月D日")} – ${we.format("M月D日")}`;
		}
		const isToday = value.isSame(today, "day");
		const todayText = _pisell_utils.locales.getText(I18N_TODAY);
		const dateStr = djLoc === "en" ? dl.format("MMM D") : dl.format("M月D日");
		const weekStr = dl.format("ddd");
		const parts = [];
		if (isToday && todayText) parts.push(todayText);
		parts.push(dateStr);
		parts.push(weekStr);
		return parts.join(djLoc === "en" ? " · " : " · ");
	}, [
		value,
		today,
		engineLocale,
		navigationUnit
	]);
	const prevAria = navigationUnit === "month" ? _pisell_utils.locales.getText(I18N_PREV_MONTH) : navigationUnit === "week" ? _pisell_utils.locales.getText(I18N_PREV_WEEK) : _pisell_utils.locales.getText(I18N_PREV);
	const nextAria = navigationUnit === "month" ? _pisell_utils.locales.getText(I18N_NEXT_MONTH) : navigationUnit === "week" ? _pisell_utils.locales.getText(I18N_NEXT_WEEK) : _pisell_utils.locales.getText(I18N_NEXT);
	const pickAria = navigationUnit === "month" ? _pisell_utils.locales.getText(I18N_PICK_MONTH_TITLE) : navigationUnit === "week" ? _pisell_utils.locales.getText(I18N_PICK_WEEK_TITLE) : _pisell_utils.locales.getText(I18N_PICK_DATE);
	const modalTitle = navigationUnit === "month" ? _pisell_utils.locales.getText(I18N_PICK_MONTH_TITLE) : navigationUnit === "week" ? _pisell_utils.locales.getText(I18N_PICK_WEEK_TITLE) : _pisell_utils.locales.getText(I18N_PICK_DATE_TITLE);
	/** 场控等场景：不要整套只读导航条，仅展示日期文案 */
	if (navigationLocked && navigationLockedDisplay === "labelOnly") return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("pisell-reservation-schedule", "pisell-reservation-schedule--label-only", className),
		style
	}, /* @__PURE__ */ react.default.createElement("span", {
		className: "pisell-reservation-schedule-label pisell-reservation-schedule-label--static",
		"aria-label": centerLabel
	}, centerLabel));
	return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("pisell-reservation-schedule", className),
		style
	}, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-reservation-schedule-inner" }, /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: "pisell-reservation-schedule-btn",
		"aria-label": prevAria,
		disabled: prevDisabled,
		onClick: () => {
			if (navigationLocked) return;
			emit(prevTarget);
		}
	}, /* @__PURE__ */ react.default.createElement(_pisell_icon_es_ChevronLeft.default, { className: "pisell-reservation-schedule-btn-icon" })), navigationLocked ? /* @__PURE__ */ react.default.createElement("span", {
		className: "pisell-reservation-schedule-label pisell-reservation-schedule-label--static",
		"aria-label": centerLabel
	}, centerLabel) : /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: "pisell-reservation-schedule-label pisell-reservation-schedule-label-trigger",
		"aria-label": pickAria,
		"aria-haspopup": "dialog",
		"aria-expanded": calendarOpen,
		onClick: () => setCalendarOpen(true)
	}, centerLabel), /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: "pisell-reservation-schedule-btn",
		"aria-label": nextAria,
		disabled: nextDisabled,
		onClick: () => {
			if (navigationLocked) return;
			emit(nextTarget);
		}
	}, /* @__PURE__ */ react.default.createElement(_pisell_icon_es_ChevronRight.default, { className: "pisell-reservation-schedule-btn-icon" }))), /* @__PURE__ */ react.default.createElement(antd.Modal, {
		title: modalTitle,
		open: !navigationLocked && calendarOpen,
		onCancel: () => setCalendarOpen(false),
		footer: null,
		width: "auto",
		centered: true,
		destroyOnClose: true,
		wrapClassName: "pisell-reservation-schedule-date-modal"
	}, navigationUnit === "month" ? /* @__PURE__ */ react.default.createElement("div", { className: "pisell-reservation-schedule-month-picker-wrap" }, /* @__PURE__ */ react.default.createElement(_pisell_date_picker.DateCalendar, {
		value,
		views: ["year", "month"],
		openTo: "month",
		minDate,
		maxDate,
		shouldDisableMonth: (month) => {
			const start = (0, dayjs.default)(month).startOf("month");
			if (minDate && start.isBefore(minDate.startOf("month"))) return true;
			if (maxDate && start.isAfter(maxDate.startOf("month"))) return true;
			return false;
		},
		onChange: (d) => {
			if (!d) return;
			emit((0, dayjs.default)(d).startOf("month"));
			setCalendarOpen(false);
		}
	})) : /* @__PURE__ */ react.default.createElement(_pisell_date_picker.DateCalendar, {
		value,
		minDate,
		maxDate,
		onChange: (d) => {
			if (!d) return;
			const picked = (0, dayjs.default)(d);
			if (navigationUnit === "week") emit(startOfWeekMonday(picked));
			else emit(picked);
			setCalendarOpen(false);
		}
	})));
}
PisellReservationSchedule.displayName = "PisellReservationSchedule";
//#endregion
exports.PisellReservationSchedule = PisellReservationSchedule;
