var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/saleDetail/Base.tsx
var Base_exports = {};
__export(Base_exports, {
  default: () => Base_default
});
module.exports = __toCommonJS(Base_exports);
var import_react = __toESM(require("react"));
var import_react_dom = require("react-dom");
var import_classnames = __toESM(require("classnames"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_utils2 = require("../../utils");
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_compensatedPisellContainer = __toESM(require("../../pro/compensatedPisellContainer"));
var import_responsive = require("../../responsive");
var import_salesSdk = require("../salesSdk");
var import_locales = __toESM(require("./locales"));
var import_tmpl = require("./tmpl");
var import_index = require("./index.less");
function SaleDetailFloatingClosePortal({
  anchorRef,
  enabled,
  onClose,
  placement
}) {
  const [portalContainer, setPortalContainer] = (0, import_react.useState)(null);
  (0, import_react.useEffect)(() => {
    var _a;
    if (!enabled) {
      setPortalContainer(null);
      return;
    }
    const drawerWrapper = (_a = anchorRef.current) == null ? void 0 : _a.closest(
      ".pisell-lowcode-drawer-content-wrapper"
    );
    setPortalContainer(drawerWrapper || null);
  }, [anchorRef, enabled]);
  if (!enabled || !portalContainer) {
    return null;
  }
  return (0, import_react_dom.createPortal)(
    /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        type: "button",
        className: (0, import_classnames.default)(
          "pisell-sale-detail-floating-close",
          `pisell-sale-detail-floating-close--${placement}`
        ),
        "aria-label": import_utils.locales.getText("pisell2.sale-detail.close"),
        onClick: onClose
      },
      /* @__PURE__ */ import_react.default.createElement(import_materials.Iconfont, { type: "pisell2-x-close" })
    ),
    portalContainer
  );
}
function SaleDetailBaseContent(props) {
  const {
    className,
    containerProps,
    context: saleDetailContext,
    renderType = "current",
    style,
    template,
    mobilePresentation,
    moduleConfig,
    tmpl,
    variant
  } = props;
  const responsiveVariant = variant || "desktop";
  const currentTemplate = tmpl || template || "A";
  const content = /* @__PURE__ */ import_react.default.createElement(
    import_tmpl.SaleDetailTemplateRenderer,
    {
      context: saleDetailContext,
      moduleConfig,
      mobilePresentation,
      renderType,
      tmpl: currentTemplate,
      variant: responsiveVariant
    }
  );
  const nextContainerProps = (0, import_react.useMemo)(() => {
    const {
      closable,
      className: className2,
      placement,
      rootClassName,
      ...restContainerProps
    } = containerProps || {};
    const nextPlacement = renderType === "drawer" && responsiveVariant === "phone" ? placement || "bottom" : placement;
    const isSideDrawer = renderType === "drawer" && nextPlacement !== "top" && nextPlacement !== "bottom";
    const nextClosable = closable ?? false;
    const nextClassName = (0, import_classnames.default)(
      "pisell-sale-detail-container",
      `pisell-sale-detail-container--${responsiveVariant}`,
      className2
    );
    const nextRootClassName = renderType === "drawer" ? (0, import_classnames.default)(
      rootClassName,
      "pisell-sale-detail-drawer-root",
      `pisell-sale-detail-drawer-root--${responsiveVariant}`
    ) : rootClassName;
    if (renderType === "drawer" && responsiveVariant === "phone") {
      return {
        placement: nextPlacement,
        ...isSideDrawer ? { width: "90%" } : {
          height: "95vh",
          width: "100%"
        },
        closable: false,
        className: nextClassName,
        rootClassName: nextRootClassName,
        zIndex: 950,
        ...restContainerProps
      };
    }
    if (renderType === "drawer") {
      return {
        zIndex: 950,
        placement: nextPlacement,
        width: 480,
        closable: isSideDrawer ? false : nextClosable,
        className: nextClassName,
        rootClassName: nextRootClassName,
        ...restContainerProps
      };
    }
    return {
      closable: nextClosable,
      className: nextClassName,
      zIndex: 950,
      ...restContainerProps
    };
  }, [renderType, responsiveVariant, containerProps]);
  const containerPlacement = containerProps == null ? void 0 : containerProps.placement;
  const isDesktopSideDrawer = renderType === "drawer" && responsiveVariant !== "phone" && containerPlacement !== "top" && containerPlacement !== "bottom";
  const desktopSideDrawerPlacement = containerPlacement === "left" ? "left" : "right";
  const saleDetailRef = (0, import_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__ */ import_react.default.createElement(
    "div",
    {
      ref: saleDetailRef,
      className: (0, import_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__ */ import_react.default.createElement(
      import_compensatedPisellContainer.default,
      {
        renderType,
        props: nextContainerProps,
        showHandlerBar: renderType === "drawer" && responsiveVariant === "phone"
      },
      /* @__PURE__ */ import_react.default.createElement(
        SaleDetailFloatingClosePortal,
        {
          anchorRef: saleDetailRef,
          enabled: isDesktopSideDrawer,
          onClose: handleFloatingClose,
          placement: desktopSideDrawerPlacement
        }
      ),
      saleDetailNode
    );
  }
  return saleDetailNode;
}
function SaleDetailBaseWithSdkData(props) {
  const sales = (0, import_salesSdk.useSalesSdk)();
  const customer = (0, import_salesSdk.useSalesSdkCustomer)();
  const cart = (0, import_salesSdk.useSalesSdkCart)();
  const summary = (0, import_salesSdk.useSalesSdkSummary)();
  const booking = (0, import_salesSdk.useSalesSdkBookingContext)();
  const saleDetailContext = (0, import_react.useMemo)(
    () => ({
      sales,
      customer,
      cart,
      summary,
      booking
    }),
    [sales, customer, cart, summary, booking]
  );
  return /* @__PURE__ */ import_react.default.createElement(SaleDetailBaseContent, { ...props, context: saleDetailContext });
}
function SaleDetailBase(props) {
  var _a, _b, _c, _d, _e, _f;
  const context = (0, import_useEngineContext.default)();
  import_utils2.request.setRequest((_a = context.appHelper.utils) == null ? void 0 : _a.request);
  import_utils.locales.init(
    import_locales.default,
    ((_f = (_e = (_d = (_c = (_b = context.appHelper.utils) == null ? void 0 : _b.getApp) == null ? void 0 : _c.call(_b)) == null ? void 0 : _d.locales) == null ? void 0 : _e.getLocale) == null ? void 0 : _f.call(_e)) || "en"
  );
  if (props.context) {
    return /* @__PURE__ */ import_react.default.createElement(SaleDetailBaseContent, { ...props, context: props.context });
  }
  return /* @__PURE__ */ import_react.default.createElement(SaleDetailBaseWithSdkData, { ...props });
}
SaleDetailBase.displayName = "SaleDetailBase";
var Base_default = (0, import_responsive.withResponsive)(SaleDetailBase);
