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 { deriveSummary } from "../../selectors.js";
import { useSalesSdkCart } from "../../hooks/useSalesSdkCart.js";
import { useSalesSdk } from "../../hooks/useSalesSdk.js";
import { useSalesSdkCustomer } from "../../hooks/useSalesSdkCustomer.js";
import { useSalesSdkSummary } from "../../hooks/useSalesSdkSummary.js";
import { useSalesSdkProducts } from "../../hooks/useSalesSdkProducts.js";
import { FIXTURE_DETAIL, mergeDetailFixture } from "../fixtures.js";
import { RawSnapshotPanel } from "./RawSnapshotPanel.js";
import { ActionLog } from "./ActionLog.js";
import { DemoAdvancedConfig } from "./DemoAdvancedConfig.js";
import React, { useMemo, useState } from "react";
//#region src/plus/salesSdk/demo/components/DemoTabDetail.tsx
/**
* 详情 Tab：只读展示场景。
*
* 流程：点「一键查看详情」→ sales.loadDetail(orderId) → 四个面板自动渲染服务端详情。
* 不暴露 createNew / addProduct / submit 等写操作；如需调试请到 新建 / 编辑 Tab。
*/
const DemoTabDetail = ({ initialOrderId }) => {
	const sales = useSalesSdk();
	const customer = useSalesSdkCustomer();
	const cart = useSalesSdkCart();
	const summaryState = useSalesSdkSummary();
	const products = useSalesSdkProducts();
	const initialFixture = useMemo(() => {
		return _objectSpread2(_objectSpread2({}, FIXTURE_DETAIL), initialOrderId ? { orderId: initialOrderId } : {});
	}, [initialOrderId]);
	const [override, setOverride] = useState({});
	const fixture = useMemo(() => mergeDetailFixture(initialFixture, override), [initialFixture, override]);
	const [entries, setEntries] = useState([]);
	const [loading, setLoading] = useState(false);
	const [bookingStatus, setBookingStatusInput] = useState("confirmed");
	const [statusLoading, setStatusLoading] = useState(false);
	deriveSummary(summaryState.summary);
	const append = (entry) => {
		setEntries((prev) => [...prev, entry]);
	};
	const runLoadDetail = function() {
		var _ref = _asyncToGenerator(function* () {
			if (!Number.isFinite(fixture.orderId) || fixture.orderId <= 0) {
				append({
					status: "error",
					message: "orderId 必须为正整数"
				});
				return;
			}
			setLoading(true);
			append({
				status: "running",
				message: `loadDetail(${fixture.orderId}) running...`
			});
			try {
				const result = yield sales.loadDetail(fixture.orderId);
				append({
					status: "success",
					message: `loadDetail(${fixture.orderId}) success\n${safeJson(result)}`
				});
			} catch (e) {
				var _e$message;
				append({
					status: "error",
					message: `loadDetail(${fixture.orderId}) failed\n${String((_e$message = e === null || e === void 0 ? void 0 : e.message) !== null && _e$message !== void 0 ? _e$message : e)}`
				});
			} finally {
				setLoading(false);
			}
		});
		return function runLoadDetail() {
			return _ref.apply(this, arguments);
		};
	}();
	/**
	* 调用 sales.setBookingStatus(status) 变更当前订单的预约状态。
	* 内部由 OS BookingTicket 解析 schedule id（metadata.appointment_booking_parent_schedule_id 优先，
	* 否则取 root booking.schedule_event_id），并自动刷新销售详情。
	* 需先 loadDetail 让 tempOrder 就绪，否则 OS 会抛 "tempOrder 未加载"。
	*/
	const runSetBookingStatus = function() {
		var _ref2 = _asyncToGenerator(function* () {
			const next = bookingStatus.trim();
			if (!next) {
				append({
					status: "error",
					message: "appointment_status 不能为空"
				});
				return;
			}
			setStatusLoading(true);
			append({
				status: "running",
				message: `setBookingStatus(${next}) running...`
			});
			try {
				append({
					status: "success",
					message: `setBookingStatus(${next}) success\n${safeJson(yield sales.setBookingStatus(next))}`
				});
			} catch (e) {
				var _e$message2;
				append({
					status: "error",
					message: `setBookingStatus(${next}) failed\n${String((_e$message2 = e === null || e === void 0 ? void 0 : e.message) !== null && _e$message2 !== void 0 ? _e$message2 : e)}`
				});
			} finally {
				setStatusLoading(false);
			}
		});
		return function runSetBookingStatus() {
			return _ref2.apply(this, arguments);
		};
	}();
	/**
	* 结账：直接调用 SDK 的 openCheckout，
	* moduleKey 与错误处理都收敛在 SalesSdkProvider 内部，组件层只负责日志记录。
	*/
	const handleCheckout = function() {
		var _ref3 = _asyncToGenerator(function* () {
			append({
				status: "running",
				message: "openCheckout running..."
			});
			try {
				append({
					status: "success",
					message: `openCheckout success\n${safeJson(yield sales.openCheckout())}`
				});
			} catch (e) {
				var _e$message3;
				append({
					status: "error",
					message: `openCheckout failed\n${String((_e$message3 = e === null || e === void 0 ? void 0 : e.message) !== null && _e$message3 !== void 0 ? _e$message3 : e)}`
				});
			}
		});
		return function handleCheckout() {
			return _ref3.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ React.createElement("section", { className: "sales-sdk-demo__tab" }, /* @__PURE__ */ React.createElement("header", { className: "sales-sdk-demo__panel" }, /* @__PURE__ */ React.createElement("div", { className: "sales-sdk-demo__section-title" }, /* @__PURE__ */ React.createElement("h3", null, "详情展示"), /* @__PURE__ */ React.createElement("span", null, "status: ", sales.status, " / ready: ", sales.isReady ? "yes" : "no")), /* @__PURE__ */ React.createElement("p", { className: "sales-sdk-demo__hint" }, "只读场景：调用 ", /* @__PURE__ */ React.createElement("code", null, "sales.loadDetail(orderId)"), " 拉取服务端订单详情后展示客户 / 购物车 / 汇总。"), /* @__PURE__ */ React.createElement("div", { className: "sales-sdk-demo__toolbar" }, /* @__PURE__ */ React.createElement("button", {
		type: "button",
		className: "sales-sdk-demo__primary",
		disabled: loading,
		onClick: runLoadDetail
	}, "一键查看详情（orderId = ", fixture.orderId, "）"), /* @__PURE__ */ React.createElement("button", {
		type: "button",
		className: "sales-sdk-demo__primary",
		disabled: loading,
		onClick: handleCheckout
	}, "结账（orderId = ", fixture.orderId, "）")), /* @__PURE__ */ React.createElement("div", {
		className: "sales-sdk-demo__toolbar",
		style: {
			marginTop: 8,
			gap: 8,
			alignItems: "center"
		}
	}, /* @__PURE__ */ React.createElement("span", { style: {
		fontSize: 12,
		color: "#666"
	} }, "变更预约状态："), /* @__PURE__ */ React.createElement("select", {
		value: bookingStatus,
		onChange: (e) => setBookingStatusInput(e.target.value),
		disabled: statusLoading || !sales.isReady
	}, /* @__PURE__ */ React.createElement("option", { value: "confirmed" }, "confirmed"), /* @__PURE__ */ React.createElement("option", { value: "pending" }, "pending"), /* @__PURE__ */ React.createElement("option", { value: "checked_in" }, "checked_in"), /* @__PURE__ */ React.createElement("option", { value: "completed" }, "completed"), /* @__PURE__ */ React.createElement("option", { value: "cancelled" }, "cancelled"), /* @__PURE__ */ React.createElement("option", { value: "no_show" }, "no_show")), /* @__PURE__ */ React.createElement("button", {
		type: "button",
		className: "sales-sdk-demo__primary",
		disabled: statusLoading || !sales.isReady,
		onClick: runSetBookingStatus,
		title: !sales.isReady ? "请先点「一键查看详情」加载订单" : void 0
	}, statusLoading ? "提交中..." : `setBookingStatus(${bookingStatus})`))), /* @__PURE__ */ React.createElement(DemoAdvancedConfig, {
		variant: "detail",
		value: override,
		onChange: setOverride
	}), sales.error ? /* @__PURE__ */ React.createElement("section", { className: "sales-sdk-demo__panel sales-sdk-demo__panel--error" }, /* @__PURE__ */ React.createElement("h3", null, "SDK Error"), /* @__PURE__ */ React.createElement("pre", null, sales.error.message)) : null, /* @__PURE__ */ React.createElement(RawSnapshotPanel, {
		sales,
		cart,
		customer,
		summary: summaryState.summary,
		products: products.products
	}), /* @__PURE__ */ React.createElement(ActionLog, { entries }));
};
function safeJson(value) {
	try {
		return JSON.stringify(value, null, 2);
	} catch (_unused) {
		return String(value);
	}
}
//#endregion
export { DemoTabDetail };
