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); }
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 _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 _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 _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; }
import React, { useMemo } from 'react';
import { CheckCircleOutlined, CloseCircleOutlined, EditOutlined, PrinterOutlined, UnorderedListOutlined } from '@ant-design/icons';
import { Iconfont, ProductCard } from '@pisell/materials';
import { buildDetailModalRoomCardData, FloorRoomCardFromData, resolveBookingIdForHeader } from "../../floorRoomCard";
import { bookingProductLineToProductCardData } from "./bookingProductToProductCard";
import { isBookingRawOrderPaid } from "./bookingDetailFromRaw";
import { isCompletedBookingRaw, orderCurrencySymbol, parseBookingProducts, resolveAppointmentStatusTag } from "./bookingDetailHostActions";
import { bookingDetailActionCellTintClass, PREFIX, stopUiClick, t } from "./bookingDetailModalConstants";
var BASE_ACTION_ITEMS = [{
  key: 'action-edit',
  icon: /*#__PURE__*/React.createElement(EditOutlined, null)
}, {
  key: 'action-cart',
  icon: /*#__PURE__*/React.createElement(Iconfont, {
    type: "pisell2-coins-hand"
  })
}, {
  key: 'action-print',
  icon: /*#__PURE__*/React.createElement(PrinterOutlined, null)
}, {
  key: 'action-list',
  icon: /*#__PURE__*/React.createElement(UnorderedListOutlined, null)
}];
function buildActionItems(status, bookingRaw) {
  var completed = isCompletedBookingRaw(bookingRaw);
  var statusAction = completed ? null : status === 'occupied' ? {
    key: 'action-complete',
    icon: /*#__PURE__*/React.createElement(Iconfont, {
      type: "pisell2-check-square-broken"
    })
  } : {
    key: 'action-start',
    icon: /*#__PURE__*/React.createElement(Iconfont, {
      type: "pisell2-send-01"
    })
  };
  return [BASE_ACTION_ITEMS[1], BASE_ACTION_ITEMS[0], {
    key: 'action-form',
    icon: /*#__PURE__*/React.createElement(Iconfont, {
      type: "pisell2-edit-05"
    })
  }].concat(_toConsumableArray(statusAction ? [statusAction] : []), _toConsumableArray(BASE_ACTION_ITEMS.slice(2)), [{
    key: 'action-close',
    icon: /*#__PURE__*/React.createElement(CloseCircleOutlined, null)
  }]);
}
/**
 * 单条 booking 单元：RoomCard + 操作栏 + 商品列表。
 */
export function BookingDetailUnit(props) {
  var bookingRaw = props.bookingRaw,
    tableRow = props.tableRow,
    isCancellingOrder = props.isCancellingOrder,
    isCompletingBooking = props.isCompletingBooking,
    getApp = props.getApp,
    onActionCellClick = props.onActionCellClick;
  var data = useMemo(function () {
    return buildDetailModalRoomCardData(bookingRaw, tableRow !== null && tableRow !== void 0 ? tableRow : null);
  }, [bookingRaw, tableRow]);
  var headerOverride = useMemo(function () {
    return resolveBookingIdForHeader(bookingRaw);
  }, [bookingRaw]);
  var statusTag = useMemo(function () {
    return resolveAppointmentStatusTag(bookingRaw);
  }, [bookingRaw]);
  var isCompleted = useMemo(function () {
    return isCompletedBookingRaw(bookingRaw);
  }, [bookingRaw]);
  var isPaid = useMemo(function () {
    return isBookingRawOrderPaid(bookingRaw);
  }, [bookingRaw]);
  var items = useMemo(function () {
    return buildActionItems(data === null || data === void 0 ? void 0 : data.status, bookingRaw);
  }, [data === null || data === void 0 ? void 0 : data.status, bookingRaw]);
  var productSources = useMemo(function () {
    return parseBookingProducts(bookingRaw).map(function (line, i) {
      return bookingProductLineToProductCardData(line, i, {
        app: getApp === null || getApp === void 0 ? void 0 : getApp()
      });
    });
  }, [bookingRaw, getApp]);
  var symbol = useMemo(function () {
    return orderCurrencySymbol(bookingRaw.order);
  }, [bookingRaw.order]);
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__booking-card")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__room-card-slot pisell-res-floor-room-card"),
    onMouseDown: stopUiClick,
    onClick: stopUiClick
  }, /*#__PURE__*/React.createElement(FloorRoomCardFromData, {
    data: data,
    shellShape: "rect",
    hideHeader: true,
    hideHeaderCapacity: true,
    headerMainFieldOverride: headerOverride,
    bodyStatusTag: statusTag
  })), /*#__PURE__*/React.createElement("div", {
    className: isCompleted ? "".concat(PREFIX, "__action-grid ").concat(PREFIX, "__action-grid--cols-3") : "".concat(PREFIX, "__action-grid"),
    role: "group",
    "aria-label": t('action-grid-aria')
  }, items.map(function (_ref) {
    var ak = _ref.key,
      icon = _ref.icon;
    var isPaidCheckout = ak === 'action-cart' && isPaid;
    var tintClass = bookingDetailActionCellTintClass(ak);
    var className = ["".concat(PREFIX, "__action-cell"), tintClass, isPaidCheckout ? "".concat(PREFIX, "__action-cell--paid") : ''].filter(Boolean).join(' ');
    var displayIcon = isPaidCheckout ? /*#__PURE__*/React.createElement(CheckCircleOutlined, null) : icon;
    var labelKey = isPaidCheckout ? 'action-paid' : ak;
    return /*#__PURE__*/React.createElement("button", {
      key: ak,
      type: "button",
      className: className,
      disabled: isPaidCheckout || ak === 'action-close' && isCancellingOrder || ak === 'action-complete' && isCompletingBooking,
      onClick: function onClick(ev) {
        return onActionCellClick(bookingRaw, ak, ev);
      }
    }, /*#__PURE__*/React.isValidElement(displayIcon) ? /*#__PURE__*/React.cloneElement(displayIcon, {
      className: "".concat(PREFIX, "__action-icon"),
      'aria-hidden': true
    }) : null, /*#__PURE__*/React.createElement("span", {
      className: "".concat(PREFIX, "__action-label")
    }, t(labelKey)));
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__products-section")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__products-list")
  }, productSources.length === 0 ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__products-empty")
  }, t('empty-items')) : productSources.map(function (ds) {
    return /*#__PURE__*/React.createElement("div", {
      key: String(ds._id),
      className: "".concat(PREFIX, "__product-card-slot")
    }, /*#__PURE__*/React.createElement(ProductCard, {
      dataSource: _objectSpread(_objectSpread({}, ds), {}, {
        symbol: symbol
      }),
      isShowImage: false,
      isShowHolder: false,
      isShowNote: false,
      isShowDelete: false,
      isShowPackageNote: false,
      isShowEditProduct: false
    }));
  }))));
}