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_CREATE, mergeMutableFixture } from "../fixtures.js";
import { RawSnapshotPanel } from "./RawSnapshotPanel.js";
import { DemoAdvancedConfig } from "./DemoAdvancedConfig.js";
import { CartPanel } from "./CartPanel.js";
import { DemoStepRunner } from "./DemoStepRunner.js";
import { buildCreateSteps } from "./buildSteps.js";
import React, { useMemo, useState } from "react";
import { useMemoizedFn } from "ahooks";
//#region src/plus/salesSdk/demo/components/DemoTabCreate.tsx
/**
* 新建 Tab：一键跑「createNew → loadCustomers → selectCustomer → loadProducts → addProductWithFlow
*          → recalcSummary → submit」全流程。
*
* 每步可单独执行，便于排查具体哪一步抛错；步骤间通过闭包传递上一步挑出的客户 / 商品引用。
* 真实接口（submit / addProduct）的副作用提示由顶部 banner 给出。
*/
const DemoTabCreate = () => {
	const sales = useSalesSdk();
	const customer = useSalesSdkCustomer();
	const products = useSalesSdkProducts();
	const cart = useSalesSdkCart();
	const summaryState = useSalesSdkSummary();
	const [override, setOverride] = useState({});
	const [editBookingError, setEditBookingError] = useState(null);
	const fixture = useMemo(() => mergeMutableFixture(FIXTURE_CREATE, override), [override]);
	const steps = useMemo(() => buildCreateSteps({
		sales,
		customer,
		products,
		cart
	}, fixture), [
		sales,
		customer,
		products,
		cart,
		fixture
	]);
	/** 清空 OS 空单 + 刷新 cart/summary 快照，避免 createNew 复用上一轮的 tempOrder / localStorage */
	const handleDemoReset = useMemoizedFn(_asyncToGenerator(function* () {
		yield sales.restore();
		yield cart.recalcSummary();
	}));
	deriveSummary(summaryState.summary);
	return /* @__PURE__ */ React.createElement("section", { className: "sales-sdk-demo__tab" }, /* @__PURE__ */ React.createElement("header", { className: "sales-sdk-demo__panel sales-sdk-demo__panel--warn" }, /* @__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" }, "注意：此 Tab 会调用真实 OS 接口（createNew / addProductWithFlow / submit）。加车若需规格会打开 Info2 弹窗。submit 会弹二次确认，其它步骤直接执行。")), /* @__PURE__ */ React.createElement(DemoAdvancedConfig, {
		variant: "create",
		value: override,
		onChange: setOverride
	}), /* @__PURE__ */ React.createElement(DemoStepRunner, {
		title: "步骤序列（点一键串行执行 / 也可单步）",
		steps,
		onReset: handleDemoReset
	}), 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(CartPanel, {
		cart,
		onEditBookingError: (err) => {
			var _message;
			return setEditBookingError(String((_message = err === null || err === void 0 ? void 0 : err.message) !== null && _message !== void 0 ? _message : err));
		}
	}), editBookingError ? /* @__PURE__ */ React.createElement("section", { className: "sales-sdk-demo__panel sales-sdk-demo__panel--error" }, /* @__PURE__ */ React.createElement("h3", null, "编辑资源失败"), /* @__PURE__ */ React.createElement("pre", null, editBookingError)) : null, /* @__PURE__ */ React.createElement(RawSnapshotPanel, {
		sales,
		cart,
		customer,
		summary: summaryState.summary,
		products: products.products
	}));
};
//#endregion
export { DemoTabCreate };
