import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import useEngineContext from "../../../hooks/useEngineContext.js";
import ClientName from "../../clientName/index.js";
import { DELIVERY_TYPE_COLOR_MAP, FULFILLMENT_STATUS_COLOR_MAP, ORDER_STATUS_COLOR_MAP, PAYMENT_STATUS_COLOR_MAP, SHOP_SERVICE_TYPE_COLOR_MAP, getDeliveryTypeMap, getFilterButtonList, getOrderHeaderLabels, getOrderStatusMap, getPaymentStatusMap, getPlatformMap, getShippingStatusMap, getShopServiceTypeMap, getSortList } from "../config/order.js";
import { createNoteApis } from "../serve.js";
import { BUSINESS_COLOR_MAP, getBusinessLabelMap } from "../config/index.js";
import { Tag as Tag$1, formatAmount as formatAmount$1, formatPhone, getDisplayLines, textStyles } from "./perspectiveUtils.js";
import BookingNotesPreview from "../components/BookingNotesPreview.js";
import React, { useCallback, useMemo } from "react";
import { locales } from "@pisell/utils";
import { PisellDateTimeDisplay, PisellTags } from "@pisell/materials";
//#region src/plus/pisellSalesManagement/hooks/useOrderPerspective.tsx
/**
* 提取联系人原始对象，兼容 `{ value: {...} }`、对象本体、数组首项等结构。
*
* @example
* const payload = pickContactPayload({ value: { first_name: 'Tom' } });
* console.log(payload.first_name); // Tom
*/
function pickContactPayload(raw) {
	const first = Array.isArray(raw) ? raw[0] : raw;
	if ((first === null || first === void 0 ? void 0 : first.value) && typeof first.value === "object") return first.value;
	if (first && typeof first === "object") return first;
	return {};
}
/**
* 订单行联系人字段归一化：优先 `value`，再回退 `contacts_info` / `contactsInfo`。
*
* @example
* const info = resolveOrderContactInfo(record, record?.contacts_info);
* console.log(info.displayName, info.phone, info.email);
*/
function resolveOrderContactInfo(record, contactValue) {
	var _contact$phone, _contact$phone2;
	const fromValue = pickContactPayload(contactValue);
	const fromSnake = pickContactPayload(record === null || record === void 0 ? void 0 : record.contacts_info);
	const fromCamel = pickContactPayload(record === null || record === void 0 ? void 0 : record.contactsInfo);
	const contact = Object.keys(fromValue).length ? fromValue : Object.keys(fromSnake).length ? fromSnake : fromCamel;
	const firstName = String((contact === null || contact === void 0 ? void 0 : contact.first_name) || "").trim();
	const lastName = String((contact === null || contact === void 0 ? void 0 : contact.last_name) || "").trim();
	return {
		displayName: String((contact === null || contact === void 0 ? void 0 : contact.display_name) || `${firstName} ${lastName}`).trim(),
		phone: typeof (contact === null || contact === void 0 ? void 0 : contact.phone) === "string" ? contact.phone : (contact === null || contact === void 0 || (_contact$phone = contact.phone) === null || _contact$phone === void 0 ? void 0 : _contact$phone.phone) || (contact === null || contact === void 0 || (_contact$phone2 = contact.phone) === null || _contact$phone2 === void 0 ? void 0 : _contact$phone2.value) || (contact === null || contact === void 0 ? void 0 : contact.mobile) || (contact === null || contact === void 0 ? void 0 : contact.phone_number) || "",
		email: (contact === null || contact === void 0 ? void 0 : contact.email) || ""
	};
}
/**
* 订单行配送方式归一化：
* - 店内服务优先读取 `metadata.shop_service_type.server_type`
* - 其次读取 `delivery_type`
* - 兼容 `shop_service_type` 扁平结构
*
* @example
* const method = resolveShippingMethodDisplay(record, {
*   shopServiceTypeLabelMap: SHOP_SERVICE_TYPE_LABEL_MAP,
*   deliveryTypeLabelMap: DELIVERY_TYPE_LABEL_MAP,
* });
* console.log(method?.label);
*/
function resolveShippingMethodDisplay(record, maps) {
	var _record$metadata, _record$shop_service_, _maps$deliveryTypeLab;
	const deliveryType = (record === null || record === void 0 ? void 0 : record.delivery_type) || (record === null || record === void 0 ? void 0 : record.shipping_type) || ((record === null || record === void 0 ? void 0 : record.type) === "delivery" || (record === null || record === void 0 ? void 0 : record.business_code) === "delivery" ? "local_delivery" : void 0);
	const serverType = (record === null || record === void 0 || (_record$metadata = record.metadata) === null || _record$metadata === void 0 || (_record$metadata = _record$metadata.shop_service_type) === null || _record$metadata === void 0 ? void 0 : _record$metadata.server_type) || (record === null || record === void 0 || (_record$shop_service_ = record.shop_service_type) === null || _record$shop_service_ === void 0 ? void 0 : _record$shop_service_.server_type) || (record === null || record === void 0 ? void 0 : record.shop_service_type);
	if (deliveryType === "shop_service" || deliveryType == null && serverType) {
		var _maps$shopServiceType;
		const label = (_maps$shopServiceType = maps.shopServiceTypeLabelMap) === null || _maps$shopServiceType === void 0 ? void 0 : _maps$shopServiceType[serverType];
		if (!label) return null;
		return {
			label,
			color: SHOP_SERVICE_TYPE_COLOR_MAP === null || SHOP_SERVICE_TYPE_COLOR_MAP === void 0 ? void 0 : SHOP_SERVICE_TYPE_COLOR_MAP[serverType]
		};
	}
	const label = (_maps$deliveryTypeLab = maps.deliveryTypeLabelMap) === null || _maps$deliveryTypeLab === void 0 ? void 0 : _maps$deliveryTypeLab[deliveryType];
	if (!label || deliveryType === "nil") return null;
	return {
		label,
		color: DELIVERY_TYPE_COLOR_MAP === null || DELIVERY_TYPE_COLOR_MAP === void 0 ? void 0 : DELIVERY_TYPE_COLOR_MAP[deliveryType]
	};
}
/**
* 解析订单商品数量，兼容顶层 / summary / products 明细三种来源。
*
* @example
* const count = resolveOrderItemsCount({
*   summary: { product_quantity: 1 },
*   products: [{ product_quantity: 2 }],
* });
* console.log(count); // 1（优先 summary）
*/
function resolveOrderItemsCount(record) {
	var _record$summary;
	const topLevelCount = Number(record === null || record === void 0 ? void 0 : record.product_quantity);
	if (Number.isFinite(topLevelCount) && topLevelCount >= 0) return topLevelCount;
	const summaryCount = Number(record === null || record === void 0 || (_record$summary = record.summary) === null || _record$summary === void 0 ? void 0 : _record$summary.product_quantity);
	if (Number.isFinite(summaryCount) && summaryCount >= 0) return summaryCount;
	const products = Array.isArray(record === null || record === void 0 ? void 0 : record.products) ? record.products : [];
	if (products.length === 0) return 0;
	return products.reduce((sum, item) => {
		var _ref, _item$product_quantit;
		const qty = Number((_ref = (_item$product_quantit = item === null || item === void 0 ? void 0 : item.product_quantity) !== null && _item$product_quantit !== void 0 ? _item$product_quantit : item === null || item === void 0 ? void 0 : item.quantity) !== null && _ref !== void 0 ? _ref : 0);
		if (!Number.isFinite(qty) || qty <= 0) return sum;
		return sum + qty;
	}, 0);
}
/**
* 解析「Created by / Updated by」显示对象，兼容新旧订单结构。
* - 旧结构优先：`create_account` / `last_edit_account`
* - 新结构回退：`customer`
* - 最后回退：`customer_name` + 电话/邮箱
*
* @example
* const actor = resolveOrderActorDisplay({
*   customer_name: '佳 贾',
*   phone: '0425541521',
*   country_calling_code: '61',
* });
* console.log(actor?.display_name); // 佳 贾
*/
function resolveOrderActorDisplay(record, preferredActor) {
	const candidate = preferredActor || (record === null || record === void 0 ? void 0 : record.last_edit_account) || (record === null || record === void 0 ? void 0 : record.create_account) || (record === null || record === void 0 ? void 0 : record.customer);
	if (candidate && typeof candidate === "object") {
		const displayName = String((candidate === null || candidate === void 0 ? void 0 : candidate.display_name) || (candidate === null || candidate === void 0 ? void 0 : candidate.customer_name) || [candidate === null || candidate === void 0 ? void 0 : candidate.first_name, candidate === null || candidate === void 0 ? void 0 : candidate.last_name].filter(Boolean).join(" ")).trim();
		if (displayName) return _objectSpread2(_objectSpread2({}, candidate), {}, { display_name: displayName });
	}
	if ((record === null || record === void 0 ? void 0 : record.customer_name) || (record === null || record === void 0 ? void 0 : record.phone) || (record === null || record === void 0 ? void 0 : record.email)) return {
		display_name: (record === null || record === void 0 ? void 0 : record.customer_name) || "",
		phone: (record === null || record === void 0 ? void 0 : record.phone) || "",
		country_calling_code: (record === null || record === void 0 ? void 0 : record.country_calling_code) || "",
		email: (record === null || record === void 0 ? void 0 : record.email) || ""
	};
	return null;
}
/**
* 统一解析订单金额（兼容新结构 summary/payments 与旧结构顶层金额字段）。
*
* @example
* const amounts = resolveOrderPaymentAmounts({
*   summary: { total_amount: '23.28' },
*   payments: [{ status: 'paid', amount: '6.07' }],
* });
* console.log(amounts.totalAmount, amounts.paidAmount, amounts.dueAmount);
*/
function resolveOrderPaymentAmounts(record) {
	var _record$summary2, _record$summary3;
	const summaryTotal = Number(record === null || record === void 0 || (_record$summary2 = record.summary) === null || _record$summary2 === void 0 ? void 0 : _record$summary2.total_amount);
	const topLevelTotal = Number(record === null || record === void 0 ? void 0 : record.total_amount);
	const totalAmount = Number.isFinite(summaryTotal) ? summaryTotal : Number.isFinite(topLevelTotal) ? topLevelTotal : 0;
	const topLevelPaid = Number(record === null || record === void 0 ? void 0 : record.paid_amount);
	const topLevelRefunded = Number((record === null || record === void 0 ? void 0 : record.total_refund_amount) || (record === null || record === void 0 || (_record$summary3 = record.summary) === null || _record$summary3 === void 0 ? void 0 : _record$summary3.total_refund_amount) || 0);
	const payments = Array.isArray(record === null || record === void 0 ? void 0 : record.payments) ? record.payments : [];
	const paidFromPayments = payments.reduce((sum, item) => {
		var _ref2, _item$amount;
		const status = String((item === null || item === void 0 ? void 0 : item.status) || "").toLowerCase();
		if (![
			"paid",
			"captured",
			"success",
			"completed"
		].includes(status)) return sum;
		const amount = Number((_ref2 = (_item$amount = item === null || item === void 0 ? void 0 : item.amount) !== null && _item$amount !== void 0 ? _item$amount : item === null || item === void 0 ? void 0 : item.origin_amount) !== null && _ref2 !== void 0 ? _ref2 : 0);
		if (!Number.isFinite(amount) || amount <= 0) return sum;
		return sum + amount;
	}, 0);
	const refundedFromPayments = payments.reduce((sum, item) => {
		var _ref3, _item$amount2;
		const status = String((item === null || item === void 0 ? void 0 : item.status) || "").toLowerCase();
		if (![
			"refunded",
			"partially_refunded",
			"refund"
		].includes(status)) return sum;
		const amount = Number((_ref3 = (_item$amount2 = item === null || item === void 0 ? void 0 : item.amount) !== null && _item$amount2 !== void 0 ? _item$amount2 : item === null || item === void 0 ? void 0 : item.origin_amount) !== null && _ref3 !== void 0 ? _ref3 : 0);
		if (!Number.isFinite(amount) || amount <= 0) return sum;
		return sum + amount;
	}, 0);
	const paidAmount = Number.isFinite(topLevelPaid) ? topLevelPaid : paidFromPayments;
	return {
		totalAmount,
		paidAmount,
		refundedAmount: Number.isFinite(topLevelRefunded) ? topLevelRefunded : refundedFromPayments,
		dueAmount: Math.max(totalAmount - paidAmount, 0)
	};
}
const useOrderPerspective = () => {
	var _context$appHelper, _context$appHelper$ge, _context$appHelper2, _context$engine;
	const context = useEngineContext();
	const app = context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 || (_context$appHelper$ge = _context$appHelper.getApp) === null || _context$appHelper$ge === void 0 ? void 0 : _context$appHelper$ge.call(_context$appHelper);
	const NativePage = context === null || context === void 0 || (_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.utils) === null || _context$appHelper2 === void 0 ? void 0 : _context$appHelper2.NativePage;
	/** 监听 locale，切换语言时重新计算 label/filter/sort 等文案 */
	const locale = context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.locale;
	/**
	* 静态部分：label maps 等只需计算一次的数据。
	*/
	const staticData = useMemo(() => {
		return {
			PAYMENT_STATUS_LABEL_MAP: getPaymentStatusMap(),
			PLATFORM_LABEL_MAP: getPlatformMap(),
			ORDER_STATUS_LABEL_MAP: getOrderStatusMap(),
			SHIPPING_STATUS_LABEL_MAP: getShippingStatusMap(),
			DELIVERY_TYPE_LABEL_MAP: getDeliveryTypeMap(),
			SHOP_SERVICE_TYPE_LABEL_MAP: getShopServiceTypeMap(),
			BUSINESS_LABEL_MAP: getBusinessLabelMap(),
			orderLabels: getOrderHeaderLabels(),
			amountLabels: {
				total: locales.getText("pisell2.orderList.common.totalAmount"),
				paid: locales.getText("pisell2.orderList.common.paidAmount"),
				due: locales.getText("pisell2.orderList.common.dueAmount"),
				refunded: locales.getText("pisell2.orderList.common.refundedAmount"),
				items: locales.getText("pisell2.orderList.common.itemsCount")
			}
		};
	}, [locale]);
	/**
	* 构建 columns 的工厂：接收 runtimeContext，使列渲染可访问 refresh 等运行时能力。
	*/
	const buildColumns = useCallback((ctx) => {
		const { PAYMENT_STATUS_LABEL_MAP, PLATFORM_LABEL_MAP, ORDER_STATUS_LABEL_MAP, SHIPPING_STATUS_LABEL_MAP, DELIVERY_TYPE_LABEL_MAP, SHOP_SERVICE_TYPE_LABEL_MAP, BUSINESS_LABEL_MAP, orderLabels, amountLabels } = staticData;
		return [
			{
				title: orderLabels.saleBrief,
				key: "sale_brief",
				width: 225,
				render: (_, record) => {
					const orderCode = record === null || record === void 0 ? void 0 : record.shop_full_order_number;
					const currencySymbol = record === null || record === void 0 ? void 0 : record.currency_symbol;
					const { totalAmount } = resolveOrderPaymentAmounts(record);
					const paymentStatus = record === null || record === void 0 ? void 0 : record.payment_status;
					const orderNote = record === null || record === void 0 ? void 0 : record.note;
					const internalNotes = record === null || record === void 0 ? void 0 : record.internal_note;
					const noteApis = createNoteApis((record === null || record === void 0 ? void 0 : record.id) || (record === null || record === void 0 ? void 0 : record.order_id) || (record === null || record === void 0 ? void 0 : record.orderId));
					const tags = (record === null || record === void 0 ? void 0 : record.tag) || (record === null || record === void 0 ? void 0 : record.tags) || [];
					const paymentLabel = PAYMENT_STATUS_LABEL_MAP[paymentStatus] || paymentStatus || "-";
					const paymentColor = PAYMENT_STATUS_COLOR_MAP[paymentStatus] || "";
					return /* @__PURE__ */ React.createElement("div", {
						className: "pisell-sales-grid-sale-brief",
						style: {
							display: "flex",
							flexDirection: "column"
						}
					}, /* @__PURE__ */ React.createElement("div", { style: {
						fontWeight: "bold",
						textDecoration: "underline",
						color: "var(--theme-color, #7f56d9)"
					} }, orderCode), Array.isArray(tags) && tags.length > 0 ? /* @__PURE__ */ React.createElement("div", { style: {
						display: "flex",
						gap: 4,
						flexWrap: "wrap"
					} }, tags.map((item) => /* @__PURE__ */ React.createElement(Tag$1, {
						key: `${item === null || item === void 0 ? void 0 : item.id}`,
						style: {
							margin: 0,
							maxWidth: 160,
							whiteSpace: "nowrap",
							overflow: "hidden",
							textOverflow: "ellipsis",
							fontWeight: 500
						}
					}, item === null || item === void 0 ? void 0 : item.name))) : null, /* @__PURE__ */ React.createElement("div", { style: { fontWeight: "bold" } }, /* @__PURE__ */ React.createElement("span", { style: { marginRight: 6 } }, amountLabels.total, ":"), /* @__PURE__ */ React.createElement("span", null, formatAmount$1(totalAmount, currencySymbol))), /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(Tag$1, { style: {
						backgroundColor: paymentColor,
						whiteSpace: "nowrap",
						border: "none",
						fontWeight: 500
					} }, paymentLabel)), /* @__PURE__ */ React.createElement(BookingNotesPreview, {
						dataSource: {
							note: orderNote,
							internalNotes,
							apis: noteApis,
							onChange: (value, type, noteType) => {
								ctx.refresh();
							}
						},
						labels: {
							orderNote: orderLabels.orderNote,
							internalNote: orderLabels.internalNote
						},
						style: { maxWidth: "100%" }
					}));
				}
			},
			{
				title: orderLabels.customer,
				key: "customer",
				align: "center",
				width: 150,
				uniformCompare: (value, record) => {
					const customer = record === null || record === void 0 ? void 0 : record.customer;
					if (!customer) return "-";
					const guestCode = customer === null || customer === void 0 ? void 0 : customer.guest_code;
					if (guestCode) return `${guestCode}`;
					if (customer.user_id === 1) return "walk-in";
					return customer;
				},
				render: (_, record) => {
					const customer = record === null || record === void 0 ? void 0 : record.customer;
					if (!customer) return "-";
					if (customer === null || customer === void 0 ? void 0 : customer.guest_code) return /* @__PURE__ */ React.createElement("div", { className: "pisell-sales-grid-person-cell" }, /* @__PURE__ */ React.createElement("div", { style: { fontWeight: "bold" } }, orderLabels.guest));
					const lines = getDisplayLines([formatPhone((customer === null || customer === void 0 ? void 0 : customer.phone) || (record === null || record === void 0 ? void 0 : record.phone), (customer === null || customer === void 0 ? void 0 : customer.country_calling_code) || (record === null || record === void 0 ? void 0 : record.country_calling_code)), customer === null || customer === void 0 ? void 0 : customer.email]);
					return /* @__PURE__ */ React.createElement("div", { className: "pisell-sales-grid-person-cell" }, /* @__PURE__ */ React.createElement(ClientName, {
						dataSource: customer,
						style: { fontSize: 14 }
					}), lines.map((line, index) => /* @__PURE__ */ React.createElement("div", {
						key: `${line}-${index}`,
						style: textStyles.text
					}, line)));
				}
			},
			{
				title: orderLabels.contact,
				dataIndex: "contacts_info",
				key: "contacts_info",
				align: "center",
				width: 150,
				uniformCompare: (value, record) => {
					if (!value) return "-";
					return value;
				},
				render: (value, record) => {
					const info = resolveOrderContactInfo(record, value);
					if (!(info === null || info === void 0 ? void 0 : info.displayName) && !(info === null || info === void 0 ? void 0 : info.phone) && !(info === null || info === void 0 ? void 0 : info.email)) return "-";
					const lines = getDisplayLines([formatPhone(info === null || info === void 0 ? void 0 : info.phone), info === null || info === void 0 ? void 0 : info.email]);
					return /* @__PURE__ */ React.createElement("div", { className: "pisell-sales-grid-person-cell" }, /* @__PURE__ */ React.createElement(ClientName, {
						dataSource: { display_name: info === null || info === void 0 ? void 0 : info.displayName },
						style: { fontSize: 14 }
					}), lines.map((line, index) => /* @__PURE__ */ React.createElement("div", {
						key: `${line}-${index}`,
						style: textStyles.text
					}, line)));
				}
			},
			{
				title: orderLabels.paymentInfo,
				key: "payment_info",
				width: 124,
				align: "center",
				render: (_, record) => {
					const value = record === null || record === void 0 ? void 0 : record.payment_status;
					const currencySymbol = record === null || record === void 0 ? void 0 : record.currency_symbol;
					const { totalAmount, paidAmount, refundedAmount, dueAmount } = resolveOrderPaymentAmounts(record);
					const amountStyles = {
						due: {
							fontWeight: "bold",
							color: "#D92D20"
						},
						refunded: {
							color: "#CA8504",
							fontWeight: "bold"
						}
					};
					const lines = [];
					if ([
						"payment_processing",
						"unpaid",
						"partially_paid"
					].includes(value)) {
						lines.push({
							label: amountLabels.due,
							value: formatAmount$1(dueAmount, currencySymbol),
							bold: true,
							kind: "due"
						});
						lines.push({
							label: amountLabels.total,
							value: formatAmount$1(totalAmount, currencySymbol),
							kind: "total"
						});
					} else if (value === "paid") {
						lines.push({
							label: amountLabels.paid,
							value: formatAmount$1(paidAmount, currencySymbol),
							bold: true,
							kind: "paid"
						});
						lines.push({
							label: amountLabels.total,
							value: formatAmount$1(totalAmount, currencySymbol),
							kind: "total"
						});
					} else if (["partially_refunded", "refunded"].includes(value)) {
						lines.push({
							label: amountLabels.refunded,
							value: formatAmount$1(refundedAmount, currencySymbol),
							bold: true,
							kind: "refunded"
						});
						lines.push({
							label: amountLabels.paid,
							value: formatAmount$1(paidAmount, currencySymbol),
							kind: "paid"
						});
						lines.push({
							label: amountLabels.total,
							value: formatAmount$1(totalAmount, currencySymbol),
							kind: "total"
						});
					} else if (["voided", "authorized"].includes(value)) lines.push({
						label: amountLabels.total,
						value: formatAmount$1(totalAmount, currencySymbol),
						bold: true,
						kind: "total"
					});
					else lines.push({
						label: amountLabels.total,
						value: formatAmount$1(totalAmount, currencySymbol),
						bold: true,
						kind: "total"
					});
					return /* @__PURE__ */ React.createElement("div", { style: {
						display: "flex",
						flexDirection: "column",
						gap: 4,
						alignItems: "flex-start"
					} }, lines.map((line) => /* @__PURE__ */ React.createElement("div", {
						key: line.label,
						style: textStyles.text
					}, /* @__PURE__ */ React.createElement("span", { style: _objectSpread2({ whiteSpace: "nowrap" }, line.kind && amountStyles[line.kind] ? amountStyles[line.kind] : {}) }, line.label, ": ", line.value))));
				}
			},
			{
				title: orderLabels.items,
				key: "product_quantity",
				align: "center",
				width: 120,
				render: (_, record) => {
					const count = resolveOrderItemsCount(record);
					return /* @__PURE__ */ React.createElement("span", { style: _objectSpread2(_objectSpread2({}, textStyles.text), {}, { textAlign: "center" }) }, count, amountLabels.items);
				}
			},
			{
				title: orderLabels.orderOrigin,
				key: "order_origin",
				width: 138,
				align: "center",
				render: (_, record) => {
					const channel = PLATFORM_LABEL_MAP[record === null || record === void 0 ? void 0 : record.order_sales_channel] || PLATFORM_LABEL_MAP[record === null || record === void 0 ? void 0 : record.order_platform] || (record === null || record === void 0 ? void 0 : record.order_sales_channel) || (record === null || record === void 0 ? void 0 : record.order_platform);
					const device = (record === null || record === void 0 ? void 0 : record.device_name) || (record === null || record === void 0 ? void 0 : record.device) || (record === null || record === void 0 ? void 0 : record.terminal);
					const time = (record === null || record === void 0 ? void 0 : record.created_at) || (record === null || record === void 0 ? void 0 : record.order_time);
					const actor = resolveOrderActorDisplay(record, record === null || record === void 0 ? void 0 : record.create_account);
					const lines = getDisplayLines([channel, device]);
					if (lines.length === 0) return null;
					const businessLabel = BUSINESS_LABEL_MAP[record.business_code];
					return /* @__PURE__ */ React.createElement("div", { style: {
						display: "flex",
						flexDirection: "column",
						alignItems: "flex-start"
					} }, businessLabel ? /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(Tag$1, {
						color: BUSINESS_COLOR_MAP[record.business_code],
						style: {
							color: "black",
							fontWeight: 500
						}
					}, businessLabel)) : null, lines.map((line, index) => /* @__PURE__ */ React.createElement("div", {
						key: `${line}-${index}`,
						style: textStyles.text
					}, line)), /* @__PURE__ */ React.createElement("div", { style: {
						display: "flex",
						alignItems: "center",
						whiteSpace: "nowrap"
					} }, /* @__PURE__ */ React.createElement("span", null, orderLabels.createdBy, ":"), actor ? /* @__PURE__ */ React.createElement(ClientName, {
						dataSource: actor,
						style: { fontSize: 14 }
					}) : /* @__PURE__ */ React.createElement("span", { style: _objectSpread2(_objectSpread2({}, textStyles.text), {}, { marginLeft: 4 }) }, "-")), /* @__PURE__ */ React.createElement(PisellDateTimeDisplay, {
						value: time,
						layout: "inline",
						textAlign: "left",
						timeFormat: { padHour: true },
						dateFormat: {
							showYear: false,
							shortMonth: true,
							showRelativeDay: true,
							useCommaBeforeYear: false
						},
						fields: { fieldOrder: [
							"time",
							"weekday",
							"date"
						] },
						style: { whiteSpace: "nowrap" }
					}));
				}
			},
			{
				title: orderLabels.paymentStatus,
				dataIndex: "payment_status",
				key: "payment_status",
				align: "center",
				width: 160,
				render: (value) => {
					const key = value === "partially_paid" ? "unpaid" : value;
					const label = PAYMENT_STATUS_LABEL_MAP[key] || value || "-";
					const color = PAYMENT_STATUS_COLOR_MAP[key] || "";
					return /* @__PURE__ */ React.createElement("div", { style: { textAlign: "center" } }, /* @__PURE__ */ React.createElement(PisellTags, { style: {
						backgroundColor: color,
						whiteSpace: "nowrap"
					} }, label));
				}
			},
			{
				title: orderLabels.shippingMethod,
				key: "delivery_type",
				align: "center",
				width: 160,
				render: (_, record) => {
					const method = resolveShippingMethodDisplay(record, {
						shopServiceTypeLabelMap: SHOP_SERVICE_TYPE_LABEL_MAP,
						deliveryTypeLabelMap: DELIVERY_TYPE_LABEL_MAP
					});
					if (!method) return "-";
					return /* @__PURE__ */ React.createElement("div", { style: { textAlign: "center" } }, /* @__PURE__ */ React.createElement(Tag$1, {
						style: {
							whiteSpace: "nowrap",
							fontWeight: 500
						},
						color: method.color
					}, method.label));
				},
				uniformCompare: (_, record) => {
					var _record$metadata2;
					return `${(record === null || record === void 0 ? void 0 : record.delivery_type) || ""}|${(record === null || record === void 0 || (_record$metadata2 = record.metadata) === null || _record$metadata2 === void 0 || (_record$metadata2 = _record$metadata2.shop_service_type) === null || _record$metadata2 === void 0 ? void 0 : _record$metadata2.server_type) || ""}`;
				}
			},
			{
				title: orderLabels.orderStatus,
				dataIndex: "status",
				key: "status",
				align: "center",
				width: 150,
				render: (value) => {
					const color = ORDER_STATUS_COLOR_MAP[value];
					const label = ORDER_STATUS_LABEL_MAP[value] || value || "-";
					return /* @__PURE__ */ React.createElement("div", { style: { textAlign: "center" } }, /* @__PURE__ */ React.createElement(PisellTags, { style: {
						whiteSpace: "nowrap",
						borderColor: color === null || color === void 0 ? void 0 : color.borderColor,
						color: color === null || color === void 0 ? void 0 : color.textColor
					} }, label));
				}
			},
			{
				title: orderLabels.fulfillmentStatus,
				dataIndex: "shipping_status",
				key: "shipping_status",
				align: "center",
				width: 160,
				render: (value) => {
					const color = FULFILLMENT_STATUS_COLOR_MAP[value];
					const label = SHIPPING_STATUS_LABEL_MAP[value] || value || "-";
					return /* @__PURE__ */ React.createElement("div", { style: { textAlign: "center" } }, /* @__PURE__ */ React.createElement(PisellTags, { style: {
						whiteSpace: "nowrap",
						borderColor: color === null || color === void 0 ? void 0 : color.borderColor,
						color: color === null || color === void 0 ? void 0 : color.textColor
					} }, label));
				}
			},
			{
				title: orderLabels.updated,
				key: "last_edit_account",
				width: 132,
				align: "center",
				render: (_, record) => {
					var _record$last_edit_acc;
					const editor = resolveOrderActorDisplay(record, record === null || record === void 0 ? void 0 : record.last_edit_account);
					const time = (record === null || record === void 0 || (_record$last_edit_acc = record.last_edit_account) === null || _record$last_edit_acc === void 0 ? void 0 : _record$last_edit_acc.created_at) || (editor === null || editor === void 0 ? void 0 : editor.updated_at) || (record === null || record === void 0 ? void 0 : record.updated_at);
					const lines = getDisplayLines([PLATFORM_LABEL_MAP[record === null || record === void 0 ? void 0 : record.order_platform] || (record === null || record === void 0 ? void 0 : record.order_platform)]);
					return /* @__PURE__ */ React.createElement("div", { style: {
						display: "flex",
						flexDirection: "column",
						alignItems: "flex-start"
					} }, editor ? /* @__PURE__ */ React.createElement(ClientName, {
						dataSource: editor,
						style: { fontSize: 14 }
					}) : /* @__PURE__ */ React.createElement("div", { style: _objectSpread2({}, textStyles.text) }, "-"), /* @__PURE__ */ React.createElement(PisellDateTimeDisplay, {
						value: time,
						layout: "inline",
						textAlign: "left",
						timeFormat: { padHour: true },
						dateFormat: {
							showYear: false,
							shortMonth: true,
							showRelativeDay: true,
							useCommaBeforeYear: false
						},
						fields: { fieldOrder: [
							"time",
							"weekday",
							"date"
						] },
						style: { whiteSpace: "nowrap" }
					}), lines.map((line, index) => /* @__PURE__ */ React.createElement("div", {
						key: `${line}-${index}`,
						style: textStyles.text
					}, line)));
				}
			},
			{
				title: orderLabels.orderId,
				dataIndex: "order_id",
				key: "order_id",
				align: "center",
				width: 100,
				render: (_value, record) => {
					const orderId = record === null || record === void 0 ? void 0 : record.shop_order_number;
					return /* @__PURE__ */ React.createElement("span", { style: _objectSpread2(_objectSpread2({}, textStyles.text), {}, { textAlign: "center" }) }, orderId);
				}
			}
		];
	}, [staticData]);
	return useMemo(() => ({
		key: "handle",
		label: "Handle",
		getChildComponentProps: (ctx) => ({
			grid: {
				bordered: true,
				collapseUniformColumns: true,
				columns: buildColumns(ctx),
				defaultHiddenColumnKeys: [
					"order_origin",
					"payment_status",
					"delivery_type",
					"status",
					"shipping_status",
					"last_edit_account",
					"id"
				],
				scroll: {
					x: "max-content",
					autoCalc: true,
					scrollToFirstRowOnChange: true
				},
				rowSelection: {
					fixed: true,
					columnWidth: 48
				},
				onRow: (record) => ({ onClick: () => {
					var _ref4, _record$order_id, _app$history, _app$history2;
					const orderId = (_ref4 = (_record$order_id = record === null || record === void 0 ? void 0 : record.order_id) !== null && _record$order_id !== void 0 ? _record$order_id : record === null || record === void 0 ? void 0 : record.orderId) !== null && _ref4 !== void 0 ? _ref4 : record === null || record === void 0 ? void 0 : record.id;
					const nativePage = NativePage === null || NativePage === void 0 ? void 0 : NativePage();
					if ((nativePage === null || nativePage === void 0 ? void 0 : nativePage.open) && orderId != null) {
						nativePage.open({
							page: "/native/order/detail",
							params: { order_id: orderId }
						});
						return;
					}
					const pathname = encodeURIComponent((app === null || app === void 0 || (_app$history = app.history) === null || _app$history === void 0 || (_app$history = _app$history.instance) === null || _app$history === void 0 || (_app$history = _app$history.location) === null || _app$history === void 0 ? void 0 : _app$history.pathname) || "");
					app === null || app === void 0 || (_app$history2 = app.history) === null || _app$history2 === void 0 || _app$history2.push(`/order-detail?id=${record.id}&goback=${pathname}`);
				} })
			},
			toolBar: {
				search: { showConfirmButton: false },
				sort: { list: getSortList() },
				quickFilter: { filterList: [{
					type: "rangePicker",
					name: "orderDate",
					key: "orderDate",
					props: { placeholder: locales.getText("pisell2.orderList.quickFilter.orderDate.placeholder") }
				}] },
				filter: {
					hiddenQuickFilter: true,
					defaultFilterValue: {
						quickFilter: [],
						otherFilter: getFilterButtonList(),
						values: {}
					}
				}
			},
			batchActionBar: {
				overflowThreshold: 2,
				defaultActions: {
					showSelectAll: false,
					showClear: false,
					showInvert: false,
					showDelete: false
				}
			}
		})
	}), [
		buildColumns,
		app,
		locale,
		NativePage
	]);
};
//#endregion
export { useOrderPerspective };
