import React, { useEffect, useRef } from 'react';
import classNames from 'classnames';
import { withResponsive } from "../../responsive";
import { useBigSaleContext, useResolvedSaleDetailModuleConfig } from "./hooks";
import { BigSaleTemplateRenderer } from "./templates";
var DEFAULT_MODE = 'ticket';

/**
 * BigSaleBase 位于 SalesSdkProvider 内部，负责把 SDK 数据分发给商品列表和 SaleDetailBase。
 */
function BigSaleBase(_ref) {
  var _ref$isActive = _ref.isActive,
    isActive = _ref$isActive === void 0 ? true : _ref$isActive,
    saleDetailHeaderSlot = _ref.saleDetailHeaderSlot,
    overlaySlot = _ref.overlaySlot,
    className = _ref.className,
    style = _ref.style,
    _ref$mode = _ref.mode,
    mode = _ref$mode === void 0 ? DEFAULT_MODE : _ref$mode,
    tmpl = _ref.tmpl,
    _ref$template = _ref.template,
    template = _ref$template === void 0 ? 'standard' : _ref$template,
    _ref$variant = _ref.variant,
    variant = _ref$variant === void 0 ? 'desktop' : _ref$variant,
    productLoadParams = _ref.productLoadParams,
    productDisplayConfig = _ref.productDisplayConfig,
    productCatalogSessionKey = _ref.productCatalogSessionKey,
    saleDetailTemplate = _ref.saleDetailTemplate,
    saleDetailModuleConfig = _ref.saleDetailModuleConfig,
    _ref$autoCreate = _ref.autoCreate,
    autoCreate = _ref$autoCreate === void 0 ? true : _ref$autoCreate,
    orderId = _ref.orderId,
    locale = _ref.locale,
    saleDetailWrapperProps = _ref.saleDetailWrapperProps,
    cartProductsPerRow = _ref.cartProductsPerRow,
    showClient = _ref.showClient,
    onOrderComplete = _ref.onOrderComplete;
  var context = useBigSaleContext();
  var currentTemplate = tmpl || template || 'standard';
  var autoCreateStartedRef = useRef(false);
  var resolvedSaleDetailModuleConfig = useResolvedSaleDetailModuleConfig({
    template: currentTemplate,
    saleDetailModuleConfig: saleDetailModuleConfig,
    cartProductsPerRow: cartProductsPerRow,
    showClient: showClient
  });
  var isSecondaryTemplate = currentTemplate === 'secondaryScreen';
  useEffect(function () {
    if (!autoCreate || orderId || autoCreateStartedRef.current) return;
    if (!context.sales.bookingTicket || context.sales.tempOrder) return;
    if (context.sales.status !== 'idle') return;
    autoCreateStartedRef.current = true;
    context.sales.createNew().catch(function () {
      autoCreateStartedRef.current = false;
    });
  }, [autoCreate, orderId, context.sales, context.sales.bookingTicket, context.sales.status, context.sales.tempOrder]);
  return /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-big-sale', "pisell-big-sale--".concat(variant), "pisell-big-sale-tmpl--".concat(currentTemplate), {
      'pisell-big-sale--secondary-screen': isSecondaryTemplate
    }, className),
    style: style
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-big-sale__layout"
  }, /*#__PURE__*/React.createElement(BigSaleTemplateRenderer, {
    isActive: isActive,
    mode: mode,
    tmpl: currentTemplate,
    variant: variant,
    context: context,
    productLoadParams: productLoadParams,
    productDisplayConfig: productDisplayConfig,
    productCatalogSessionKey: productCatalogSessionKey,
    saleDetailTemplate: saleDetailTemplate,
    saleDetailModuleConfig: resolvedSaleDetailModuleConfig,
    orderId: orderId,
    locale: locale,
    saleDetailWrapperProps: isSecondaryTemplate ? saleDetailWrapperProps : undefined,
    saleDetailHeaderSlot: saleDetailHeaderSlot,
    onOrderComplete: onOrderComplete
  })), overlaySlot);
}
BigSaleBase.displayName = 'BigSaleBase';
export var ResponsiveBigSaleBase = withResponsive(BigSaleBase);
export default BigSaleBase;