import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import React, { useMemo } from "react";
import { DatePicker, Input } from "antd";
import "./detePickerCpt.less";
//#region src/components/pisellDatePicker/datePickerCpt.tsx
const _excluded = [
	"format",
	"value",
	"onChange",
	"onDateChange"
];
/**
* 校验时间字符串是否符合格式
* @param dateString
* @param format
*/
const isValidDate = (dateString, format) => {
	return window.dayjs(dateString, format, true).isValid();
};
const DatePickerInput = (props) => {
	const { format, value, onChange, onDateChange } = props, others = _objectWithoutProperties(props, _excluded);
	/**
	* 数据输入变更事件
	* @param val
	*/
	const eventChange = (val) => {
		onChange === null || onChange === void 0 || onChange({ target: { value: val } });
		if (isValidDate(val, format)) onDateChange === null || onDateChange === void 0 || onDateChange(window.dayjs(val, format), window.dayjs(val, format).format(format));
	};
	const handleChange = (e) => {
		let inputValue = e.target.value;
		if (!/^[\d/]*$/.test(inputValue)) return;
		let parts = format.split(/[^a-zA-Z]/);
		let separators = format.match(/[^a-zA-Z]+/g) || [];
		if (inputValue.length < value.length) {
			eventChange === null || eventChange === void 0 || eventChange(inputValue);
			return;
		}
		let cursor = 0;
		let result = "";
		let lastIndex = 0;
		for (let i = 0; i < parts.length; i++) {
			const part = parts[i];
			if (!part) continue;
			const separator = separators[i] || "";
			const expectedLength = part.length;
			lastIndex = lastIndex + part.length + separator.length;
			let start = cursor;
			if (i > 0) start += separators.slice(0, i).join("").length;
			let end = start + expectedLength;
			let userPart = inputValue.slice(start, end);
			if (userPart.length > 0) result += separators[i - 1] || "";
			if (userPart.length === 0 && result.slice(result.length - 1) !== inputValue.slice(result.length - 1)) userPart = inputValue.slice(result.length - 1);
			if (part.includes("Y")) result += userPart;
			else {
				if (userPart.length === 1 && [
					"M",
					"D",
					"H",
					"m",
					"s"
				].includes(part[0])) {
					if ("M" === part[0] && userPart > 1) userPart = "0" + userPart;
					if ("D" === part[0] && userPart > 3) userPart = "0" + userPart;
					if ("H" === part[0] && userPart > 2) userPart = "0" + userPart;
					if ("m" === part[0] && userPart > 7) userPart = "0" + userPart;
					if ("s" === part[0] && userPart > 7) userPart = "0" + userPart;
				}
				result += userPart;
			}
			if (userPart.length === expectedLength && !inputValue.slice(lastIndex - 1).startsWith(separator)) result += separator;
			cursor += userPart.length;
		}
		eventChange === null || eventChange === void 0 || eventChange(result);
	};
	return /* @__PURE__ */ React.createElement(Input, _objectSpread2(_objectSpread2({}, others), {}, {
		type: "tel",
		value,
		onChange: handleChange
	}));
};
/**
* 当前日期组件实例
* @param props
* @constructor
*/
const DatePickerCpt = (props) => {
	const { format, type, onChange, size, picker } = props;
	/**
	* 不同类型时注入参数
	*/
	const propsObj = useMemo(() => {
		if (picker && picker !== "date") return { inputRender: void 0 };
		if (type === "input") return {
			popupClassName: "pisell-date-picker-type-input",
			suffixIcon: null,
			placeholder: format || ""
		};
		else if (type === "select") return { inputReadOnly: true };
		return {};
	}, [
		type,
		picker,
		format
	]);
	return /* @__PURE__ */ React.createElement(DatePicker, _objectSpread2(_objectSpread2({}, props), {}, {
		className: "pisell-date-picker",
		inputRender: (props) => {
			return /* @__PURE__ */ React.createElement(DatePickerInput, _objectSpread2(_objectSpread2({}, props), {}, {
				onDateChange: onChange,
				format
			}));
		}
	}, propsObj));
};
//#endregion
export { DatePickerCpt as default };
