import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import { request } from "../../utils/index.js";
import { useSalesSdk } from "../salesSdk/hooks/useSalesSdk.js";
import { useSalesSdkCart } from "../salesSdk/hooks/useSalesSdkCart.js";
import { useSalesSdkCustomer } from "../salesSdk/hooks/useSalesSdkCustomer.js";
import { useSalesSdkSummary } from "../salesSdk/hooks/useSalesSdkSummary.js";
import CompensatedPisellContainer from "../../pro/compensatedPisellContainer/index.js";
import withResponsive from "../../responsive/withResponsive.js";
import locales_default from "./locales.js";
import { SaleDetailTemplateRenderer } from "./tmpl/index.js";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { locales } from "@pisell/utils";
import { Iconfont } from "@pisell/materials";
import classNames from "classnames";
import { createPortal } from "react-dom";
import "./index.less";
//#region src/plus/saleDetail/Base.tsx
const _excluded = [
	"closable",
	"className",
	"placement",
	"rootClassName"
];
function SaleDetailFloatingClosePortal({ anchorRef, enabled, onClose, placement }) {
	const [portalContainer, setPortalContainer] = useState(null);
	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 createPortal(/* @__PURE__ */ React.createElement("button", {
		type: "button",
		className: classNames("pisell-sale-detail-floating-close", `pisell-sale-detail-floating-close--${placement}`),
		"aria-label": locales.getText("pisell2.sale-detail.close"),
		onClick: onClose
	}, /* @__PURE__ */ React.createElement(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.createElement(SaleDetailTemplateRenderer, {
		dataSource,
		tmpl: currentTemplate
	});
	const nextContainerProps = useMemo(() => {
		const _ref = containerProps || {}, { closable, className, placement, rootClassName } = _ref, restContainerProps = _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 = classNames("pisell-sale-detail-container", `pisell-sale-detail-container--${responsiveVariant}`, className);
		const nextRootClassName = renderType === "drawer" ? classNames(rootClassName, "pisell-sale-detail-drawer-root", `pisell-sale-detail-drawer-root--${responsiveVariant}`) : rootClassName;
		if (renderType === "drawer" && responsiveVariant === "phone") return _objectSpread2(_objectSpread2({ placement: nextPlacement }, isSideDrawer ? { width: "90%" } : {
			height: "95vh",
			width: "100%"
		}), {}, {
			closable: false,
			className: nextClassName,
			rootClassName: nextRootClassName,
			zIndex: 950
		}, restContainerProps);
		if (renderType === "drawer") return _objectSpread2({
			zIndex: 950,
			placement: nextPlacement,
			width: 480,
			closable: isSideDrawer ? false : nextClosable,
			className: nextClassName,
			rootClassName: nextRootClassName
		}, restContainerProps);
		return _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 = 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.createElement("div", {
		ref: saleDetailRef,
		className: classNames("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.createElement(CompensatedPisellContainer, {
		renderType,
		props: nextContainerProps,
		showHandlerBar: renderType === "drawer" && responsiveVariant === "phone"
	}, /* @__PURE__ */ React.createElement(SaleDetailFloatingClosePortal, {
		anchorRef: saleDetailRef,
		enabled: isDesktopSideDrawer,
		onClose: handleFloatingClose,
		placement: desktopSideDrawerPlacement
	}), saleDetailNode);
	return saleDetailNode;
}
function SaleDetailBaseWithSdkData(props) {
	const sales = useSalesSdk();
	const customer = useSalesSdkCustomer();
	const cart = useSalesSdkCart();
	const summary = useSalesSdkSummary();
	const dataSource = useMemo(() => ({
		sales,
		customer,
		cart,
		summary
	}), [
		sales,
		customer,
		cart,
		summary
	]);
	return /* @__PURE__ */ React.createElement(SaleDetailBaseContent, _objectSpread2(_objectSpread2({}, props), {}, { dataSource }));
}
function SaleDetailBase(props) {
	var _context$appHelper$ut, _context$appHelper$ut2, _context$appHelper$ut3, _context$appHelper$ut4;
	const context = useEngineContext();
	request.setRequest((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 ? void 0 : _context$appHelper$ut.request);
	locales.init(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.createElement(SaleDetailBaseContent, _objectSpread2(_objectSpread2({}, props), {}, { dataSource: props.dataSource }));
	return /* @__PURE__ */ React.createElement(SaleDetailBaseWithSdkData, props);
}
SaleDetailBase.displayName = "SaleDetailBase";
var Base_default = withResponsive(SaleDetailBase);
//#endregion
export { Base_default as default };
