function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { useMemo } from 'react';
import classNames from 'classnames';
import { AmountDetails, CartActions, Client, Contact, PromotionAlert, SaleQrButton } from "../../components";
import { CartItems } from "../../components/CartItems/Editable";
import { DetailActions, TEMPLATE_A_ACTIONS } from "../../components/DetailActions";
import { MobileSheetFrame } from "../../shared";
import { resolveSaleDetailShowContactInfo, resolveSaleDetailTransactionHabit } from "../../utils";
import "./index.less";
import { useMemoizedFn } from 'ahooks';
function isCheckoutAction(action) {
  return action.key === 'checkout' || action.key === 'checkout-cart';
}
function resolveTemplateAActionByTransactionHabit(action, habitType) {
  var isSavePrimary = habitType === 'service_now_pay_later';
  if (action.key === 'save') {
    return _objectSpread(_objectSpread({}, action), {}, {
      row: 1,
      order: isSavePrimary ? 30 : 20,
      slot: isSavePrimary ? 'footer-primary' : 'footer-secondary'
    });
  }
  if (isCheckoutAction(action)) {
    return _objectSpread(_objectSpread({}, action), {}, {
      row: 1,
      order: isSavePrimary ? 20 : 30,
      slot: isSavePrimary ? 'footer-secondary' : 'footer-primary'
    });
  }
  return action;
}
function resolveTemplateAOrderActions(actions, habitType) {
  if (!habitType) return actions;
  return actions.map(function (action) {
    return resolveTemplateAActionByTransactionHabit(action, habitType);
  });
}
function SaleDetailTemplateAMain(_ref) {
  var _context$booking, _context$cart3, _context$cart4, _context$sales, _context$sales2, _context$sales$tempOr2, _moduleConfig$cartIte3, _moduleConfig$cartIte4, _moduleConfig$cartIte6;
  var context = _ref.context,
    moduleConfig = _ref.moduleConfig;
  var bookingConfig = (_context$booking = context.booking) === null || _context$booking === void 0 ? void 0 : _context$booking.getBookingConfig();
  var showContactFromConfig = resolveSaleDetailShowContactInfo(bookingConfig);
  var hiddenModules = (moduleConfig === null || moduleConfig === void 0 ? void 0 : moduleConfig.hidden) || [];
  var showCustomer = !hiddenModules.includes('customer');
  var showContact = !hiddenModules.includes('contact') && showContactFromConfig;
  var showCartItems = !hiddenModules.includes('cartItems');
  var showCartActions = !hiddenModules.includes('cartActions');
  var showPromotionAlert = !hiddenModules.includes('promotionAlert');
  var hasCartItems = useMemo(function () {
    var _context$cart, _context$cart2;
    return Boolean((_context$cart = context.cart) === null || _context$cart === void 0 || (_context$cart = _context$cart.bookings) === null || _context$cart === void 0 ? void 0 : _context$cart.length) || Boolean((_context$cart2 = context.cart) === null || _context$cart2 === void 0 || (_context$cart2 = _context$cart2.items) === null || _context$cart2 === void 0 ? void 0 : _context$cart2.length);
  }, [(_context$cart3 = context.cart) === null || _context$cart3 === void 0 || (_context$cart3 = _context$cart3.bookings) === null || _context$cart3 === void 0 ? void 0 : _context$cart3.length, (_context$cart4 = context.cart) === null || _context$cart4 === void 0 || (_context$cart4 = _context$cart4.items) === null || _context$cart4 === void 0 ? void 0 : _context$cart4.length]);
  var clientContext = useMemo(function () {
    return {
      customer: context.customer,
      sales: context.sales
    };
  }, [context.customer, (_context$sales = context.sales) === null || _context$sales === void 0 ? void 0 : _context$sales.setContactsInfo]);
  var saleQrButtonClick = useMemoizedFn(function () {
    context.sales.activateCamera();
  });
  var clientEntryContent = useMemo(function () {
    return /*#__PURE__*/React.createElement("div", {
      className: "pisell-sale-detail-client-entry"
    }, /*#__PURE__*/React.createElement(Client, {
      context: clientContext,
      isShowAvatar: true
    }), /*#__PURE__*/React.createElement(SaleQrButton, {
      onClick: saleQrButtonClick
    }));
  }, [clientContext]);
  var contactContext = useMemo(function () {
    return {
      sales: context.sales
    };
  }, [(_context$sales2 = context.sales) === null || _context$sales2 === void 0 ? void 0 : _context$sales2.setContactsInfo]);
  var contactContent = useMemo(function () {
    var _context$sales$tempOr;
    return /*#__PURE__*/React.createElement(Contact, {
      dataSource: (_context$sales$tempOr = context.sales.tempOrder) === null || _context$sales$tempOr === void 0 ? void 0 : _context$sales$tempOr.contacts_info,
      context: contactContext,
      showAction: true,
      showEmpty: true
    });
  }, [(_context$sales$tempOr2 = context.sales.tempOrder) === null || _context$sales$tempOr2 === void 0 ? void 0 : _context$sales$tempOr2.contacts_info, contactContext]);
  var promotionAlertContext = useMemo(function () {
    return {
      cart: context.cart,
      sales: context.sales
    };
  }, [context.cart, context.sales]);
  var cartActionsContext = useMemo(function () {
    return {
      cart: context.cart,
      sales: context.sales,
      summary: context.summary
    };
  }, [context.cart, context.sales, context.summary]);
  var cartItemsContext = useMemo(function () {
    return {
      cart: context.cart,
      sales: context.sales,
      booking: context.booking
    };
  }, [context.cart, context.sales, context.booking]);
  var CartItemsInteractionPolicy = useMemo(function () {
    var _moduleConfig$cartIte, _moduleConfig$cartIte2;
    return {
      cardAction: 'edit',
      disabledEdit: true,
      isShowRightActions: true,
      showProductImage: moduleConfig === null || moduleConfig === void 0 || (_moduleConfig$cartIte = moduleConfig.cartItems) === null || _moduleConfig$cartIte === void 0 ? void 0 : _moduleConfig$cartIte.showProductImage,
      showRemoveButton: moduleConfig === null || moduleConfig === void 0 || (_moduleConfig$cartIte2 = moduleConfig.cartItems) === null || _moduleConfig$cartIte2 === void 0 ? void 0 : _moduleConfig$cartIte2.showRemoveButton
    };
  }, [moduleConfig === null || moduleConfig === void 0 || (_moduleConfig$cartIte3 = moduleConfig.cartItems) === null || _moduleConfig$cartIte3 === void 0 ? void 0 : _moduleConfig$cartIte3.showProductImage, moduleConfig === null || moduleConfig === void 0 || (_moduleConfig$cartIte4 = moduleConfig.cartItems) === null || _moduleConfig$cartIte4 === void 0 ? void 0 : _moduleConfig$cartIte4.showRemoveButton]);
  var cartItemContent = useMemo(function () {
    var _moduleConfig$cartIte5;
    return /*#__PURE__*/React.createElement("div", {
      className: "pisell-sale-detail-cart-scroll-panel"
    }, /*#__PURE__*/React.createElement("div", {
      className: "pisell-sale-detail-scroll-content"
    }, /*#__PURE__*/React.createElement(CartItems, {
      context: cartItemsContext,
      layout: moduleConfig === null || moduleConfig === void 0 || (_moduleConfig$cartIte5 = moduleConfig.cartItems) === null || _moduleConfig$cartIte5 === void 0 ? void 0 : _moduleConfig$cartIte5.layout,
      interactionPolicy: CartItemsInteractionPolicy,
      showEmpty: true
    })), showPromotionAlert ? /*#__PURE__*/React.createElement(PromotionAlert, {
      context: promotionAlertContext
    }) : null);
  }, [cartItemsContext, moduleConfig === null || moduleConfig === void 0 || (_moduleConfig$cartIte6 = moduleConfig.cartItems) === null || _moduleConfig$cartIte6 === void 0 ? void 0 : _moduleConfig$cartIte6.layout, CartItemsInteractionPolicy, promotionAlertContext]);
  var cartActionsContent = useMemo(function () {
    return /*#__PURE__*/React.createElement(CartActions, {
      context: cartActionsContext,
      hasCartItems: hasCartItems
    });
  }, [cartActionsContext, hasCartItems]);
  return /*#__PURE__*/React.createElement("main", {
    className: "pisell-sale-detail-main"
  }, showCustomer ? clientEntryContent : null, showContact ? contactContent : null, showCartItems ? cartItemContent : null, showCartActions ? cartActionsContent : null);
}
function SaleDetailTemplateAFooter(_ref2) {
  var _context$booking$getB, _context$booking2, _context$booking2$get;
  var amountDetailsDefaultExpanded = _ref2.amountDetailsDefaultExpanded,
    context = _ref2.context,
    onOrderComplete = _ref2.onOrderComplete,
    _ref2$orderActions = _ref2.orderActions,
    orderActions = _ref2$orderActions === void 0 ? TEMPLATE_A_ACTIONS : _ref2$orderActions,
    showAmountDetails = _ref2.showAmountDetails,
    showOrderActions = _ref2.showOrderActions;
  var bookingConfig = (_context$booking$getB = (_context$booking2 = context.booking) === null || _context$booking2 === void 0 || (_context$booking2$get = _context$booking2.getBookingConfig) === null || _context$booking2$get === void 0 ? void 0 : _context$booking2$get.call(_context$booking2)) !== null && _context$booking$getB !== void 0 ? _context$booking$getB : null;
  var transactionHabit = useMemo(function () {
    return resolveSaleDetailTransactionHabit(bookingConfig);
  }, [bookingConfig]);
  var effectiveOrderActions = useMemo(function () {
    return resolveTemplateAOrderActions(orderActions, transactionHabit.type);
  }, [orderActions, transactionHabit.type]);
  var hasOrderActions = showOrderActions && effectiveOrderActions.length > 0;
  var hasFloorResourceAction = effectiveOrderActions.some(function (action) {
    return action.key === 'select-floor-resource';
  });
  if (!showAmountDetails && !hasOrderActions) {
    return null;
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-sale-detail-footer"
  }, showAmountDetails ? /*#__PURE__*/React.createElement(AmountDetails, {
    defaultExpanded: amountDetailsDefaultExpanded,
    context: {
      sales: context.sales,
      summary: context.summary
    }
  }) : null, hasOrderActions ? /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-sale-detail-button-actions', 'pisell-sale-detail-button-actions--template-a', {
      'pisell-sale-detail-button-actions--with-floor-resource': hasFloorResourceAction
    })
  }, /*#__PURE__*/React.createElement(DetailActions, {
    actions: effectiveOrderActions,
    onOrderComplete: onOrderComplete
  })) : null);
}
export function SaleDetailTemplateA(_ref3) {
  var _moduleConfig$amountD;
  var context = _ref3.context,
    mobilePresentation = _ref3.mobilePresentation,
    moduleConfig = _ref3.moduleConfig,
    renderType = _ref3.renderType,
    variant = _ref3.variant;
  var hiddenModules = (moduleConfig === null || moduleConfig === void 0 ? void 0 : moduleConfig.hidden) || [];
  var showAmountDetails = !hiddenModules.includes('amountDetails');
  var showOrderActions = !hiddenModules.includes('orderActions');
  var isMobileBottomSheet = variant === 'phone' && renderType === 'current' && mobilePresentation === 'bottomSheet';
  var bodyContent = /*#__PURE__*/React.createElement("main", {
    className: "pisell-sale-detail-tmpl-body"
  }, /*#__PURE__*/React.createElement("section", {
    className: "pisell-sale-detail-tmpl-content"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-sale-detail-inline-layout"
  }, /*#__PURE__*/React.createElement(SaleDetailTemplateAMain, {
    context: context,
    moduleConfig: moduleConfig
  }))));
  var footerContent = /*#__PURE__*/React.createElement(SaleDetailTemplateAFooter, {
    context: {
      sales: context.sales,
      summary: context.summary,
      booking: context.booking
    },
    onOrderComplete: context.onOrderComplete,
    orderActions: moduleConfig === null || moduleConfig === void 0 ? void 0 : moduleConfig.orderActions,
    showAmountDetails: showAmountDetails,
    showOrderActions: showOrderActions,
    amountDetailsDefaultExpanded: moduleConfig === null || moduleConfig === void 0 || (_moduleConfig$amountD = moduleConfig.amountDetails) === null || _moduleConfig$amountD === void 0 ? void 0 : _moduleConfig$amountD.defaultExpanded
  });
  if (isMobileBottomSheet) {
    return /*#__PURE__*/React.createElement(MobileSheetFrame, {
      className: classNames('pisell-sale-detail-tmpl-a', 'pisell-sale-detail-tmpl-a--mobile-sheet'),
      footer: footerContent
    }, bodyContent);
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-sale-detail-tmpl-a"
  }, /*#__PURE__*/React.createElement("main", {
    className: "pisell-sale-detail-tmpl-body"
  }, /*#__PURE__*/React.createElement("section", {
    className: "pisell-sale-detail-tmpl-content"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-sale-detail-inline-layout"
  }, /*#__PURE__*/React.createElement(SaleDetailTemplateAMain, {
    context: context,
    moduleConfig: moduleConfig
  }), footerContent))));
}