import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import usePisellConfig from "../../../pisell-config-provider/hooks/usePisellConfig.js";
import { withMode } from "../../dataSourceForm/utils.js";
import { omit } from "../../../table/Table/utils.js";
import SelectTime from "../../../select-time/index.js";
import TimePickerReadPretty from "./ReadPretty.js";
import React, { useMemo } from "react";
import { isString } from "@pisell/utils";
import { TimePicker } from "antd";
import { ClockCircleOutlined } from "@ant-design/icons";
import classNames from "classnames";
import dayjs from "dayjs";
import "./style.less";
//#region src/components/dataSourceComponents/fields/TimePicker/WithMode.tsx
const TimePicker$1 = (props) => {
	const config = usePisellConfig();
	const { format = "HH:mm", className, size = "middle", value, onChange, disabled, placeholder, defaultValue: propsDefaultValue } = props;
	const normalizedValue = useMemo(() => {
		if (!value) return void 0;
		if (dayjs.isDayjs(value)) return value;
		try {
			return dayjs(String(value), format);
		} catch (_unused) {
			return;
		}
	}, [value, format]);
	const defaultValue = useMemo(() => {
		if (!propsDefaultValue) return void 0;
		if (dayjs.isDayjs(propsDefaultValue)) return propsDefaultValue;
		try {
			return dayjs(String(propsDefaultValue), format);
		} catch (_unused2) {
			return;
		}
	}, [propsDefaultValue, format]);
	if (config.platform === "h5") {
		const sizeClassMap = {
			large: "pisell-h5-time-picker-lg",
			middle: "",
			small: "pisell-h5-time-picker-sm"
		};
		const timeFormat = isString(format) ? format : "HH:mm:ss";
		const stepValue = timeFormat.includes("ss") ? 1 : 60;
		return /* @__PURE__ */ React.createElement("div", { className: "pisell-h5-time-picker-wrapper" }, /* @__PURE__ */ React.createElement("input", {
			type: "time",
			step: stepValue,
			value: (normalizedValue === null || normalizedValue === void 0 ? void 0 : normalizedValue.format(timeFormat)) || "",
			onChange: (e) => {
				const timeString = e.target.value;
				if (onChange && timeString) {
					const timeValue = dayjs(`${dayjs().format("YYYY-MM-DD")} ${timeString}`);
					onChange(timeValue, timeValue.format(timeFormat));
				}
			},
			disabled,
			placeholder,
			className: classNames("pisell-h5-time-picker", sizeClassMap[size], className)
		}), /* @__PURE__ */ React.createElement(ClockCircleOutlined, { className: "pisell-h5-time-picker-suffix" }));
	}
	if (format === "HH:mm") return /* @__PURE__ */ React.createElement(SelectTime, {
		value: normalizedValue === null || normalizedValue === void 0 ? void 0 : normalizedValue.format("HH:mm"),
		onChange: (timeString) => {
			if (onChange) onChange(timeString ? dayjs(`${dayjs().format("YYYY-MM-DD")} ${timeString}`) : null, timeString || "");
		},
		timePickerProps: _objectSpread2({ format }, omit(props, ["value", "defaultValue"]) || {}),
		popoverProps: {},
		timeProps: {},
		slice: 15
	});
	return /* @__PURE__ */ React.createElement(TimePicker, _objectSpread2(_objectSpread2({}, props), {}, {
		value: normalizedValue,
		defaultValue
	}));
};
const TimePickerWithMode = withMode(TimePicker$1, TimePickerReadPretty);
//#endregion
export { TimePickerWithMode as default };
