function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { memo, useCallback, useMemo, useState } from 'react';
import classNames from 'classnames';
import { Iconfont } from '@pisell/materials';
import { locales } from '@pisell/utils';
import { SaleOverview, SummaryFinancialSections } from "../../components";
import { Summary as AmountSummary } from "../../components/Summary";
import { DetailActions, TEMPLATE_B_PAY_ACTIONS, TEMPLATE_B_SUMMARY_ACTIONS } from "../../components/DetailActions";
import { MobileSheetFrame } from "../../shared";
import { Customer, Notes, Pay, Summary, getSaleDetailTemplateBNotesCount } from "./components";
import "./index.less";
var EMPTY_ACTIONS = [];
var TEMPLATE_B_ACTIONS_BY_TAB = {
  summary: TEMPLATE_B_SUMMARY_ACTIONS,
  pay: TEMPLATE_B_PAY_ACTIONS,
  customer: EMPTY_ACTIONS,
  notes: EMPTY_ACTIONS
};
var SaleDetailTemplateBSider = /*#__PURE__*/memo(function SaleDetailTemplateBSider(_ref) {
  var activeTabKey = _ref.activeTabKey,
    items = _ref.items,
    onActiveTabChange = _ref.onActiveTabChange;
  return /*#__PURE__*/React.createElement("aside", {
    className: "pisell-sale-detail-tmpl-sider",
    role: "tablist"
  }, items.map(function (item) {
    var active = item.key === activeTabKey;
    var badgeCount = item.badgeCount;
    var hasBadge = typeof badgeCount === 'number' && badgeCount > 0;
    var badgeText = hasBadge ? badgeCount > 99 ? '99+' : String(badgeCount) : '';
    return /*#__PURE__*/React.createElement("button", {
      key: item.key,
      type: "button",
      role: "tab",
      "aria-selected": active,
      className: classNames('pisell-sale-detail-tmpl-sider-tab', {
        'pisell-sale-detail-tmpl-sider-tab--active': active
      }),
      onClick: function onClick() {
        return onActiveTabChange(item.key);
      }
    }, /*#__PURE__*/React.createElement("span", {
      className: "pisell-sale-detail-tmpl-sider-tab__icon"
    }, /*#__PURE__*/React.createElement(Iconfont, {
      type: item.iconType || 'pisell2-dots-horizontal'
    })), /*#__PURE__*/React.createElement("span", {
      className: "pisell-sale-detail-tmpl-sider-tab__label"
    }, item.title), hasBadge ? /*#__PURE__*/React.createElement("span", {
      className: "pisell-sale-detail-tmpl-sider-tab__badge"
    }, badgeText) : null);
  }));
});
var SaleDetailTemplateBFooter = /*#__PURE__*/memo(function SaleDetailTemplateBFooter(_ref2) {
  var activeTabKey = _ref2.activeTabKey,
    amountDetailsDefaultExpanded = _ref2.amountDetailsDefaultExpanded,
    amountContext = _ref2.amountContext,
    onOrderComplete = _ref2.onOrderComplete,
    printOnlyOrderActions = _ref2.printOnlyOrderActions,
    showAmountDetails = _ref2.showAmountDetails,
    showOrderActions = _ref2.showOrderActions;
  var baseActions = TEMPLATE_B_ACTIONS_BY_TAB[activeTabKey] || EMPTY_ACTIONS;
  var actions = printOnlyOrderActions ? baseActions.filter(function (action) {
    return action.key === 'print';
  }) : baseActions;
  var shouldShowAmountDetails = (activeTabKey === 'summary' || activeTabKey === 'pay') && showAmountDetails;
  var hasOrderActions = showOrderActions && actions.length > 0;
  var amountSummaryContext = useMemo(function () {
    return {
      sales: amountContext.sales,
      summary: amountContext.summary
    };
  }, [amountContext.sales, amountContext.summary]);
  if (!shouldShowAmountDetails && !hasOrderActions) {
    return null;
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-sale-detail-footer"
  }, shouldShowAmountDetails ? /*#__PURE__*/React.createElement(AmountSummary, {
    defaultExpanded: amountDetailsDefaultExpanded,
    context: amountSummaryContext
  }) : null, hasOrderActions ? /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-sale-detail-button-actions', 'pisell-sale-detail-button-actions--template-b')
  }, /*#__PURE__*/React.createElement(DetailActions, {
    actions: actions,
    onOrderComplete: onOrderComplete
  })) : null);
});
var SaleDetailTemplateBActivePane = /*#__PURE__*/memo(function SaleDetailTemplateBActivePane(_ref3) {
  var activeTabKey = _ref3.activeTabKey,
    context = _ref3.context,
    moduleConfig = _ref3.moduleConfig,
    showOrderNote = _ref3.showOrderNote,
    variant = _ref3.variant,
    hideFinancialSections = _ref3.hideFinancialSections;
  switch (activeTabKey) {
    case 'customer':
      return /*#__PURE__*/React.createElement(Customer, {
        context: context,
        variant: variant
      });
    case 'notes':
      return showOrderNote ? /*#__PURE__*/React.createElement(Notes, {
        context: context
      }) : null;
    case 'pay':
      return /*#__PURE__*/React.createElement(Pay, {
        context: context
      });
    case 'summary':
    default:
      return /*#__PURE__*/React.createElement(Summary, {
        context: context,
        moduleConfig: moduleConfig,
        hideFinancialSections: hideFinancialSections
      });
  }
});
export function SaleDetailTemplateB(_ref4) {
  var _context$sales$tempOr, _context$sales$tempOr2, _moduleConfig$amountD;
  var context = _ref4.context,
    mobilePresentation = _ref4.mobilePresentation,
    moduleConfig = _ref4.moduleConfig,
    renderType = _ref4.renderType,
    summaryTwoColumn = _ref4.summaryTwoColumn,
    variant = _ref4.variant;
  var hiddenModules = (moduleConfig === null || moduleConfig === void 0 ? void 0 : moduleConfig.hidden) || [];
  var _useState = useState('summary'),
    _useState2 = _slicedToArray(_useState, 2),
    activeTabKey = _useState2[0],
    setActiveTabKey = _useState2[1];
  var showOverview = !hiddenModules.includes('overview');
  var showAmountDetails = !hiddenModules.includes('amountDetails');
  var isCancelledOrder = String(((_context$sales$tempOr = context.sales.tempOrder) === null || _context$sales$tempOr === void 0 ? void 0 : _context$sales$tempOr.status) || '').toLowerCase() === 'cancelled';
  var isFullyRefundedOrder = ((_context$sales$tempOr2 = context.sales.tempOrder) === null || _context$sales$tempOr2 === void 0 ? void 0 : _context$sales$tempOr2.payment_status) === 'refunded';
  var showOrderActions = !hiddenModules.includes('orderActions') && (!isCancelledOrder || isFullyRefundedOrder);
  var printOnlyOrderActions = isCancelledOrder && isFullyRefundedOrder;
  var showOrderNote = !hiddenModules.includes('orderNote');
  var notesCount = showOrderNote ? getSaleDetailTemplateBNotesCount(context) : 0;
  var isMobileBottomSheet = variant === 'phone' && renderType === 'current' && mobilePresentation === 'bottomSheet';
  // 仅 summary tab + 非手机底部抽屉时启用两栏：把金额明细 + footer 抽到右列。
  var twoColumnSummary = !!summaryTwoColumn && activeTabKey === 'summary' && !isMobileBottomSheet;
  var activePaneContext = useMemo(function () {
    return {
      sales: context.sales,
      customer: context.customer,
      cart: context.cart,
      summary: context.summary,
      booking: context.booking,
      onOrderComplete: context.onOrderComplete
    };
  }, [context.booking, context.cart, context.customer, context.onOrderComplete, context.sales, context.summary.summary]);
  var currentLocale = locales.getLocale();
  var tabs = useMemo(function () {
    return [{
      key: 'summary',
      title: locales.getText('pisell2.sale-detail.summary'),
      iconType: 'pisell2-dots-horizontal'
    }, {
      key: 'customer',
      title: locales.getText('pisell2.sale-detail.customer'),
      iconType: 'pisell2-user-01'
    }].concat(_toConsumableArray(showOrderNote ? [{
      key: 'notes',
      title: locales.getText('pisell2.sale-detail.notes'),
      iconType: 'pisell2-message-alert-square',
      badgeCount: notesCount
    }] : []), [{
      key: 'pay',
      title: locales.getText('pisell2.sale-detail.pay'),
      iconType: 'pisell2-credit-card-02'
    }]);
  }, [currentLocale, notesCount, showOrderNote]);
  var activeTabContent = /*#__PURE__*/React.createElement(SaleDetailTemplateBActivePane, {
    activeTabKey: activeTabKey,
    context: activePaneContext,
    moduleConfig: moduleConfig,
    showOrderNote: showOrderNote,
    variant: variant,
    hideFinancialSections: twoColumnSummary
  });
  var overviewContent = showOverview ? /*#__PURE__*/React.createElement(SaleOverview, {
    className: "pisell-sale-detail-tmpl-overview",
    context: context
  }) : null;
  var handleActiveTabChange = useCallback(function (key) {
    setActiveTabKey(key);
  }, []);
  var siderContent = /*#__PURE__*/React.createElement(SaleDetailTemplateBSider, {
    items: tabs,
    activeTabKey: activeTabKey,
    onActiveTabChange: handleActiveTabChange
  });
  var footerContext = useMemo(function () {
    return {
      sales: context.sales,
      summary: context.summary
    };
  }, [context.sales.bookingTicket, context.sales.tempOrder, context.summary.summary]);
  var mainContent = /*#__PURE__*/React.createElement("section", {
    className: "pisell-sale-detail-tmpl-main"
  }, activeTabContent);
  var bodyContent = /*#__PURE__*/React.createElement(React.Fragment, null, overviewContent, /*#__PURE__*/React.createElement("main", {
    className: "pisell-sale-detail-tmpl-body"
  }, /*#__PURE__*/React.createElement("section", {
    className: "pisell-sale-detail-tmpl-workspace"
  }, siderContent, /*#__PURE__*/React.createElement("section", {
    className: "pisell-sale-detail-tmpl-right"
  }, mainContent))));
  var footerContent = /*#__PURE__*/React.createElement(SaleDetailTemplateBFooter, {
    activeTabKey: activeTabKey,
    amountContext: footerContext,
    onOrderComplete: context.onOrderComplete,
    showAmountDetails: showAmountDetails,
    showOrderActions: showOrderActions,
    printOnlyOrderActions: printOnlyOrderActions,
    amountDetailsDefaultExpanded: moduleConfig === null || moduleConfig === void 0 || (_moduleConfig$amountD = moduleConfig.amountDetails) === null || _moduleConfig$amountD === void 0 ? void 0 : _moduleConfig$amountD.defaultExpanded
  });
  // 两栏模式下，金额明细从左侧滚动内容里抽出，与 footer 一起组成右列。
  var moneyAside = /*#__PURE__*/React.createElement("aside", {
    className: "pisell-sale-detail-tmpl-money"
  }, showAmountDetails ? /*#__PURE__*/React.createElement(SummaryFinancialSections, {
    className: "pisell-sale-detail-tmpl-money__financial"
  }) : null, footerContent);
  if (isMobileBottomSheet) {
    return /*#__PURE__*/React.createElement(MobileSheetFrame, {
      className: classNames('pisell-sale-detail-tmpl-b', 'pisell-sale-detail-tmpl-b--mobile-sheet'),
      footer: footerContent
    }, bodyContent);
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-sale-detail-tmpl-b"
  }, overviewContent, /*#__PURE__*/React.createElement("main", {
    className: "pisell-sale-detail-tmpl-body"
  }, /*#__PURE__*/React.createElement("section", {
    className: "pisell-sale-detail-tmpl-workspace"
  }, siderContent, /*#__PURE__*/React.createElement("section", {
    className: classNames('pisell-sale-detail-tmpl-right', {
      'pisell-sale-detail-tmpl-right--two-col': twoColumnSummary
    })
  }, twoColumnSummary ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-sale-detail-tmpl-pair"
  }, mainContent, moneyAside) : /*#__PURE__*/React.createElement(React.Fragment, null, mainContent, footerContent)))));
}