import { getChannelSelectOptions, getCustomerSelectOptions, getLocationSelectOptions, getLogisticsSelectOptions, getPaymentSelectOptions, getTagSelectOptions } from "./serve.js";
import React from "react";
import { Tooltip } from "antd";
import { PisellTags } from "@pisell/materials";
//#region src/plus/orderList/config.tsx
/**
* 获取配送方式映射
* @param locales - 多语言对象
*/
const getDeliveryTypeMap = (locales) => ({
	nil: locales.getText("pisell2.orderList.deliveryType.nil"),
	shipping: locales.getText("pisell2.orderList.deliveryType.shipping"),
	local_delivery: locales.getText("pisell2.orderList.deliveryType.local_delivery"),
	pickup: locales.getText("pisell2.orderList.deliveryType.pickup"),
	shop_service: locales.getText("pisell2.orderList.deliveryType.shop_service")
});
/**
* 获取到店服务类型映射
* @param locales - 多语言对象
*/
const getShopServiceTypeMap = (locales) => ({
	"dine-in": locales.getText("pisell2.orderList.shopServiceType.dine-in"),
	takeaway: locales.getText("pisell2.orderList.shopServiceType.takeaway")
});
const getPlatformMap = (locales) => ({
	online_store: locales.getText("pisell2.orderList.platform.online_store"),
	wholesalebusiness: locales.getText("pisell2.orderList.platform.wholesalebusiness"),
	"table-order": locales.getText("pisell2.orderList.platform.table-order"),
	mobile_order: locales.getText("pisell2.orderList.platform.mobile_order"),
	kiosk: locales.getText("pisell2.orderList.platform.kiosk"),
	pos: locales.getText("pisell2.orderList.platform.pos")
});
/**
* 获取订单状态映射
* @param locales - 多语言对象
*/
const getOrderStatusMap = (locales) => ({
	open: locales.getText("pisell2.orderList.orderStatus.open"),
	completed: locales.getText("pisell2.orderList.orderStatus.completed"),
	cancelled: locales.getText("pisell2.orderList.orderStatus.cancelled")
});
const ORDER_STATUS_COLOR_MAP = {
	open: {
		borderColor: "#1570EF",
		textColor: "#175CD3"
	},
	completed: {
		borderColor: "#079455",
		textColor: "#067647"
	},
	cancelled: {
		borderColor: "#D92D20",
		textColor: "#B42318"
	}
};
/**
* 获取处理状态映射
* @param locales - 多语言对象
*/
const getShippingStatusMap = (locales) => ({
	fulfilled: locales.getText("pisell2.orderList.shippingStatus.falseulfilled"),
	unfulfilled: locales.getText("pisell2.orderList.shippingStatus.unfulfilled"),
	fulfilling: locales.getText("pisell2.orderList.shippingStatus.fulfilling"),
	partial_fulfilled: locales.getText("pisell2.orderList.shippingStatus.partial_fulfilled")
});
const SHIPPING_STATUS_COLOR_MAP = {
	fulfilling: {
		borderColor: "#DC6803",
		textColor: "#B54708"
	},
	unfulfilled: {
		borderColor: "#DC6803",
		textColor: "#B54708"
	},
	partial_fulfilled: {
		borderColor: "#088AB2",
		textColor: "#0E7090"
	},
	fulfilled: {
		borderColor: "#475467",
		textColor: "#344054"
	}
};
/**
* 获取支付状态映射
* @param locales - 多语言对象
*/
const getPaymentStatusMap = (locales) => ({
	authorized: locales.getText("pisell2.orderList.paymentStatus.authorized"),
	paid: locales.getText("pisell2.orderList.paymentStatus.paid"),
	partially_refunded: locales.getText("pisell2.orderList.paymentStatus.partially_refunded"),
	payment_pending: locales.getText("pisell2.orderList.paymentStatus.payment_pending"),
	refunded: locales.getText("pisell2.orderList.paymentStatus.refunded"),
	unpaid: locales.getText("pisell2.orderList.paymentStatus.unpaid"),
	voided: locales.getText("pisell2.orderList.paymentStatus.voided"),
	payment_processing: locales.getText("pisell2.orderList.paymentStatus.payment_processing")
});
const PAYMENT_STATUS_COLOR_MAP = {
	unpaid: "#B2DDFF",
	payment_processing: "#FEDF89",
	payment_pending: "#FEDF89",
	authorized: "#ABEFC6",
	paid: "#ABEFC6",
	refunded: "#FECDCA",
	partially_refunded: "#FCCEEE",
	voided: "#D0D5DD"
};
const renderMapValue = (value, map) => {
	return map[value] || "";
};
/**
* 获取表格列配置（支持多语言）
* @param locales - 多语言对象
* @returns 表格列配置数组
*/
const getTableColumns = (locales) => [
	{
		title: locales.getText("pisell2.orderList.column.orderNumber"),
		dataIndex: "shop_full_order_number",
		key: "shop_full_order_number",
		width: 100,
		render: (value, record) => {
			const { note } = record || {};
			return /* @__PURE__ */ React.createElement("div", { className: "order-id-cell" }, /* @__PURE__ */ React.createElement("span", { className: "order-id-link" }, value), note && /* @__PURE__ */ React.createElement(Tooltip, { title: /* @__PURE__ */ React.createElement("div", { style: { whiteSpace: "pre-wrap" } }, note) }, /* @__PURE__ */ React.createElement("span", { className: "order-id-link-badge cursor-pointer" }, locales.getText("pisell2.orderList.common.note"))));
		}
	},
	{
		title: locales.getText("pisell2.orderList.column.customerInfo"),
		dataIndex: "customer",
		key: "customer",
		width: 200,
		render: (value, record) => {
			const { email, phone } = value || {};
			const customer_name = (value === null || value === void 0 ? void 0 : value.customer_name) || (record === null || record === void 0 ? void 0 : record.customer_name);
			if (value === null || value === void 0 ? void 0 : value.guest_code) return /* @__PURE__ */ React.createElement("div", { className: "compound-cell" }, /* @__PURE__ */ React.createElement("div", { className: "title" }, locales.getText("pisell2.orderList.common.guest")));
			return /* @__PURE__ */ React.createElement("div", { className: "compound-cell" }, /* @__PURE__ */ React.createElement("div", { className: "title" }, customer_name || "-"), /* @__PURE__ */ React.createElement("div", { className: "subtitle" }, phone || "-"), /* @__PURE__ */ React.createElement("div", { className: "subtitle" }, email || "-"));
		}
	},
	{
		title: locales.getText("pisell2.orderList.column.contactInfo"),
		dataIndex: "contacts_info",
		key: "contacts_info",
		width: 200,
		render: (value, record) => {
			const { first_name, last_name, display_name, phone: phoneInfo, email } = (value === null || value === void 0 ? void 0 : value.value) || {};
			const { country_calling_code, country_code, phone } = phoneInfo || {};
			const _name = display_name || `${first_name || ""} ${last_name || ""}`;
			return /* @__PURE__ */ React.createElement("div", { className: "compound-cell" }, /* @__PURE__ */ React.createElement("div", { className: "title" }, `${_name || "-"}`), /* @__PURE__ */ React.createElement("div", { className: "subtitle" }, phone || "-"), /* @__PURE__ */ React.createElement("div", { className: "subtitle" }, email || "-"));
		}
	},
	{
		title: locales.getText("pisell2.orderList.column.tags"),
		dataIndex: "tag",
		key: "tag",
		width: 200,
		render: (value) => {
			if (Array.isArray(value)) return /* @__PURE__ */ React.createElement("div", { className: "tags-container" }, value.map((item, index) => /* @__PURE__ */ React.createElement(PisellTags, { key: index }, item.name)));
			return "-";
		}
	},
	{
		title: locales.getText("pisell2.orderList.column.deliveryMethod"),
		dataIndex: "delivery_type",
		key: "delivery_type",
		width: 160,
		render: (value, record) => {
			const { shop_service_type } = record.metadata || {};
			const SHOP_SERVICE_TYPE_MAP = getShopServiceTypeMap(locales);
			const DELIVERY_TYPE_MAP = getDeliveryTypeMap(locales);
			let node = null;
			if (value === "shop_service") node = renderMapValue(shop_service_type === null || shop_service_type === void 0 ? void 0 : shop_service_type.server_type, SHOP_SERVICE_TYPE_MAP);
			node = renderMapValue(value, DELIVERY_TYPE_MAP);
			return node ? /* @__PURE__ */ React.createElement(PisellTags, { style: { whiteSpace: "nowrap" } }, node) : "-";
		}
	},
	{
		title: locales.getText("pisell2.orderList.column.paymentInfo"),
		dataIndex: "payment_status",
		key: "payment_status",
		width: 140,
		render: (value, record) => {
			const PAYMENT_STATUS_MAP = getPaymentStatusMap(locales);
			/** 部分付并入「需付款」：文案/色与 unpaid 一致 */
			const paymentKey = value === "partially_paid" ? "unpaid" : value;
			const total_amount = Number(record.total_amount || 0);
			const paid_amount = Number(record.paid_amount || 0);
			const total_refund_amount = Number(record.total_refund_amount || 0);
			const unpaid = Number(total_amount - paid_amount).toFixed(2);
			let tooltipContent = "";
			if ([
				"payment_pending",
				"payment_processing",
				"unpaid"
			].includes(paymentKey)) tooltipContent = `${locales.getText("pisell2.orderList.common.totalAmount")}: $${total_amount.toFixed(2)}\n${locales.getText("pisell2.orderList.common.unpaidAmount")}: $${unpaid}`;
			else if (paymentKey === "paid") tooltipContent = `${locales.getText("pisell2.orderList.common.totalAmount")}: $${total_amount.toFixed(2)}\n${locales.getText("pisell2.orderList.common.paidAmount")}: $${paid_amount.toFixed(2)}`;
			else if (["partially_refunded", "refunded"].includes(value)) tooltipContent = `${locales.getText("pisell2.orderList.common.totalAmount")}: $${total_amount.toFixed(2)}\n${locales.getText("pisell2.orderList.common.paidAmount")}: $${paid_amount.toFixed(2)}\n${locales.getText("pisell2.orderList.common.refundedAmount")}: $${total_refund_amount.toFixed(2)}`;
			const color = PAYMENT_STATUS_COLOR_MAP[paymentKey] || "";
			if (["voided", "authorized"].includes(value)) return /* @__PURE__ */ React.createElement(PisellTags, {
				className: "cursor-pointer",
				style: {
					backgroundColor: color,
					whiteSpace: "nowrap"
				}
			}, renderMapValue(paymentKey, PAYMENT_STATUS_MAP) || "-");
			return /* @__PURE__ */ React.createElement(Tooltip, { title: /* @__PURE__ */ React.createElement("div", { style: { whiteSpace: "pre-wrap" } }, tooltipContent) }, /* @__PURE__ */ React.createElement(PisellTags, {
				className: "cursor-pointer",
				style: {
					backgroundColor: color,
					whiteSpace: "nowrap"
				}
			}, renderMapValue(paymentKey, PAYMENT_STATUS_MAP)));
		}
	},
	{
		title: locales.getText("pisell2.orderList.column.orderChannel"),
		dataIndex: "order_sales_channel",
		key: "order_sales_channel",
		width: 160,
		render: (value) => {
			return renderMapValue(value, getPlatformMap(locales)) || "-";
		}
	},
	{
		title: locales.getText("pisell2.orderList.column.productQuantity"),
		dataIndex: "product_quantity",
		key: "product_quantity",
		width: 180,
		isShow: false,
		render: (value) => {
			return `${Number(value || 0)}${locales.getText("pisell2.orderList.common.itemsCount")}`;
		}
	},
	{
		title: locales.getText("pisell2.orderList.column.orderStatus"),
		dataIndex: "status",
		key: "status",
		width: 140,
		isShow: false,
		render: (value) => {
			const ORDER_STATUS_MAP = getOrderStatusMap(locales);
			const color = ORDER_STATUS_COLOR_MAP[value];
			let style = {};
			if (color) style = {
				borderColor: color.borderColor,
				color: color.textColor,
				whiteSpace: "nowrap"
			};
			return /* @__PURE__ */ React.createElement(PisellTags, {
				className: "cursor-pointer",
				style
			}, renderMapValue(value, ORDER_STATUS_MAP) || "-");
		}
	},
	{
		title: locales.getText("pisell2.orderList.column.processStatus"),
		dataIndex: "shipping_status",
		key: "shipping_status",
		width: 140,
		isShow: false,
		render: (value) => {
			const SHIPPING_STATUS_MAP = getShippingStatusMap(locales);
			const color = SHIPPING_STATUS_COLOR_MAP[value];
			let style = {};
			if (color) style = {
				borderColor: color.borderColor,
				color: color.textColor,
				whiteSpace: "nowrap"
			};
			return /* @__PURE__ */ React.createElement(PisellTags, {
				className: "cursor-pointer",
				style
			}, renderMapValue(value, SHIPPING_STATUS_MAP) || "-");
		}
	},
	{
		title: locales.getText("pisell2.orderList.column.createInfo"),
		dataIndex: "create_account",
		key: "create_account",
		isShow: false,
		width: 180,
		render: (value, record) => {
			const { created_at, create_account_id } = record || {};
			const { first_name, last_name } = value || {};
			let display_name = `${first_name || ""} ${last_name || ""}`;
			if (Number(create_account_id) > 0) display_name = locales.getText("pisell2.orderList.filter.customer.label");
			return /* @__PURE__ */ React.createElement("div", { className: "compound-cell" }, /* @__PURE__ */ React.createElement("div", { className: "title" }, `${display_name || "-"}`), /* @__PURE__ */ React.createElement("div", { className: "subtitle" }, created_at || "-"));
		}
	},
	{
		title: locales.getText("pisell2.orderList.column.moreInfo"),
		dataIndex: "last_edit_account",
		key: "last_edit_account",
		isShow: false,
		width: 180,
		render: (value) => {
			if (value) {
				const { account, created_at } = value || {};
				const { display_name } = account || {};
				return /* @__PURE__ */ React.createElement("div", { className: "compound-cell" }, /* @__PURE__ */ React.createElement("div", { className: "title" }, `${display_name || "-"}`), /* @__PURE__ */ React.createElement("div", { className: "subtitle" }, created_at || "-"));
			}
			return "-";
		}
	}
];
const getQuickFilterList = (locales) => {
	return [{
		name: "orderDate",
		type: "rangePicker",
		key: "orderDate",
		label: locales.getText("pisell2.orderList.quickFilter.orderDate.label"),
		other: { placeholder: locales.getText("pisell2.orderList.quickFilter.orderDate.placeholder") }
	}, {
		name: "keyword",
		type: "search",
		key: "keyword",
		other: {
			allowClear: true,
			placeholder: locales.getText("pisell2.orderList.quickFilter.keyword.placeholder"),
			trigger: ["onChange"],
			style: { width: "400px" }
		}
	}];
};
const getFilterButtonList = (locales) => {
	const ORDER_STATUS_MAP = getOrderStatusMap(locales);
	const PAYMENT_STATUS_MAP = getPaymentStatusMap(locales);
	const SHIPPING_STATUS_MAP = getShippingStatusMap(locales);
	const DELIVERY_TYPE_MAP = getDeliveryTypeMap(locales);
	return [
		{
			name: "status",
			type: "select",
			key: "status",
			label: locales.getText("pisell2.orderList.filter.orderStatus.label"),
			other: {
				allowClear: true,
				mode: "multiple",
				placeholder: locales.getText("pisell2.orderList.filter.orderStatus.placeholder"),
				options: Object.entries(ORDER_STATUS_MAP).map(([key, value]) => ({
					label: value,
					value: key
				}))
			}
		},
		{
			name: "payment_status",
			type: "select",
			key: "payment_status",
			label: locales.getText("pisell2.orderList.filter.paymentStatus.label"),
			other: {
				allowClear: true,
				mode: "multiple",
				placeholder: locales.getText("pisell2.orderList.filter.paymentStatus.placeholder"),
				options: Object.entries(PAYMENT_STATUS_MAP).map(([key, value]) => ({
					label: value,
					value: key
				}))
			}
		},
		{
			name: "shipping_status",
			type: "select",
			key: "shipping_status",
			label: locales.getText("pisell2.orderList.filter.shippingStatus.label"),
			other: {
				allowClear: true,
				mode: "multiple",
				placeholder: locales.getText("pisell2.orderList.filter.shippingStatus.placeholder"),
				options: Object.entries(SHIPPING_STATUS_MAP).map(([key, value]) => ({
					label: value,
					value: key
				}))
			}
		},
		{
			name: "customer_id",
			type: "select",
			key: "customer_id",
			label: locales.getText("pisell2.orderList.filter.customer.label"),
			other: {
				allowClear: true,
				isSearchServer: true,
				showSearch: true,
				placeholder: locales.getText("pisell2.orderList.filter.customer.placeholder"),
				options: (params) => getCustomerSelectOptions(params).then((res) => res.map((item) => ({
					label: item.display_name,
					value: item.id
				})))
			}
		},
		{
			name: "order_sales_channel",
			type: "select",
			key: "order_sales_channel",
			label: locales.getText("pisell2.orderList.filter.salesChannel.label"),
			other: {
				allowClear: true,
				mode: "multiple",
				placeholder: locales.getText("pisell2.orderList.filter.salesChannel.placeholder"),
				options: () => getChannelSelectOptions().then((res) => res.map((item) => ({
					label: item.name,
					value: item.code
				})))
			}
		},
		{
			name: "payment_methods",
			type: "select",
			key: "payment_methods",
			label: locales.getText("pisell2.orderList.filter.paymentMethod.label"),
			other: {
				allowClear: true,
				mode: "multiple",
				placeholder: locales.getText("pisell2.orderList.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: "total_amount",
			type: "numberRange",
			key: "total_amount",
			label: locales.getText("pisell2.orderList.filter.totalAmount.label"),
			other: { placeholder: locales.getText("pisell2.orderList.filter.totalAmount.placeholder") }
		},
		{
			name: "tag_ids",
			type: "select",
			key: "tag_ids",
			label: locales.getText("pisell2.orderList.filter.tags.label"),
			other: {
				allowClear: true,
				mode: "multiple",
				isSearchServer: true,
				showSearch: true,
				placeholder: locales.getText("pisell2.orderList.filter.tags.placeholder"),
				options: (params) => getTagSelectOptions(params).then((res) => res.map((item) => ({
					label: item.name,
					value: item.id
				})))
			}
		},
		{
			name: "shipping_type",
			type: "select",
			key: "shipping_type",
			label: locales.getText("pisell2.orderList.filter.shippingType.label"),
			other: {
				allowClear: true,
				mode: "multiple",
				placeholder: locales.getText("pisell2.orderList.filter.shippingType.placeholder"),
				options: Object.entries(DELIVERY_TYPE_MAP).filter(([key]) => !["nil", "shop_service"].includes(key)).map(([key, value]) => ({
					label: value,
					value: key
				}))
			}
		},
		{
			name: "delivery_zone_name",
			type: "text",
			key: "delivery_zone_name",
			label: locales.getText("pisell2.orderList.filter.deliveryZone.label"),
			other: { placeholder: locales.getText("pisell2.orderList.filter.deliveryZone.placeholder") }
		},
		{
			name: "location_id",
			type: "select",
			key: "location_id",
			label: locales.getText("pisell2.orderList.filter.pickupLocation.label"),
			other: {
				allowClear: true,
				placeholder: locales.getText("pisell2.orderList.filter.pickupLocation.placeholder"),
				options: () => getLocationSelectOptions().then((res) => res.map((item) => ({
					label: item.name,
					value: item.id
				})))
			}
		},
		{
			name: "shipping_order_number",
			type: "text",
			key: "shipping_order_number",
			label: locales.getText("pisell2.orderList.filter.trackingNumber.label"),
			other: { placeholder: locales.getText("pisell2.orderList.filter.trackingNumber.placeholder") }
		},
		{
			name: "logistics_company_id",
			type: "select",
			key: "logistics_company_id",
			label: locales.getText("pisell2.orderList.filter.logistics.label"),
			other: {
				allowClear: true,
				mode: "multiple",
				placeholder: locales.getText("pisell2.orderList.filter.logistics.placeholder"),
				options: () => getLogisticsSelectOptions().then((res) => res.map((item) => ({
					label: item.name,
					value: item.id
				})))
			}
		},
		{
			name: "zip",
			type: "text",
			key: "zip",
			label: locales.getText("pisell2.orderList.filter.zipCode.label"),
			other: { placeholder: locales.getText("pisell2.orderList.filter.zipCode.placeholder") }
		}
	];
};
const getSortList = (locales) => {
	return [
		{
			type: "string",
			isCustom: false,
			name: "shop_full_order_number",
			label: locales.getText("pisell2.orderList.sort.orderNumber")
		},
		{
			type: "time",
			isCustom: false,
			name: "created_at",
			label: locales.getText("pisell2.orderList.sort.createdAt")
		},
		{
			type: "time",
			isCustom: false,
			name: "updated_at",
			label: locales.getText("pisell2.orderList.sort.updatedAt")
		},
		{
			type: "string",
			isCustom: false,
			name: "customer_name",
			label: locales.getText("pisell2.orderList.sort.customerName")
		},
		{
			type: "date",
			isCustom: false,
			name: "payment_status",
			label: locales.getText("pisell2.orderList.sort.paymentStatus")
		},
		{
			type: "date",
			isCustom: false,
			name: "shipping_status",
			label: locales.getText("pisell2.orderList.sort.shippingStatus")
		},
		{
			type: "number",
			isCustom: false,
			name: "total_amount",
			label: locales.getText("pisell2.orderList.sort.totalAmount")
		},
		{
			type: "time",
			isCustom: false,
			name: "delivery_start_at",
			label: locales.getText("pisell2.orderList.sort.deliveryStartAt")
		}
	];
};
//#endregion
export { getFilterButtonList, getQuickFilterList, getSortList, getTableColumns };
