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");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_index = require("../../utils/index.js");
const require_useSalesSdk = require("../salesSdk/hooks/useSalesSdk.js");
const require_useSalesSdkCart = require("../salesSdk/hooks/useSalesSdkCart.js");
const require_useSalesSdkCustomer = require("../salesSdk/hooks/useSalesSdkCustomer.js");
const require_useSalesSdkSummary = require("../salesSdk/hooks/useSalesSdkSummary.js");
const require_index$1 = require("../../pro/compensatedPisellContainer/index.js");
const require_withResponsive = require("../../responsive/withResponsive.js");
const require_locales = require("./locales.js");
const require_index$2 = require("./tmpl/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let react_dom = require("react-dom");
require("./index.less");
//#region src/plus/saleDetail/Base.tsx
const _excluded = [
	"closable",
	"className",
	"placement",
	"rootClassName"
];
function SaleDetailFloatingClosePortal({ anchorRef, enabled, onClose, placement }) {
	const [portalContainer, setPortalContainer] = (0, react.useState)(null);
	(0, react.useEffect)(() => {
		var _anchorRef$current;
		if (!enabled) {
			setPortalContainer(null);
			return;
		}
		setPortalContainer(((_anchorRef$current = anchorRef.current) === null || _anchorRef$current === void 0 ? void 0 : _anchorRef$current.closest(".pisell-lowcode-drawer-content-wrapper")) || null);
	}, [anchorRef, enabled]);
	if (!enabled || !portalContainer) return null;
	return (0, react_dom.createPortal)(/* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: (0, classnames.default)("pisell-sale-detail-floating-close", `pisell-sale-detail-floating-close--${placement}`),
		"aria-label": _pisell_utils.locales.getText("pisell2.sale-detail.close"),
		onClick: onClose
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, { type: "pisell2-x-close" })), portalContainer);
}
function SaleDetailBaseContent(props) {
	const { className, containerProps, dataSource, renderType = "current", style, template, tmpl, variant } = props;
	const responsiveVariant = variant || "desktop";
	const currentTemplate = tmpl || template || "A";
	const content = /* @__PURE__ */ react.default.createElement(require_index$2.SaleDetailTemplateRenderer, {
		dataSource,
		tmpl: currentTemplate
	});
	const nextContainerProps = (0, react.useMemo)(() => {
		const _ref = containerProps || {}, { closable, className, placement, rootClassName } = _ref, restContainerProps = require_objectWithoutProperties._objectWithoutProperties(_ref, _excluded);
		const nextPlacement = renderType === "drawer" && responsiveVariant === "phone" ? placement || "bottom" : placement;
		const isSideDrawer = renderType === "drawer" && nextPlacement !== "top" && nextPlacement !== "bottom";
		const nextClosable = closable !== null && closable !== void 0 ? closable : false;
		const nextClassName = (0, classnames.default)("pisell-sale-detail-container", `pisell-sale-detail-container--${responsiveVariant}`, className);
		const nextRootClassName = renderType === "drawer" ? (0, classnames.default)(rootClassName, "pisell-sale-detail-drawer-root", `pisell-sale-detail-drawer-root--${responsiveVariant}`) : rootClassName;
		if (renderType === "drawer" && responsiveVariant === "phone") return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({ placement: nextPlacement }, isSideDrawer ? { width: "90%" } : {
			height: "95vh",
			width: "100%"
		}), {}, {
			closable: false,
			className: nextClassName,
			rootClassName: nextRootClassName,
			zIndex: 950
		}, restContainerProps);
		if (renderType === "drawer") return require_objectSpread2._objectSpread2({
			zIndex: 950,
			placement: nextPlacement,
			width: 480,
			closable: isSideDrawer ? false : nextClosable,
			className: nextClassName,
			rootClassName: nextRootClassName
		}, restContainerProps);
		return require_objectSpread2._objectSpread2({
			closable: nextClosable,
			className: nextClassName,
			zIndex: 950
		}, restContainerProps);
	}, [
		renderType,
		responsiveVariant,
		containerProps
	]);
	const containerPlacement = containerProps === null || containerProps === void 0 ? void 0 : containerProps.placement;
	const isDesktopSideDrawer = renderType === "drawer" && responsiveVariant !== "phone" && containerPlacement !== "top" && containerPlacement !== "bottom";
	const desktopSideDrawerPlacement = containerPlacement === "left" ? "left" : "right";
	const saleDetailRef = (0, react.useRef)(null);
	const handleFloatingClose = (event) => {
		const currentContainerProps = containerProps || {};
		const onClose = currentContainerProps.onClose;
		const onCancel = currentContainerProps.onCancel;
		if (typeof onClose === "function") {
			onClose(event);
			return;
		}
		if (typeof onCancel === "function") onCancel(event);
	};
	const saleDetailNode = /* @__PURE__ */ react.default.createElement("div", {
		ref: saleDetailRef,
		className: (0, classnames.default)("pisell-sale-detail", `pisell-sale-detail--${responsiveVariant}`, `pisell-sale-detail-layout--${renderType}`, `pisell-sale-detail-tmpl--${currentTemplate}`, className),
		style
	}, content);
	if (renderType === "modal" || renderType === "drawer") return /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		renderType,
		props: nextContainerProps,
		showHandlerBar: renderType === "drawer" && responsiveVariant === "phone"
	}, /* @__PURE__ */ react.default.createElement(SaleDetailFloatingClosePortal, {
		anchorRef: saleDetailRef,
		enabled: isDesktopSideDrawer,
		onClose: handleFloatingClose,
		placement: desktopSideDrawerPlacement
	}), saleDetailNode);
	return saleDetailNode;
}
function SaleDetailBaseWithSdkData(props) {
	const sales = require_useSalesSdk.useSalesSdk();
	const customer = require_useSalesSdkCustomer.useSalesSdkCustomer();
	const cart = require_useSalesSdkCart.useSalesSdkCart();
	const summary = require_useSalesSdkSummary.useSalesSdkSummary();
	const dataSource = (0, react.useMemo)(() => ({
		sales,
		customer,
		cart,
		summary
	}), [
		sales,
		customer,
		cart,
		summary
	]);
	return /* @__PURE__ */ react.default.createElement(SaleDetailBaseContent, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props), {}, { dataSource }));
}
function SaleDetailBase(props) {
	var _context$appHelper$ut, _context$appHelper$ut2, _context$appHelper$ut3, _context$appHelper$ut4;
	const context = require_useEngineContext.default();
	require_index.request.setRequest((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 ? void 0 : _context$appHelper$ut.request);
	_pisell_utils.locales.init(require_locales.default, ((_context$appHelper$ut2 = context.appHelper.utils) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut2.getApp) === null || _context$appHelper$ut3 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut3.call(_context$appHelper$ut2)) === null || _context$appHelper$ut3 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut3.locales) === null || _context$appHelper$ut3 === void 0 || (_context$appHelper$ut4 = _context$appHelper$ut3.getLocale) === null || _context$appHelper$ut4 === void 0 ? void 0 : _context$appHelper$ut4.call(_context$appHelper$ut3)) || "en");
	if (props.dataSource) return /* @__PURE__ */ react.default.createElement(SaleDetailBaseContent, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props), {}, { dataSource: props.dataSource }));
	return /* @__PURE__ */ react.default.createElement(SaleDetailBaseWithSdkData, props);
}
SaleDetailBase.displayName = "SaleDetailBase";
var Base_default = require_withResponsive.default(SaleDetailBase);
//#endregion
exports.default = Base_default;
