const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_index = require("../../clientName/index.js");
const require_order = require("../config/order.js");
const require_serve = require("../serve.js");
const require_index$1 = require("../config/index.js");
const require_perspectiveUtils = require("./perspectiveUtils.js");
const require_BookingNotesPreview = require("../components/BookingNotesPreview.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
//#region src/plus/pisellSalesManagement/hooks/useOrderPerspective.tsx
const useOrderPerspective = () => {
	var _context$appHelper, _context$appHelper$ge, _context$appHelper2, _context$engine;
	const context = require_useEngineContext.default();
	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 = (0, react.useMemo)(() => {
		return {
			PAYMENT_STATUS_LABEL_MAP: require_order.getPaymentStatusMap(),
			PLATFORM_LABEL_MAP: require_order.getPlatformMap(),
			ORDER_STATUS_LABEL_MAP: require_order.getOrderStatusMap(),
			SHIPPING_STATUS_LABEL_MAP: require_order.getShippingStatusMap(),
			DELIVERY_TYPE_LABEL_MAP: require_order.getDeliveryTypeMap(),
			SHOP_SERVICE_TYPE_LABEL_MAP: require_order.getShopServiceTypeMap(),
			BUSINESS_LABEL_MAP: require_index$1.getBusinessLabelMap(),
			orderLabels: require_order.getOrderHeaderLabels(),
			amountLabels: {
				total: _pisell_utils.locales.getText("pisell2.orderList.common.totalAmount"),
				paid: _pisell_utils.locales.getText("pisell2.orderList.common.paidAmount"),
				due: _pisell_utils.locales.getText("pisell2.orderList.common.dueAmount"),
				refunded: _pisell_utils.locales.getText("pisell2.orderList.common.refundedAmount"),
				items: _pisell_utils.locales.getText("pisell2.orderList.common.itemsCount")
			}
		};
	}, [locale]);
	/**
	* 构建 columns 的工厂：接收 runtimeContext，使列渲染可访问 refresh 等运行时能力。
	*/
	const buildColumns = (0, react.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 = record === null || record === void 0 ? void 0 : record.total_amount;
					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 = require_serve.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 = require_order.PAYMENT_STATUS_COLOR_MAP[paymentStatus] || "";
					return /* @__PURE__ */ react.default.createElement("div", { style: {
						display: "flex",
						flexDirection: "column"
					} }, /* @__PURE__ */ react.default.createElement("div", { style: {
						fontWeight: "bold",
						textDecoration: "underline",
						color: "var(--theme-color, #7f56d9)"
					} }, orderCode), Array.isArray(tags) && tags.length > 0 ? /* @__PURE__ */ react.default.createElement("div", { style: {
						display: "flex",
						gap: 4,
						flexWrap: "wrap"
					} }, tags.map((item) => /* @__PURE__ */ react.default.createElement(require_perspectiveUtils.Tag, {
						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.default.createElement("div", { style: { fontWeight: "bold" } }, /* @__PURE__ */ react.default.createElement("span", { style: { marginRight: 6 } }, amountLabels.total, ":"), /* @__PURE__ */ react.default.createElement("span", null, require_perspectiveUtils.formatAmount(totalAmount, currencySymbol))), /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement(require_perspectiveUtils.Tag, { style: {
						backgroundColor: paymentColor,
						whiteSpace: "nowrap",
						border: "none",
						fontWeight: 500
					} }, paymentLabel)), /* @__PURE__ */ react.default.createElement(require_BookingNotesPreview.default, {
						dataSource: {
							note: orderNote,
							internalNotes,
							apis: noteApis,
							onChange: (value, type, noteType) => {
								ctx.refresh();
							}
						},
						labels: {
							orderNote: orderLabels.orderNote,
							internalNote: orderLabels.internalNote
						},
						style: { maxWidth: 160 }
					}));
				}
			},
			{
				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.default.createElement("div", { style: {
						display: "flex",
						flexDirection: "column",
						alignItems: "center",
						textAlign: "center"
					} }, /* @__PURE__ */ react.default.createElement("div", { style: { fontWeight: "bold" } }, orderLabels.guest));
					const lines = require_perspectiveUtils.getDisplayLines([require_perspectiveUtils.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.default.createElement("div", { style: {
						display: "flex",
						flexDirection: "column",
						alignItems: "center",
						textAlign: "center"
					} }, /* @__PURE__ */ react.default.createElement(require_index.default, {
						dataSource: customer,
						style: { fontSize: 14 }
					}), lines.map((line, index) => /* @__PURE__ */ react.default.createElement("div", {
						key: `${line}-${index}`,
						style: require_perspectiveUtils.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) => {
					if (!value) return "-";
					const info = (value === null || value === void 0 ? void 0 : value.value) || {};
					const lines = require_perspectiveUtils.getDisplayLines([require_perspectiveUtils.formatPhone(info === null || info === void 0 ? void 0 : info.phone), info === null || info === void 0 ? void 0 : info.email]);
					return /* @__PURE__ */ react.default.createElement("div", { style: {
						display: "flex",
						flexDirection: "column",
						alignItems: "center",
						textAlign: "center"
					} }, /* @__PURE__ */ react.default.createElement(require_index.default, {
						dataSource: info,
						style: { fontSize: 14 }
					}), lines.map((line, index) => /* @__PURE__ */ react.default.createElement("div", {
						key: `${line}-${index}`,
						style: require_perspectiveUtils.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 = Number((record === null || record === void 0 ? void 0 : record.total_amount) || 0);
					const paidAmount = Number((record === null || record === void 0 ? void 0 : record.paid_amount) || 0);
					const refundedAmount = Number((record === null || record === void 0 ? void 0 : record.total_refund_amount) || 0);
					const dueAmount = totalAmount - paidAmount;
					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: require_perspectiveUtils.formatAmount(dueAmount, currencySymbol),
							bold: true,
							kind: "due"
						});
						lines.push({
							label: amountLabels.total,
							value: require_perspectiveUtils.formatAmount(totalAmount, currencySymbol),
							kind: "total"
						});
					} else if (value === "paid") {
						lines.push({
							label: amountLabels.paid,
							value: require_perspectiveUtils.formatAmount(paidAmount, currencySymbol),
							bold: true,
							kind: "paid"
						});
						lines.push({
							label: amountLabels.total,
							value: require_perspectiveUtils.formatAmount(totalAmount, currencySymbol),
							kind: "total"
						});
					} else if (["partially_refunded", "refunded"].includes(value)) {
						lines.push({
							label: amountLabels.refunded,
							value: require_perspectiveUtils.formatAmount(refundedAmount, currencySymbol),
							bold: true,
							kind: "refunded"
						});
						lines.push({
							label: amountLabels.paid,
							value: require_perspectiveUtils.formatAmount(paidAmount, currencySymbol),
							kind: "paid"
						});
						lines.push({
							label: amountLabels.total,
							value: require_perspectiveUtils.formatAmount(totalAmount, currencySymbol),
							kind: "total"
						});
					} else if (["voided", "authorized"].includes(value)) lines.push({
						label: amountLabels.total,
						value: require_perspectiveUtils.formatAmount(totalAmount, currencySymbol),
						bold: true,
						kind: "total"
					});
					else lines.push({
						label: amountLabels.total,
						value: require_perspectiveUtils.formatAmount(totalAmount, currencySymbol),
						bold: true,
						kind: "total"
					});
					return /* @__PURE__ */ react.default.createElement("div", { style: {
						display: "flex",
						flexDirection: "column",
						gap: 4,
						alignItems: "flex-start"
					} }, lines.map((line) => /* @__PURE__ */ react.default.createElement("div", {
						key: line.label,
						style: require_perspectiveUtils.textStyles.text
					}, /* @__PURE__ */ react.default.createElement("span", { style: require_objectSpread2._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 = (record === null || record === void 0 ? void 0 : record.product_quantity) || 0;
					return /* @__PURE__ */ react.default.createElement("span", { style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, require_perspectiveUtils.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 lines = require_perspectiveUtils.getDisplayLines([channel, device]);
					if (lines.length === 0) return null;
					const businessLabel = BUSINESS_LABEL_MAP[record.business_code];
					return /* @__PURE__ */ react.default.createElement("div", { style: {
						display: "flex",
						flexDirection: "column",
						alignItems: "flex-start"
					} }, businessLabel ? /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement(require_perspectiveUtils.Tag, {
						color: require_index$1.BUSINESS_COLOR_MAP[record.business_code],
						style: {
							color: "black",
							fontWeight: 500
						}
					}, businessLabel)) : null, lines.map((line, index) => /* @__PURE__ */ react.default.createElement("div", {
						key: `${line}-${index}`,
						style: require_perspectiveUtils.textStyles.text
					}, line)), /* @__PURE__ */ react.default.createElement("div", { style: {
						display: "flex",
						alignItems: "center",
						whiteSpace: "nowrap"
					} }, /* @__PURE__ */ react.default.createElement("span", null, orderLabels.createdBy, ":"), /* @__PURE__ */ react.default.createElement(require_index.default, {
						dataSource: record.create_account,
						style: { fontSize: 14 }
					})), /* @__PURE__ */ react.default.createElement(_pisell_materials.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 = require_order.PAYMENT_STATUS_COLOR_MAP[key] || "";
					return /* @__PURE__ */ react.default.createElement("div", { style: { textAlign: "center" } }, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellTags, { style: {
						backgroundColor: color,
						whiteSpace: "nowrap"
					} }, label));
				}
			},
			{
				title: orderLabels.shippingMethod,
				key: "delivery_type",
				align: "center",
				width: 160,
				render: (_, record) => {
					var _record$metadata;
					const deliveryType = record === null || record === void 0 ? void 0 : record.delivery_type;
					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;
					if (!deliveryType && !serverType) return "-";
					const isShopService = deliveryType === "shop_service";
					const label = isShopService ? SHOP_SERVICE_TYPE_LABEL_MAP[serverType] : DELIVERY_TYPE_LABEL_MAP[deliveryType];
					if (!label || !isShopService && deliveryType === "nil") return "-";
					const color = isShopService ? require_order.SHOP_SERVICE_TYPE_COLOR_MAP[serverType] : require_order.DELIVERY_TYPE_COLOR_MAP[deliveryType];
					return /* @__PURE__ */ react.default.createElement("div", { style: { textAlign: "center" } }, /* @__PURE__ */ react.default.createElement(require_perspectiveUtils.Tag, {
						style: {
							whiteSpace: "nowrap",
							fontWeight: 500
						},
						color
					}, 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 = require_order.ORDER_STATUS_COLOR_MAP[value];
					const label = ORDER_STATUS_LABEL_MAP[value] || value || "-";
					return /* @__PURE__ */ react.default.createElement("div", { style: { textAlign: "center" } }, /* @__PURE__ */ react.default.createElement(_pisell_materials.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 = require_order.FULFILLMENT_STATUS_COLOR_MAP[value];
					const label = SHIPPING_STATUS_LABEL_MAP[value] || value || "-";
					return /* @__PURE__ */ react.default.createElement("div", { style: { textAlign: "center" } }, /* @__PURE__ */ react.default.createElement(_pisell_materials.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) => {
					const editor = record === null || record === void 0 ? void 0 : record.last_edit_account;
					const time = (editor === null || editor === void 0 ? void 0 : editor.created_at) || (record === null || record === void 0 ? void 0 : record.updated_at);
					const lines = require_perspectiveUtils.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.default.createElement("div", { style: {
						display: "flex",
						flexDirection: "column",
						alignItems: "flex-start"
					} }, /* @__PURE__ */ react.default.createElement(require_index.default, {
						dataSource: editor || record.create_account,
						style: { fontSize: 14 }
					}), /* @__PURE__ */ react.default.createElement(_pisell_materials.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.default.createElement("div", {
						key: `${line}-${index}`,
						style: require_perspectiveUtils.textStyles.text
					}, line)));
				}
			},
			{
				title: orderLabels.orderId,
				dataIndex: "id",
				key: "id",
				align: "center",
				width: 100,
				render: (value) => /* @__PURE__ */ react.default.createElement("span", { style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, require_perspectiveUtils.textStyles.text), {}, { textAlign: "center" }) }, value)
			}
		];
	}, [staticData]);
	return (0, react.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 _ref, _record$order_id, _app$history, _app$history2;
					const orderId = (_ref = (_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 && _ref !== void 0 ? _ref : 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: require_order.getSortList() },
				quickFilter: { filterList: [{
					type: "rangePicker",
					name: "orderDate",
					key: "orderDate",
					props: { placeholder: _pisell_utils.locales.getText("pisell2.orderList.quickFilter.orderDate.label") }
				}] },
				filter: {
					hiddenQuickFilter: true,
					defaultFilterValue: {
						quickFilter: [],
						otherFilter: require_order.getFilterButtonList(),
						values: {}
					}
				}
			},
			batchActionBar: {
				overflowThreshold: 2,
				defaultActions: {
					showSelectAll: false,
					showClear: false,
					showInvert: false,
					showDelete: false
				}
			}
		})
	}), [
		buildColumns,
		app,
		locale,
		NativePage
	]);
};
//#endregion
exports.useOrderPerspective = useOrderPerspective;
