const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_locale = require("../../utils/locale.js");
const require_index = require("../../locales/index.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_types = require("./types.js");
let react = require("react");
react = require_runtime.__toESM(react);
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let antd_mobile = require("antd-mobile");
let _pisell_icon_es_Calendar = require("@pisell/icon/es/Calendar");
_pisell_icon_es_Calendar = require_runtime.__toESM(_pisell_icon_es_Calendar);
let antd_mobile_es_locales_zh_CN = require("antd-mobile/es/locales/zh-CN");
antd_mobile_es_locales_zh_CN = require_runtime.__toESM(antd_mobile_es_locales_zh_CN);
let antd_mobile_es_locales_zh_TW = require("antd-mobile/es/locales/zh-TW");
antd_mobile_es_locales_zh_TW = require_runtime.__toESM(antd_mobile_es_locales_zh_TW);
let antd_mobile_es_locales_zh_HK = require("antd-mobile/es/locales/zh-HK");
antd_mobile_es_locales_zh_HK = require_runtime.__toESM(antd_mobile_es_locales_zh_HK);
let antd_mobile_es_locales_en_US = require("antd-mobile/es/locales/en-US");
antd_mobile_es_locales_en_US = require_runtime.__toESM(antd_mobile_es_locales_en_US);
let antd_mobile_es_locales_ja_JP = require("antd-mobile/es/locales/ja-JP");
antd_mobile_es_locales_ja_JP = require_runtime.__toESM(antd_mobile_es_locales_ja_JP);
let antd_mobile_es_locales_pt_BR = require("antd-mobile/es/locales/pt-BR");
antd_mobile_es_locales_pt_BR = require_runtime.__toESM(antd_mobile_es_locales_pt_BR);
require("antd-mobile/es/global/global.css");
require("./PisellMobileDateRangePicker.less");
//#region src/components/pisellMobileDateRangePicker/PisellMobileDateRangePicker.tsx
/**
* 项目语言 → antd-mobile 内置 locale 包映射。
* 对齐 packages/materials/src/locales/index.ts 的 langMap。
* 未匹配时回落 en-US。
*/
const ANTD_MOBILE_LOCALE_MAP = {
	"zh-CN": antd_mobile_es_locales_zh_CN.default,
	"zh-TW": antd_mobile_es_locales_zh_TW.default,
	"zh-HK": antd_mobile_es_locales_zh_HK.default,
	"en-US": antd_mobile_es_locales_en_US.default,
	en: antd_mobile_es_locales_en_US.default,
	original: antd_mobile_es_locales_en_US.default,
	ja: antd_mobile_es_locales_ja_JP.default,
	pt: antd_mobile_es_locales_pt_BR.default
};
/** 高于 antd Popover 默认 1030，避免嵌套在齿轮/Filter 弹层下被遮挡 */
const FILTER_POPUP_Z_INDEX = 1100;
/**
* 默认日期范围：当前月起往前 1 年。
* - antd-mobile `CalendarPickerView` 默认窗口仅「打开月 +6 个月」，业务上预约/筛选场景
*   常需要至少能往前查 1 年。
* - 该组件 `renderBody` 是「一次性把 min..max 之间所有月份都渲染」，无虚拟滚动；
*   ±1 年（约 13 个月）DOM 量在 ≈400 cells，开销可接受。
* - 业务方仍可用 `min` / `max` props 显式覆盖。
*/
function getDefaultMin() {
	return (0, dayjs.default)().subtract(1, "year").startOf("month").toDate();
}
function getDefaultMax() {
	return (0, dayjs.default)().endOf("month").toDate();
}
/** 与 FilterItem `toRangePickerValue` 一致：兼容 form 中的 dayjs[] / string[] / Date[] */
function toDayjsRange(val) {
	if (val == null) return null;
	if (!Array.isArray(val) || val.length < 2) return null;
	const start = val[0] ? dayjs.default.isDayjs(val[0]) ? val[0] : (0, dayjs.default)(val[0]) : null;
	const end = val[1] ? dayjs.default.isDayjs(val[1]) ? val[1] : (0, dayjs.default)(val[1]) : null;
	if (!(start === null || start === void 0 ? void 0 : start.isValid()) || !(end === null || end === void 0 ? void 0 : end.isValid())) return null;
	return [start, end];
}
function toDateRange(val) {
	const normalized = toDayjsRange(val);
	if (!normalized) return null;
	return [normalized[0].toDate(), normalized[1].toDate()];
}
function formatRangeLabel(range, format, placeholder) {
	if (!range) return placeholder;
	return `${range[0].format(format)} - ${range[1].format(format)}`;
}
function resolveFilterGetContainer() {
	if (typeof document === "undefined") return null;
	return document.body;
}
/**
* antd-mobile CalendarPicker（range）薄封装：受控 value/onChange 与 RecordBoard 快筛 dayjs[] 对齐。
*/
const PisellMobileDateRangePicker = ({ value, onChange, format = "DD/MM/YYYY", placeholder, className, style, title, confirmText, allowClear = true, min, max, variant = "toolbar", getContainer, popupClassName, popupStyle, onOpen, onClose }) => {
	var _ANTD_MOBILE_LOCALE_M, _popupStyle$zIndex;
	const [visible, setVisible] = (0, react.useState)(false);
	const dayjsRange = (0, react.useMemo)(() => toDayjsRange(value), [value]);
	const pickerValue = (0, react.useMemo)(() => toDateRange(value), [value]);
	/**
	* 弹层「内部受控」缓冲：
	* - antd-mobile `CalendarPickerView` 内部 `usePropsValue` 每次渲染都执行
	*   `if (value !== undefined) stateRef.current = value`，因此一旦透传非空 `value`，
	*   单格点选触发的 `setDateRange + onChange` 不写回外层 form 时，下一次重渲染会立刻把
	*   内部累计选择覆盖回 prop 原值，表现为「点格子完全无反应」。
	* - 解决：始终把 `value` 接到本地 `innerValue`，并把 `onChange` 写回 `innerValue`，
	*   形成真正的「弹层内部受控」。打开弹层时再用外部 form 值同步一次初值。
	*   `onConfirm` 时把 `innerValue` 写回外部 form。
	*/
	const [innerValue, setInnerValue] = (0, react.useState)(pickerValue);
	(0, react.useEffect)(() => {
		if (visible) setInnerValue(pickerValue);
	}, [visible]);
	const resolvedPlaceholder = placeholder !== null && placeholder !== void 0 ? placeholder : require_index.getText("pisell-mobile-date-range-picker-placeholder");
	const resolvedTitle = title !== null && title !== void 0 ? title : require_index.getText("pisell-mobile-date-range-picker-title");
	const resolvedConfirmText = confirmText !== null && confirmText !== void 0 ? confirmText : require_index.getText("pisell-mobile-date-range-picker-confirm");
	const antMobileLocale = (_ANTD_MOBILE_LOCALE_M = ANTD_MOBILE_LOCALE_MAP[require_locale.getCurrentLocale()]) !== null && _ANTD_MOBILE_LOCALE_M !== void 0 ? _ANTD_MOBILE_LOCALE_M : antd_mobile_es_locales_en_US.default;
	const isFilterVariant = variant === "filter";
	const openPicker = (0, react.useCallback)(() => {
		setVisible(true);
		onOpen === null || onOpen === void 0 || onOpen();
	}, [onOpen]);
	const closePicker = (0, react.useCallback)(() => {
		setVisible(false);
		onClose === null || onClose === void 0 || onClose();
	}, [onClose]);
	const handleConfirm = (0, react.useCallback)((next) => {
		if (!next) {
			onChange === null || onChange === void 0 || onChange(void 0);
			closePicker();
			return;
		}
		onChange === null || onChange === void 0 || onChange([(0, dayjs.default)(next[0]).startOf("day"), (0, dayjs.default)(next[1]).startOf("day")]);
		closePicker();
	}, [onChange, closePicker]);
	const resolvedGetContainer = getContainer !== null && getContainer !== void 0 ? getContainer : isFilterVariant ? resolveFilterGetContainer : void 0;
	const resolvedPopupClassName = (0, classnames.default)(isFilterVariant ? require_types.PISELL_MOBILE_DATE_RANGE_PICKER_FILTER_POPUP_CLASS : null, popupClassName);
	const resolvedPopupStyle = isFilterVariant ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, popupStyle), {}, { zIndex: (_popupStyle$zIndex = popupStyle === null || popupStyle === void 0 ? void 0 : popupStyle.zIndex) !== null && _popupStyle$zIndex !== void 0 ? _popupStyle$zIndex : FILTER_POPUP_Z_INDEX }) : popupStyle;
	return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("pisell-mobile-date-range-picker", { "pisell-mobile-date-range-picker--filter": isFilterVariant }, className),
		style
	}, /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: (0, classnames.default)("pisell-mobile-date-range-picker__trigger", { "pisell-mobile-date-range-picker__trigger--placeholder": !dayjsRange }),
		onClick: openPicker
	}, /* @__PURE__ */ react.default.createElement("span", { className: "pisell-mobile-date-range-picker__trigger-text" }, formatRangeLabel(dayjsRange, format, resolvedPlaceholder)), isFilterVariant ? /* @__PURE__ */ react.default.createElement(_pisell_icon_es_Calendar.default, {
		className: "pisell-mobile-date-range-picker__trigger-icon",
		"aria-hidden": true
	}) : null), /* @__PURE__ */ react.default.createElement(antd_mobile.ConfigProvider, { locale: antMobileLocale }, /* @__PURE__ */ react.default.createElement(antd_mobile.CalendarPicker, {
		visible,
		selectionMode: "range",
		value: innerValue !== null && innerValue !== void 0 ? innerValue : void 0,
		onChange: (v) => setInnerValue(v),
		onClose: closePicker,
		onMaskClick: closePicker,
		closeOnMaskClick: true,
		onConfirm: handleConfirm,
		title: resolvedTitle,
		confirmText: resolvedConfirmText,
		allowClear,
		min: min !== null && min !== void 0 ? min : getDefaultMin(),
		max: max !== null && max !== void 0 ? max : getDefaultMax(),
		getContainer: resolvedGetContainer,
		popupClassName: resolvedPopupClassName || void 0,
		popupStyle: resolvedPopupStyle
	})));
};
//#endregion
exports.PisellMobileDateRangePicker = PisellMobileDateRangePicker;
