const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let lodash_debounce = require("lodash/debounce");
lodash_debounce = require_runtime.__toESM(lodash_debounce);
//#region src/components/pisellSelectCustomerModal/RightPanel/Add/BirthdayField/index.tsx
const { Option } = antd.Select;
const getSelectInput = (id) => {
	const input = document.getElementById(id);
	if (input) return input;
};
const setSelectInputType = ({ input, type }) => {
	if (input) input.type = type;
};
const DateSectionCom = ({ value, onChange, showTime, className = "", style = {}, useSpace = true, format, itemStyle = { width: 100 } }) => {
	const currentYear = (0, dayjs.default)().year();
	const locale = _pisell_utils.locales.locale;
	const dayRef = (0, react.useRef)(null);
	const monthRef = (0, react.useRef)(null);
	const yearRef = (0, react.useRef)(null);
	const [date, setDate] = (0, react.useState)({
		year: value ? (0, dayjs.default)(value).year() : null,
		month: value ? (0, dayjs.default)(value).month() + 1 : null,
		day: value ? (0, dayjs.default)(value).date() : null
	});
	const [dayOptions, setDayOptions] = (0, react.useState)([]);
	const [monthOptions, setMonthOptions] = (0, react.useState)([]);
	const [yearOptions, setYearOptions] = (0, react.useState)([]);
	const localeMap = {
		year: _pisell_utils.locales.getText("pisell2.text.date-year-text"),
		month: _pisell_utils.locales.getText("pisell2.text.date-month-text"),
		day: _pisell_utils.locales.getText("pisell2.text.date-day-text")
	};
	(0, react.useEffect)(() => {
		setDate({
			year: value ? (0, dayjs.default)(value).year() : null,
			month: value ? (0, dayjs.default)(value).month() + 1 : null,
			day: value ? (0, dayjs.default)(value).date() : null
		});
	}, [value]);
	(0, react.useEffect)(() => {
		setMonthOptions(renderOptions("month", 1, 12));
		setYearOptions(renderOptions("year", currentYear - 100, currentYear + 50));
	}, []);
	(0, react.useEffect)(() => {
		setDayOptions(renderOptions("day", 1, date.month ? getDaysInMonth(date.year || currentYear, date.month) : 31));
	}, [date.month, date.year]);
	/**
	* @title: 下拉框变化
	* @description:
	* @param {any} field
	* @param {any} value
	* @return {*}
	* @Author: chensheng.gu
	* @Date: 2024-03-22 17:49
	*/
	const handleDateChange = (field, value) => {
		if (!value) return;
		let newDate = {
			year: date.year,
			month: date.month,
			day: date.day,
			[field]: value
		};
		const foucsList = {
			day: monthRef,
			month: yearRef
		};
		if (field === "month" || field === "year") {
			const daysInMonth = getDaysInMonth((newDate === null || newDate === void 0 ? void 0 : newDate.year) || currentYear, newDate === null || newDate === void 0 ? void 0 : newDate.month);
			const newDay = (newDate === null || newDate === void 0 ? void 0 : newDate.day) > daysInMonth ? daysInMonth : newDate.day;
			newDate = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, newDate), {}, { day: newDay });
		}
		setDate(newDate);
		if (foucsList[field]) foucsList[field].current.focus();
		else yearRef.current.blur();
		const _format = format || "YYYY-MM-DD";
		if (newDate.year && newDate.month && newDate.day) onChange((0, dayjs.default)(`${newDate.year}-${newDate.month.toString().padStart(2, "0")}-${newDate.day.toString().padStart(2, "0")}`).format(_format));
	};
	/**
	* @title: 获取日期
	* @description:
	* @param {any} year
	* @param {any} month
	* @return {*}
	* @Author: chensheng.gu
	* @Date: 2024-03-22 17:49
	*/
	const getDaysInMonth = (year, month) => {
		return (0, dayjs.default)(`${year}-${month}`, "YYYY-MM").daysInMonth();
	};
	/**
	* @title: 渲染下拉框列表
	* @description:
	* @param {any} field
	* @param {any} start
	* @param {any} end
	* @return {*}
	* @Author: chensheng.gu
	* @Date: 2024-03-22 17:50
	*/
	const renderOptions = (field, start, end) => {
		const options = [];
		const _text = locale !== "en-US" && locale !== "en" ? localeMap[field] : "";
		for (let i = start; i <= end; i++) options.push({
			label: `${i}${_text}`,
			value: i
		});
		return options;
	};
	const selectId = (0, react.useMemo)(() => {
		return {
			dayId: (0, _pisell_utils.getUniqueId)("day"),
			monthId: (0, _pisell_utils.getUniqueId)("month"),
			yearId: (0, _pisell_utils.getUniqueId)("year")
		};
	}, []);
	(0, react.useLayoutEffect)(() => {
		setSelectInputType({
			input: getSelectInput(selectId.dayId),
			type: "number"
		});
		setSelectInputType({
			input: getSelectInput(selectId.monthId),
			type: "number"
		});
		setSelectInputType({
			input: getSelectInput(selectId.yearId),
			type: "number"
		});
	}, [selectId]);
	const handleDateSearch = (0, react.useCallback)((0, lodash_debounce.default)((field, value) => {
		const dateOption = {
			day: dayOptions,
			month: monthOptions,
			year: yearOptions
		};
		const _value = Number(value);
		if (dateOption[field].findIndex((item) => {
			return item.value === _value;
		}) !== -1) handleDateChange(field, _value);
		else handleDateChange(field, null);
	}, 300), [
		dayOptions,
		monthOptions,
		yearOptions,
		date
	]);
	const content = /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(antd.Select, {
		id: selectId.dayId,
		value: date.day,
		placeholder: _pisell_utils.locales.getText("pisell2.text.date-day-text"),
		onChange: (value) => handleDateChange("day", value),
		style: itemStyle,
		showSearch: true,
		onSearch: (value) => {
			handleDateSearch("day", value);
		},
		ref: dayRef,
		size: "large"
	}, dayOptions.map((item) => {
		return /* @__PURE__ */ react.default.createElement(Option, {
			key: item.value,
			value: item.value
		}, item.label);
	})), /* @__PURE__ */ react.default.createElement(antd.Select, {
		id: selectId.monthId,
		value: date.month,
		placeholder: _pisell_utils.locales.getText("pisell2.text.date-month-text"),
		onChange: (value) => handleDateChange("month", value),
		style: itemStyle,
		showSearch: true,
		onSearch: (value) => {
			handleDateSearch("month", value);
		},
		ref: monthRef,
		size: "large"
	}, monthOptions.map((item) => {
		return /* @__PURE__ */ react.default.createElement(Option, {
			key: item.value,
			value: item.value
		}, item.label);
	})), /* @__PURE__ */ react.default.createElement(antd.Select, {
		id: selectId.yearId,
		value: date.year,
		placeholder: _pisell_utils.locales.getText("pisell2.text.date-year-text"),
		onChange: (value) => handleDateChange("year", value),
		style: itemStyle,
		showSearch: true,
		onSearch: (value) => {
			handleDateSearch("year", value);
		},
		ref: yearRef,
		size: "large"
	}, yearOptions.map((item) => {
		return /* @__PURE__ */ react.default.createElement(Option, {
			key: item.value,
			value: item.value
		}, item.label);
	})));
	if (useSpace) return /* @__PURE__ */ react.default.createElement(antd.Space, {
		className: className || "",
		style: style || {}
	}, content);
	return /* @__PURE__ */ react.default.createElement("div", {
		className: className || "",
		style: style || {}
	}, content);
};
//#endregion
exports.default = DateSectionCom;
