import { getChannelSelectOptions, getCustomerSelectOptions, getPaymentSelectOptions } from "../../orderList/serve.js";
import { getOrderStatusMap, getPaymentStatusMap } from "./order.js";
import ResourceSelector from "../components/ResourceSelector.js";
import { locales } from "@pisell/utils";
//#region src/plus/pisellSalesManagement/config/booking.ts
const getBookingStatusMap = () => ({
	new: locales.getText("pisell2.salesManagement.booking.status.new"),
	confirmed: locales.getText("pisell2.salesManagement.booking.status.confirmed"),
	arrived: locales.getText("pisell2.salesManagement.booking.status.arrived"),
	started: locales.getText("pisell2.salesManagement.booking.status.started"),
	cancelled: locales.getText("pisell2.salesManagement.booking.status.cancelled"),
	no_show: locales.getText("pisell2.salesManagement.booking.status.no_show"),
	completed: locales.getText("pisell2.salesManagement.booking.status.completed")
});
const BOOKING_STATUS_COLOR_MAP = {
	new: "#91caff",
	confirmed: "#b7eb8f",
	arrived: "#d3adf7",
	started: "#fffb8f",
	cancelled: "#bfbfbf",
	no_show: "#ffa39e",
	completed: "#ffd591"
};
const getBookingPlatformMap = () => ({
	online_store: locales.getText("pisell2.salesManagement.booking.platform.online_store"),
	wholesalebusiness: locales.getText("pisell2.salesManagement.booking.platform.wholesalebusiness"),
	"table-order": locales.getText("pisell2.salesManagement.booking.platform.table-order"),
	mobile_order: locales.getText("pisell2.salesManagement.booking.platform.mobile_order"),
	kiosk: locales.getText("pisell2.salesManagement.booking.platform.kiosk"),
	pos: locales.getText("pisell2.salesManagement.booking.platform.pos")
});
const getBookingHeaderLabels = () => ({
	bookingBrief: locales.getText("pisell2.salesManagement.booking.column.bookingBrief"),
	serviceTime: locales.getText("pisell2.salesManagement.booking.column.serviceTime"),
	customer: locales.getText("pisell2.salesManagement.booking.column.customer"),
	contact: locales.getText("pisell2.salesManagement.booking.column.contact"),
	holder: locales.getText("pisell2.salesManagement.booking.column.holder"),
	services: locales.getText("pisell2.salesManagement.booking.column.services"),
	pax: locales.getText("pisell2.salesManagement.booking.column.pax"),
	resource: locales.getText("pisell2.salesManagement.booking.column.resource"),
	forms: locales.getText("pisell2.salesManagement.booking.column.forms"),
	orderInfo: locales.getText("pisell2.salesManagement.booking.column.orderInfo"),
	bookingId: locales.getText("pisell2.salesManagement.booking.column.bookingId"),
	orderOrigin: locales.getText("pisell2.salesManagement.booking.column.orderOrigin"),
	updated: locales.getText("pisell2.salesManagement.booking.column.updated"),
	orderNote: locales.getText("pisell2.salesManagement.booking.common.orderNote"),
	internalNote: locales.getText("pisell2.salesManagement.booking.common.internalNote"),
	more: locales.getText("pisell2.salesManagement.booking.common.more"),
	createdBy: locales.getText("pisell2.salesManagement.booking.common.createdBy"),
	guest: locales.getText("pisell2.salesManagement.order.common.guest"),
	walkIn: locales.getText("pisell2.salesManagement.order.common.walkIn")
});
/** 预约看板 / Sales Monitor 等默认拉取的订单支付态（与 filter order_payment_status 对齐） */
const defaultPaymentStatusKeys = [
	"payment_processing",
	"partially_paid",
	"paid",
	"partially_refunded",
	"refunded"
];
const getFilterButtonList = () => {
	const BOOKING_STATUS_MAP = getBookingStatusMap();
	const ORDER_STATUS_MAP = getOrderStatusMap();
	const PAYMENT_STATUS_MAP = getPaymentStatusMap();
	return [
		{
			name: "appointment_status",
			type: "single",
			key: "appointmentStatus",
			label: locales.getText("pisell2.salesManagement.booking.filter.bookingStatus.label"),
			other: {
				allowClear: true,
				mode: "multiple",
				maxTagCount: "responsive",
				placeholder: locales.getText("pisell2.salesManagement.booking.filter.bookingStatus.placeholder"),
				options: Object.entries(BOOKING_STATUS_MAP).map(([key, value]) => ({
					label: value,
					value: key
				}))
			}
		},
		{
			name: "number_between",
			type: "numberRange",
			key: "number_between",
			label: locales.getText("pisell2.salesManagement.booking.filter.paxRange.label"),
			other: {
				placeholder: locales.getText("pisell2.salesManagement.booking.filter.paxRange.placeholder"),
				minInputNumberProps: {
					min: 0,
					precision: 0
				},
				maxInputNumberProps: {
					min: 0,
					precision: 0
				}
			}
		},
		{
			name: "form_record_ids",
			type: "custom",
			key: "formRecordIds",
			label: locales.getText("pisell2.salesManagement.booking.filter.resource.label"),
			sort: true,
			localFilter: false,
			other: {
				allowClear: true,
				mode: "multiple",
				maxTagCount: "responsive",
				component: ResourceSelector
			}
		},
		{
			name: "order_sales_channel",
			type: "single",
			key: "orderSalesChannel",
			label: locales.getText("pisell2.salesManagement.booking.filter.salesChannel.label"),
			other: {
				allowClear: true,
				mode: "multiple",
				maxTagCount: "responsive",
				placeholder: locales.getText("pisell2.salesManagement.booking.filter.salesChannel.placeholder"),
				options: () => getChannelSelectOptions().then((res) => res.map((item) => ({
					label: item.name,
					value: item.code
				})))
			}
		},
		{
			name: "total_amount_between",
			type: "numberRange",
			key: "total_amount_between",
			label: locales.getText("pisell2.salesManagement.booking.filter.orderAmount.label"),
			other: { placeholder: locales.getText("pisell2.salesManagement.booking.filter.orderAmount.placeholder") }
		},
		{
			name: "order_status",
			type: "single",
			key: "order_status",
			label: locales.getText("pisell2.salesManagement.booking.filter.orderStatus.label"),
			other: {
				allowClear: true,
				mode: "multiple",
				maxTagCount: "responsive",
				placeholder: locales.getText("pisell2.salesManagement.booking.filter.orderStatus.placeholder"),
				options: Object.entries(ORDER_STATUS_MAP).map(([key, value]) => ({
					label: value,
					value: key
				}))
			}
		},
		{
			name: "order_payment_status",
			type: "single",
			key: "orderPaymentStatus",
			label: locales.getText("pisell2.salesManagement.booking.filter.paymentStatus.label"),
			other: {
				allowClear: true,
				mode: "multiple",
				placeholder: locales.getText("pisell2.salesManagement.booking.filter.paymentStatus.placeholder"),
				options: Object.entries(PAYMENT_STATUS_MAP).map(([key, value]) => ({
					label: value,
					value: key
				})),
				maxTagCount: "responsive"
			}
		},
		{
			name: "customer_id",
			type: "single",
			key: "customer_id",
			label: locales.getText("pisell2.salesManagement.booking.filter.customer.label"),
			other: {
				allowClear: true,
				isSearchServer: true,
				showSearch: true,
				placeholder: locales.getText("pisell2.salesManagement.booking.filter.customer.placeholder"),
				options: (params) => getCustomerSelectOptions(params).then((res) => res.map((item) => ({
					label: item.display_name,
					value: item.id
				})))
			}
		},
		{
			name: "payment_method",
			type: "single",
			key: "payment_method",
			label: locales.getText("pisell2.salesManagement.booking.filter.paymentMethod.label"),
			other: {
				allowClear: true,
				mode: "multiple",
				maxTagCount: "responsive",
				placeholder: locales.getText("pisell2.salesManagement.booking.filter.paymentMethod.placeholder"),
				options: () => getPaymentSelectOptions().then((res) => {
					const map = new Map(res.map((item) => [item.code, {
						label: item.name,
						value: item.code
					}]));
					return Array.from(map.values());
				})
			}
		},
		{
			name: "created_at_between",
			type: "rangePicker",
			key: "createdAtBetween",
			label: locales.getText("pisell2.salesManagement.booking.filter.orderDate.label"),
			other: {
				placeholder: locales.getText("pisell2.salesManagement.booking.filter.orderDate.placeholder"),
				showTime: true
			}
		},
		{
			name: "updated_at_between",
			type: "rangePicker",
			key: "updatedAtBetween",
			label: locales.getText("pisell2.salesManagement.booking.filter.updatedDate.label"),
			other: {
				placeholder: locales.getText("pisell2.salesManagement.booking.filter.updatedDate.placeholder"),
				showTime: true
			}
		}
	];
};
const getSortList = () => {
	return [
		{
			type: "time",
			isCustom: true,
			name: "after_execution_time",
			label: locales.getText("pisell2.salesManagement.booking.sort.afterExecutionTime"),
			getSortItems: () => {
				return [{
					key: "after_execution_time:asc",
					label: locales.getText("pisell2.salesManagement.sort.oldestFirst")
				}];
			}
		},
		{
			type: "time",
			isCustom: true,
			name: "start_at",
			label: locales.getText("pisell2.salesManagement.booking.sort.startAt"),
			getSortItems: () => {
				return [{
					key: "start_at:asc",
					label: locales.getText("pisell2.salesManagement.sort.oldestFirst")
				}, {
					key: "start_at:desc",
					label: locales.getText("pisell2.salesManagement.sort.latestFirst")
				}];
			}
		},
		{
			type: "time",
			isCustom: true,
			name: "end_at",
			label: locales.getText("pisell2.salesManagement.booking.sort.endAt"),
			getSortItems: () => {
				return [{
					key: "end_at:asc",
					label: locales.getText("pisell2.salesManagement.sort.oldestFirst")
				}, {
					key: "end_at:desc",
					label: locales.getText("pisell2.salesManagement.sort.latestFirst")
				}];
			}
		},
		{
			type: "time",
			isCustom: true,
			name: "created_at",
			label: locales.getText("pisell2.salesManagement.booking.sort.createdAt"),
			getSortItems: () => {
				return [{
					key: "created_at:asc",
					label: locales.getText("pisell2.salesManagement.sort.oldestFirst")
				}, {
					key: "created_at:desc",
					label: locales.getText("pisell2.salesManagement.sort.newestFirst")
				}];
			}
		},
		{
			type: "time",
			isCustom: true,
			name: "updated_at",
			label: locales.getText("pisell2.salesManagement.booking.sort.updatedAt"),
			getSortItems: () => {
				return [{
					key: "updated_at:asc",
					label: locales.getText("pisell2.salesManagement.sort.oldestFirst")
				}, {
					key: "updated_at:desc",
					label: locales.getText("pisell2.salesManagement.sort.newestFirst")
				}];
			},
			other: { style: { width: "100%" } }
		}
	];
};
//#endregion
export { BOOKING_STATUS_COLOR_MAP, defaultPaymentStatusKeys, getBookingHeaderLabels, getBookingPlatformMap, getBookingStatusMap, getFilterButtonList, getSortList };
