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 { flexibleObj, getDiscountTime, getEndDate, sliceDayIntoFiveMinutes } from "../../utils.js";
import dayjs from "dayjs";
import React, { useCallback, useEffect, useMemo } from "react";
import { Select } from "antd";
import { locales } from "@pisell/utils";
//#region src/components/booking/materiels/duration/index.tsx
const _excluded = [
	"locale",
	"slice",
	"timeObjChange",
	"selectProps",
	"duration",
	"usableTime",
	"startDate",
	"timeObj"
];
const Duration = (props) => {
	const { locale = "en", slice = 5, timeObjChange, selectProps, duration, usableTime, startDate, timeObj } = props, resetProps = _objectWithoutProperties(props, _excluded);
	const startDateStr = useMemo(() => dayjs(startDate).format("YYYY-MM-DD HH:mm:ss"), [startDate]);
	/**
	* @title: 时间切片
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-09 13:37
	*/
	const times = useMemo(() => {
		if (timeObj) return timeObj.durationSlicesBasedOnTime;
		let { durationSlicesBasedOnTime } = sliceDayIntoFiveMinutes(slice, locale);
		let _duration = typeof duration === "object" ? duration.type : duration;
		if (_duration === "flexible") {
			let timeValue = getEndDate(startDate, _duration, (usableTime === null || usableTime === void 0 ? void 0 : usableTime.end_at) ? dayjs(usableTime === null || usableTime === void 0 ? void 0 : usableTime.end_at) : null).diff(startDate, "minute");
			durationSlicesBasedOnTime.unshift({
				label: locales.getText("pisell2.text.flexible"),
				value: flexibleObj.create(timeValue)
			});
		}
		if (resetProps.value && !durationSlicesBasedOnTime.some((d) => d.value === resetProps.value)) durationSlicesBasedOnTime.unshift({
			label: getDiscountTime(resetProps.value),
			value: resetProps.value,
			disabled: true
		});
		return durationSlicesBasedOnTime;
	}, [
		timeObj === null || timeObj === void 0 ? void 0 : timeObj.durationSlicesBasedOnTime,
		slice,
		locale,
		duration,
		startDateStr,
		usableTime === null || usableTime === void 0 ? void 0 : usableTime.end_at
	]);
	useEffect(() => {
		timeObjChange === null || timeObjChange === void 0 || timeObjChange(times);
	}, [times]);
	const renderOptions = useCallback((array, value = "value", label = "label") => {
		return array.map((d) => {
			return /* @__PURE__ */ React.createElement(Select.Option, {
				value: d[value],
				key: d[value],
				disabled: d.disabled
			}, d[label]);
		});
	}, []);
	const options = useMemo(() => {
		console.log("render form options");
		return renderOptions(times);
	}, [times]);
	return /* @__PURE__ */ React.createElement(Select, _objectSpread2(_objectSpread2({}, selectProps), resetProps), options);
};
//#endregion
export { Duration as default };
