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_EDIT, 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 { buildEditSteps } from "./buildSteps.js";
import React, { useMemo, useState } from "react";
import { useMemoizedFn } from "ahooks";
//#region src/plus/salesSdk/demo/components/DemoTabEdit.tsx
/**
* 编辑 Tab：一键跑「loadDetail → setOrderNote → setShopDiscount → setContactsInfo
*          → loadCustomers → selectCustomer → loadProducts → addProductWithFlow
*          → recalcSummary → submit」全流程。
*
* 与新建 Tab 的差异：首步是 sales.loadDetail(orderId) 拉一份既有订单，后续在该 tempOrder
* 之上追加客户 / 商品 / submit。
*/
const DemoTabEdit = ({ initialOrderId }) => {
	const sales = useSalesSdk();
	const customer = useSalesSdkCustomer();
	const products = useSalesSdkProducts();
	const cart = useSalesSdkCart();
	const summaryState = useSalesSdkSummary();
	const initialFixture = useMemo(() => _objectSpread2(_objectSpread2({}, FIXTURE_EDIT), initialOrderId ? { orderId: initialOrderId } : {}), [initialOrderId]);
	const [override, setOverride] = useState({});
	const fixture = useMemo(() => {
		return _objectSpread2(_objectSpread2({}, mergeMutableFixture(initialFixture, override)), {}, { orderId: override.orderId != null ? Number(override.orderId) : initialFixture.orderId });
	}, [initialFixture, override]);
	const steps = useMemo(() => buildEditSteps({
		sales,
		customer,
		products,
		cart
	}, fixture), [
		sales,
		customer,
		products,
		cart,
		fixture
	]);
	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, "编辑订单一键流程（orderId = ", fixture.orderId, "）"), /* @__PURE__ */ React.createElement("span", null, "status: ", sales.status, " / ready: ", sales.isReady ? "yes" : "no")), /* @__PURE__ */ React.createElement("p", { className: "sales-sdk-demo__hint" }, "注意：此 Tab 会先 loadDetail 加载既有订单，再写入备注 / 整单优惠 / 联系人，最后追加客户 / 商品 / submit，均为真实 OS 接口。")), /* @__PURE__ */ React.createElement(DemoAdvancedConfig, {
		variant: "edit",
		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 }), /* @__PURE__ */ React.createElement(RawSnapshotPanel, {
		sales,
		cart,
		customer,
		summary: summaryState.summary,
		products: products.products
	}));
};
//#endregion
export { DemoTabEdit };
