const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_utils = require("../../service2/utils.js");
const require_utils$1 = require("../../../utils.js");
const require_utils$2 = require("../../utils.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_icon_es_AlertTriangle = require("@pisell/icon/es/AlertTriangle");
_pisell_icon_es_AlertTriangle = require_runtime.__toESM(_pisell_icon_es_AlertTriangle);
//#region src/components/booking/info/service/addService/utils.tsx
const getDurationProps = ({ cacheItem, useStartTime, currentResource, slice }) => {
	var _currentResource$_ext;
	let _cacheItem = cacheItem;
	return {
		startDate: (0, dayjs.default)((0, dayjs.default)(_cacheItem._extend.start_date).format("YYYY-MM-DD ") + (require_utils$1.getServeStartTime(useStartTime) || "00:00")),
		usableTime: currentResource === null || currentResource === void 0 || (_currentResource$_ext = currentResource._extend) === null || _currentResource$_ext === void 0 ? void 0 : _currentResource$_ext.usableTime,
		duration: require_utils$1.getDuration(_cacheItem === null || _cacheItem === void 0 ? void 0 : _cacheItem.duration),
		locale: _pisell_utils.locales.locale,
		slice,
		selectProps: { size: "large" }
	};
};
/**
* @title: 灵活时长选项
* @description:
* @param {*} useMemo
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-02-23 10:10
*/
const getFlexibleOption = ({ cacheItem, useStartTime, currentResource, slice, durationProps }) => {
	const _durationProps = durationProps || getDurationProps({
		cacheItem,
		useStartTime,
		currentResource,
		slice
	});
	let _duration = _durationProps.duration;
	if (_duration === "flexible") {
		var _durationProps$usable, _durationProps$usable2;
		let timeValue = require_utils$1.getEndDate(_durationProps.startDate, _duration, ((_durationProps$usable = _durationProps.usableTime) === null || _durationProps$usable === void 0 ? void 0 : _durationProps$usable.end_at) ? (0, dayjs.default)((_durationProps$usable2 = _durationProps.usableTime) === null || _durationProps$usable2 === void 0 ? void 0 : _durationProps$usable2.end_at) : null).diff(_durationProps.startDate, "minute");
		return {
			label: _pisell_utils.locales.getText("pisell2.text.flexible"),
			value: require_utils$1.flexibleObj.create(timeValue)
		};
	}
	return null;
};
/**
* @title: 时间切片
* @description:
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-01-09 13:37
*/
const getTimeObj = ({ cacheItem, useStartTime, currentResource, state, useDuration, durationProps }) => {
	var _state$bookingConfig;
	const flexibleOption = getFlexibleOption({
		cacheItem,
		useStartTime,
		currentResource,
		state,
		durationProps
	});
	let obj = require_utils$1.sliceDayIntoFiveMinutes((_state$bookingConfig = state.bookingConfig) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.config) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.basic) === null || _state$bookingConfig === void 0 ? void 0 : _state$bookingConfig.service_time, _pisell_utils.locales.locale, require_utils.isDayBooking(state));
	let timeSlices = [...obj.timeSlices];
	let durationSlicesBasedOnTime = [...obj.durationSlicesBasedOnTime];
	if (flexibleOption) durationSlicesBasedOnTime.unshift(flexibleOption);
	if (useDuration && !durationSlicesBasedOnTime.some((d) => d.value === useDuration)) durationSlicesBasedOnTime.unshift({
		label: require_utils$1.getDiscountTime(useDuration, require_utils.isDayBooking(state)),
		value: useDuration,
		disabled: true
	});
	if (useStartTime && useStartTime !== "now" && !timeSlices.some((d) => d.value === useStartTime)) timeSlices.unshift({
		label: (0, dayjs.default)(useStartTime, "HH:mm").format("hh:mm A"),
		value: useStartTime
	});
	timeSlices.unshift({
		label: _pisell_utils.locales.getText("pisell2.text.now"),
		value: "now"
	});
	return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, obj), {}, {
		timeSlices,
		durationSlicesBasedOnTime
	});
};
/**
* @title: 获取资源列表
* @description:
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-01-09 13:38
*/
const formatResources = ({ cacheItem, resourcesOrigin, isDay }) => {
	try {
		let _list = [...resourcesOrigin];
		const _cacheItem = cacheItem;
		_list = _list.map((d) => require_utils$1.formatResource(d, _cacheItem, (item) => {
			return /* @__PURE__ */ react.default.createElement("div", { className: "resource-option" }, /* @__PURE__ */ react.default.createElement("div", { className: "like-wrap" }), item.labelText, !item.usable && /* @__PURE__ */ react.default.createElement(_pisell_icon_es_AlertTriangle.default, { className: "resource-option-alert-icon" }));
		}, isDay));
		_list = _list.sort((a, b) => b.usable ? 1 : -1);
		return _list;
	} catch (err) {
		console.log("formatResourcesErr", err);
	}
	return [];
};
const checkCacheItemResource = ({ cacheItem, resources }) => {
	let resourceId = cacheItem._extend.resource;
	if (resourceId || resourceId === 0) {
		if (!resources.find((d) => (d === null || d === void 0 ? void 0 : d.form_record_id) === resourceId)) resourceId = void 0;
	} else if (cacheItem.new) {
		if (resources.length <= 2) {
			var _resources;
			resourceId = resources === null || resources === void 0 || (_resources = resources[(resources === null || resources === void 0 ? void 0 : resources.length) - 1]) === null || _resources === void 0 ? void 0 : _resources.form_record_id;
		}
		if (resources.length > 2) {
			const usableList = resources.filter((d) => d.usable && d.form_id !== 0);
			if (usableList.length === 1) {
				var _usableList$;
				resourceId = usableList === null || usableList === void 0 || (_usableList$ = usableList[0]) === null || _usableList$ === void 0 ? void 0 : _usableList$.form_record_id;
			}
		}
	}
	return resourceId;
};
/**
* @title: 预生成服务的 extend
* @description:
* @param {any} param1
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-06-01 13:25
*/
const getProductExtend = ({ cacheItem, state }) => {
	if (!cacheItem.new && cacheItem._extend.endDate) return cacheItem._extend;
	const resourcesOrigin = state.service.resourcesOrigin || [];
	let currentResource = null;
	if (!cacheItem.new) {
		let item = formatResources({
			cacheItem,
			resourcesOrigin
		}).find((d) => {
			var _cacheItem$_extend;
			return (d === null || d === void 0 ? void 0 : d.form_record_id) == ((_cacheItem$_extend = cacheItem._extend) === null || _cacheItem$_extend === void 0 ? void 0 : _cacheItem$_extend.resource);
		}) || {};
		if (item) currentResource = item;
	}
	let _extend = require_utils$1.getServerItemExtend(state, cacheItem, getTimeObj({
		cacheItem,
		currentResource,
		state
	}));
	const resources = formatResources({
		cacheItem: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, cacheItem), {}, { _extend }),
		resourcesOrigin
	});
	_extend.resource = checkCacheItemResource({
		cacheItem: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, cacheItem), {}, { _extend }),
		resources
	});
	return _extend;
};
const getMultiDay = function() {
	var _ref = require_asyncToGenerator._asyncToGenerator(function* ({ start_date, end_date, state, cacheItem }) {
		let _cacheItem = cacheItem || state.service.cacheItem;
		if (require_utils.isDayBookingProduct(_cacheItem, state)) {
			const { multiDayPrices, price } = yield require_utils.updateAppointmentServicePrice(state, {
				start_date,
				end_date,
				product_id: _cacheItem.product_id
			});
			_cacheItem._extend.multiDayPrices = require_utils$2.getMultiDayPrices(multiDayPrices || []);
			if (!(multiDayPrices === null || multiDayPrices === void 0 ? void 0 : multiDayPrices.length)) _cacheItem._extend.price = price;
			return _cacheItem;
		}
		return _cacheItem;
	});
	return function getMultiDay(_x) {
		return _ref.apply(this, arguments);
	};
}();
//#endregion
exports.formatResources = formatResources;
exports.getDurationProps = getDurationProps;
exports.getFlexibleOption = getFlexibleOption;
exports.getMultiDay = getMultiDay;
exports.getProductExtend = getProductExtend;
exports.getTimeObj = getTimeObj;
