const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
//#region src/plus/salesSdk/demo/components/DemoAdvancedConfig.tsx
const _excluded = ["orderNote"], _excluded2 = ["orderNote"], _excluded3 = ["shopDiscount"], _excluded4 = ["contactsInfo"];
function parseNumberOrUndefined(raw) {
	const t = raw.trim();
	if (!t) return void 0;
	const n = Number(t);
	return Number.isFinite(n) ? n : void 0;
}
function parseMenuListIds(raw) {
	const t = raw.trim();
	if (!t) return void 0;
	return t.split(/[,\s]+/).map((s) => Number(s)).filter((n) => Number.isFinite(n));
}
function FieldText({ label, value, placeholder, onChange }) {
	return /* @__PURE__ */ react.default.createElement("label", { className: "sales-sdk-demo__field" }, /* @__PURE__ */ react.default.createElement("span", null, label), /* @__PURE__ */ react.default.createElement("input", {
		value,
		placeholder,
		onChange: (e) => onChange(e.target.value)
	}));
}
function DetailForm({ value, onChange }) {
	return /* @__PURE__ */ react.default.createElement("div", { className: "sales-sdk-demo__advanced-form" }, /* @__PURE__ */ react.default.createElement(FieldText, {
		label: "orderId",
		value: value.orderId != null ? String(value.orderId) : "",
		placeholder: "使用 fixture 默认值",
		onChange: (next) => onChange(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, value), {}, { orderId: parseNumberOrUndefined(next) }))
	}));
}
function MutableForm({ value, onChange }) {
	var _params$schedule_date, _params$schedule_date2;
	const params = value.loadProductsParams || {};
	const picker = value.productPicker || {};
	const setParams = (patch) => {
		onChange(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, value), {}, { loadProductsParams: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, params), patch) }));
	};
	const setPicker = (patch) => {
		onChange(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, value), {}, { productPicker: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, picker), patch) }));
	};
	return /* @__PURE__ */ react.default.createElement("div", { className: "sales-sdk-demo__advanced-form" }, /* @__PURE__ */ react.default.createElement(FieldText, {
		label: "loadProductsParams.schedule_date",
		value: (_params$schedule_date = params.schedule_date) !== null && _params$schedule_date !== void 0 ? _params$schedule_date : "",
		placeholder: "YYYY-MM-DD",
		onChange: (next) => setParams({ schedule_date: next || void 0 })
	}), /* @__PURE__ */ react.default.createElement(FieldText, {
		label: "loadProductsParams.schedule_datetime",
		value: (_params$schedule_date2 = params.schedule_datetime) !== null && _params$schedule_date2 !== void 0 ? _params$schedule_date2 : "",
		placeholder: "YYYY-MM-DD HH:mm:ss",
		onChange: (next) => setParams({ schedule_datetime: next || void 0 })
	}), /* @__PURE__ */ react.default.createElement(FieldText, {
		label: "loadProductsParams.menu_list_ids",
		value: Array.isArray(params.menu_list_ids) ? params.menu_list_ids.join(",") : "",
		placeholder: "逗号分隔，如 44858,44859",
		onChange: (next) => setParams({ menu_list_ids: parseMenuListIds(next) })
	}), /* @__PURE__ */ react.default.createElement(FieldText, {
		label: "customerId",
		value: value.customerId != null ? String(value.customerId) : "",
		placeholder: "缺省 = customer.list[0]",
		onChange: (next) => {
			const parsed = parseNumberOrUndefined(next);
			onChange(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, value), {}, { customerId: parsed }));
		}
	}), /* @__PURE__ */ react.default.createElement(FieldText, {
		label: "productPicker.productId",
		value: picker.productId != null ? String(picker.productId) : "",
		placeholder: "缺省 = products[0].id",
		onChange: (next) => setPicker({ productId: parseNumberOrUndefined(next) })
	}), /* @__PURE__ */ react.default.createElement(FieldText, {
		label: "productPicker.variantIndex",
		value: picker.variantIndex != null ? String(picker.variantIndex) : "",
		placeholder: "缺省 0",
		onChange: (next) => setPicker({ variantIndex: parseNumberOrUndefined(next) })
	}), /* @__PURE__ */ react.default.createElement(FieldText, {
		label: "productPicker.num",
		value: picker.num != null ? String(picker.num) : "",
		placeholder: "缺省 1",
		onChange: (next) => setPicker({ num: parseNumberOrUndefined(next) })
	}));
}
function OrderMetaForm({ value, onChange }) {
	var _value$orderNote;
	const contactsJson = value.contactsInfo === void 0 ? "" : value.contactsInfo === null ? "null" : JSON.stringify(value.contactsInfo);
	return /* @__PURE__ */ react.default.createElement("div", { className: "sales-sdk-demo__advanced-form sales-sdk-demo__advanced-form--meta" }, /* @__PURE__ */ react.default.createElement(FieldText, {
		label: "orderNote（sales.setOrderNote）",
		value: (_value$orderNote = value.orderNote) !== null && _value$orderNote !== void 0 ? _value$orderNote : "",
		placeholder: "留空 = 使用 fixture 默认；填 __skip__ 跳过步骤",
		onChange: (next) => {
			const t = next.trim();
			if (t === "__skip__") {
				const { orderNote: _omit } = value;
				onChange(require_objectWithoutProperties._objectWithoutProperties(value, _excluded));
				return;
			}
			if (!t) {
				const { orderNote: _omit } = value;
				onChange(require_objectWithoutProperties._objectWithoutProperties(value, _excluded2));
				return;
			}
			onChange(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, value), {}, { orderNote: t }));
		}
	}), /* @__PURE__ */ react.default.createElement(FieldText, {
		label: "shopDiscount（sales.setShopDiscount）",
		value: value.shopDiscount != null ? String(value.shopDiscount) : "",
		placeholder: "如 5 或 5.00；填 __skip__ 跳过步骤",
		onChange: (next) => {
			const t = next.trim();
			if (t === "__skip__" || !t) {
				const { shopDiscount: _omit } = value;
				onChange(require_objectWithoutProperties._objectWithoutProperties(value, _excluded3));
				return;
			}
			const n = Number(t);
			onChange(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, value), {}, { shopDiscount: Number.isFinite(n) ? n : t }));
		}
	}), /* @__PURE__ */ react.default.createElement("label", { className: "sales-sdk-demo__field" }, /* @__PURE__ */ react.default.createElement("span", null, "contactsInfo（sales.setContactsInfo，JSON）"), /* @__PURE__ */ react.default.createElement("textarea", {
		className: "sales-sdk-demo__textarea",
		value: contactsJson,
		placeholder: "如 {\"name\":\"Demo\"}；填 __skip__ 跳过；填 null 清空",
		rows: 3,
		onChange: (e) => {
			const t = e.target.value.trim();
			if (t === "__skip__" || t === "") {
				const { contactsInfo: _omit } = value;
				onChange(require_objectWithoutProperties._objectWithoutProperties(value, _excluded4));
				return;
			}
			if (t === "null") {
				onChange(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, value), {}, { contactsInfo: null }));
				return;
			}
			try {
				const parsed = JSON.parse(t);
				if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error("contactsInfo 须为 JSON 对象或 null");
				onChange(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, value), {}, { contactsInfo: parsed }));
			} catch (_unused) {}
		}
	})));
}
function EditForm({ value, onChange }) {
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(FieldText, {
		label: "orderId",
		value: value.orderId != null ? String(value.orderId) : "",
		placeholder: "使用 fixture 默认值",
		onChange: (next) => onChange(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, value), {}, { orderId: parseNumberOrUndefined(next) }))
	}), /* @__PURE__ */ react.default.createElement(OrderMetaForm, {
		value,
		onChange
	}), /* @__PURE__ */ react.default.createElement(MutableForm, {
		value,
		onChange
	}));
}
/**
* 顶层组件，按 variant 分发渲染对应字段；默认收起以保留「一键」干净视觉。
*/
const DemoAdvancedConfig = (props) => {
	const renderBody = () => {
		if (props.variant === "detail") return /* @__PURE__ */ react.default.createElement(DetailForm, {
			value: props.value,
			onChange: props.onChange
		});
		if (props.variant === "create") return /* @__PURE__ */ react.default.createElement(MutableForm, {
			value: props.value,
			onChange: props.onChange
		});
		return /* @__PURE__ */ react.default.createElement(EditForm, {
			value: props.value,
			onChange: props.onChange
		});
	};
	return /* @__PURE__ */ react.default.createElement("div", { className: "sales-sdk-demo__advanced" }, /* @__PURE__ */ react.default.createElement(antd.Collapse, {
		ghost: true,
		items: [{
			key: "adv",
			label: "高级配置（覆盖 fixture）",
			children: renderBody()
		}]
	}));
};
//#endregion
exports.DemoAdvancedConfig = DemoAdvancedConfig;
