import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { _objectWithoutProperties } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import { request } from "../../utils/index.js";
import dayjs from "dayjs";
//#region src/plus/pisellSalesGrid/serve.ts
const _excluded = [
	"orderDate",
	"total_amount",
	"sort"
], _excluded2 = [
	"booking_time_start_between",
	"created_at_between",
	"updated_at_between",
	"number_between",
	"total_amount_between",
	"sort"
];
const formatParams = (params) => {
	const { orderDate, total_amount, sort: sort_key } = params, rest = _objectWithoutProperties(params, _excluded);
	let order_by, sort;
	if (sort_key) {
		const [orderByValue, sortValue] = sort_key.split(":");
		order_by = orderByValue;
		sort = sortValue;
	}
	return _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({}, rest), (orderDate === null || orderDate === void 0 ? void 0 : orderDate[0]) && { start_time: dayjs(orderDate[0]).format("YYYY-MM-DD") }), (orderDate === null || orderDate === void 0 ? void 0 : orderDate[1]) && { end_time: dayjs(orderDate[1]).format("YYYY-MM-DD") }), (total_amount === null || total_amount === void 0 ? void 0 : total_amount.min) !== void 0 && { min_total_amount: total_amount.min }), (total_amount === null || total_amount === void 0 ? void 0 : total_amount.max) !== void 0 && { max_total_amount: total_amount.max }), order_by && { order_by }), sort && { sort }), {}, { with: [
		"customer",
		"contactsInfo",
		"tag",
		"createAccount",
		"paidPayment",
		"lastEditAccount"
	] });
};
const getOrderList = function() {
	var _ref = _asyncToGenerator(function* (params) {
		const res = yield request.getRequest().post(`/shop/order/v2/list`, formatParams(params), {
			abort: true,
			fullResult: true,
			isShopApi: true
		});
		return res === null || res === void 0 ? void 0 : res.data;
	});
	return function getOrderList(_x) {
		return _ref.apply(this, arguments);
	};
}();
const formatBookingParams = (params) => {
	const { booking_time_start_between, created_at_between, updated_at_between, number_between, total_amount_between, sort } = params, rest = _objectWithoutProperties(params, _excluded2);
	const bookingTimeRange = Array.isArray(booking_time_start_between) ? booking_time_start_between.map((value) => value ? dayjs(value).format("YYYY-MM-DD HH:mm:ss") : void 0).filter((value) => value) : void 0;
	const createdRange = Array.isArray(created_at_between) ? created_at_between.map((value) => value ? dayjs(value).format("YYYY-MM-DD HH:mm:ss") : void 0).filter((value) => value) : void 0;
	const updatedRange = Array.isArray(updated_at_between) ? updated_at_between.map((value) => value ? dayjs(value).format("YYYY-MM-DD HH:mm:ss") : void 0).filter((value) => value) : void 0;
	const numberRange = Array.isArray(number_between) ? number_between : (number_between === null || number_between === void 0 ? void 0 : number_between.min) != null || (number_between === null || number_between === void 0 ? void 0 : number_between.max) != null ? [number_between === null || number_between === void 0 ? void 0 : number_between.min, number_between === null || number_between === void 0 ? void 0 : number_between.max] : void 0;
	const amountRange = Array.isArray(total_amount_between) ? total_amount_between : (total_amount_between === null || total_amount_between === void 0 ? void 0 : total_amount_between.min) != null || (total_amount_between === null || total_amount_between === void 0 ? void 0 : total_amount_between.max) != null ? [total_amount_between === null || total_amount_between === void 0 ? void 0 : total_amount_between.min, total_amount_between === null || total_amount_between === void 0 ? void 0 : total_amount_between.max] : void 0;
	let ext = sort === "after_execution_time:asc" ? { after_execution_time: dayjs().format("YYYY-MM-DD HH:mm:ss") } : { order_by: sort };
	return _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({}, rest), ext), (bookingTimeRange === null || bookingTimeRange === void 0 ? void 0 : bookingTimeRange.length) && { sales_time_between: bookingTimeRange }), (createdRange === null || createdRange === void 0 ? void 0 : createdRange.length) && { created_at_between: createdRange }), (updatedRange === null || updatedRange === void 0 ? void 0 : updatedRange.length) && { updated_at_between: updatedRange }), numberRange && { number_between: numberRange }), amountRange && { total_amount_between: amountRange }), {}, {
		with: [
			"order.contactsInfo",
			"order.customer",
			"order.payment",
			"appointmentForm.formRecord",
			"form",
			"contacts",
			"eventResources.relationForm",
			"notes",
			"holder.formRecord",
			"holderBySelf.formRecord"
		],
		holder_data: 1,
		child_booking: 1
	});
};
const formatBookingData = (data) => {
	return data.map((item) => {
		var _item$order, _item$order_detail;
		const product_uid = item === null || item === void 0 ? void 0 : item.product_uid;
		const currentProduct = (Array.isArray(item === null || item === void 0 || (_item$order = item.order) === null || _item$order === void 0 ? void 0 : _item$order.products) ? item.order.products : []).find((product) => product.booking_uid === product_uid);
		const orderBase = (item === null || item === void 0 ? void 0 : item.order) != null && typeof item.order === "object" ? item.order : {};
		return _objectSpread2(_objectSpread2({}, item), {}, {
			order_detail: (_item$order_detail = item === null || item === void 0 ? void 0 : item.order_detail) !== null && _item$order_detail !== void 0 ? _item$order_detail : currentProduct != null ? JSON.stringify(currentProduct) : void 0,
			id: item.schedule_event_id,
			order: _objectSpread2(_objectSpread2({}, orderBase), {}, { id: item.order_id })
		});
	});
};
const formatBookingResult = (data) => {
	return _objectSpread2(_objectSpread2({}, data), {}, { list: formatBookingData((data === null || data === void 0 ? void 0 : data.list) || []) });
};
const getBookingList = function() {
	var _ref2 = _asyncToGenerator(function* (params, options) {
		const { data } = yield request.getRequest().get(`/shop/schedule/booking`, formatBookingParams(params), _objectSpread2({
			abort: true,
			fullResult: true,
			isShopApi: true
		}, options));
		return formatBookingResult(data);
	});
	return function getBookingList(_x2, _x3) {
		return _ref2.apply(this, arguments);
	};
}();
(function() {
	var _ref3 = _asyncToGenerator(function* (subscriberId) {
		yield request.getRequest().post("/shop/order/v2/list/unsubscribe", { subscriberId }, {
			isShopApi: true,
			osServer: true
		});
	});
	return function unsubscribeOrderQuery(_x4) {
		return _ref3.apply(this, arguments);
	};
})();
(function() {
	var _ref4 = _asyncToGenerator(function* (subscriberId) {
		yield request.getRequest().get("/shop/schedule/booking/unsubscribe", { subscriberId }, {
			isShopApi: true,
			osServer: true
		});
	});
	return function unsubscribeBookingQuery(_x5) {
		return _ref4.apply(this, arguments);
	};
})();
//#endregion
export { formatBookingResult, getBookingList, getOrderList };
