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 _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, { useMemo, useState } from 'react';
import classNames from 'classnames';
import { ClockCircleOutlined, HistoryOutlined } from '@ant-design/icons';
import { locales } from '@pisell/utils';
import { Iconfont } from '@pisell/materials';
import AmountFooter from "./components/AmountFooter";
import BookingHeader from "./components/BookingHeader";
import BookingMeta from "./components/BookingMeta";
import GiftRow from "./components/GiftRow";
import LineAction from "./components/LineAction";
import NoteRow from "./components/NoteRow";
import ProductBasicInfo from "./components/ProductBasicInfo";
import ProductBundleInfo from "./components/ProductBundleInfo";
import PromotionTags from "./components/PromotionTags";
import { SwipeAction } from "../../../../../../materials/src/components/productCard/cartSkuCard/components/swipeAction";
import { PREFIX } from "./constants";
import { getLineItemThemeStyle } from "./theme";
import { getBookingDisplay, isBookingDataSource, toDisplayText } from "./utils/display";
import { stopPropagation } from "./utils/render";
import { useSaleDetailAdjustTime } from "../../context/SaleDetailAdjustTimeContext";
import "./index.less";
var ADD_TIME_PRODUCTS_PREVIEW_COUNT = 2;
var DeleteButton = function DeleteButton(_ref) {
  var source = _ref.source,
    onDelete = _ref.onDelete;
  return /*#__PURE__*/React.createElement("button", {
    className: "".concat(PREFIX, "__delete-btn"),
    type: "button",
    onClick: function onClick(event) {
      stopPropagation(event);
      onDelete === null || onDelete === void 0 || onDelete(source);
    }
  }, /*#__PURE__*/React.createElement(Iconfont, {
    type: "pisell2-trash-01"
  }));
};
var LineItem = function LineItem(props) {
  var dataSource = props.dataSource,
    itemType = props.itemType,
    _props$type = props.type,
    type = _props$type === void 0 ? 'a9' : _props$type,
    _props$theme = props.theme,
    theme = _props$theme === void 0 ? 'dark' : _props$theme,
    _props$isShowBookingH = props.isShowBookingHeader,
    isShowBookingHeader = _props$isShowBookingH === void 0 ? false : _props$isShowBookingH,
    _props$rightActions = props.rightActions,
    rightActions = _props$rightActions === void 0 ? [] : _props$rightActions,
    escapeDom = props.escapeDom,
    _props$statusOptions = props.statusOptions,
    statusOptions = _props$statusOptions === void 0 ? [] : _props$statusOptions,
    _props$statusLoading = props.statusLoading,
    statusLoading = _props$statusLoading === void 0 ? false : _props$statusLoading,
    _props$statusDisabled = props.statusDisabled,
    statusDisabled = _props$statusDisabled === void 0 ? false : _props$statusDisabled,
    isFormSubject = props.isFormSubject,
    _props$isShowRelatedP = props.isShowRelatedProduct,
    isShowRelatedProduct = _props$isShowRelatedP === void 0 ? true : _props$isShowRelatedP,
    _props$isShowImage = props.isShowImage,
    isShowImage = _props$isShowImage === void 0 ? true : _props$isShowImage,
    _props$isShowDelete = props.isShowDelete,
    isShowDelete = _props$isShowDelete === void 0 ? false : _props$isShowDelete,
    _props$isShowNote = props.isShowNote,
    isShowNote = _props$isShowNote === void 0 ? false : _props$isShowNote,
    _props$isShowAmountFo = props.isShowAmountFooter,
    isShowAmountFooter = _props$isShowAmountFo === void 0 ? false : _props$isShowAmountFo,
    _props$disabledClick = props.disabledClick,
    disabledClick = _props$disabledClick === void 0 ? false : _props$disabledClick,
    _props$disabledEdit = props.disabledEdit,
    disabledEdit = _props$disabledEdit === void 0 ? false : _props$disabledEdit,
    _props$disabledBookin = props.disabledBookingEdit,
    disabledBookingEdit = _props$disabledBookin === void 0 ? disabledEdit : _props$disabledBookin,
    _props$disabledEditCl = props.disabledEditClick,
    disabledEditClick = _props$disabledEditCl === void 0 ? false : _props$disabledEditCl,
    _props$isShowOriginal = props.isShowOriginalPrice,
    isShowOriginalPrice = _props$isShowOriginal === void 0 ? true : _props$isShowOriginal,
    _props$isShowEditProd = props.isShowEditProduct,
    isShowEditProduct = _props$isShowEditProd === void 0 ? false : _props$isShowEditProd,
    _props$maxSpecsCount = props.maxSpecsCount,
    maxSpecsCount = _props$maxSpecsCount === void 0 ? 1 : _props$maxSpecsCount,
    onBookingStatusChange = props.onBookingStatusChange,
    onDelete = props.onDelete,
    onNote = props.onNote,
    onCard = props.onCard,
    onEdit = props.onEdit,
    onAddTime = props.onAddTime,
    _props$showAddTime = props.showAddTime,
    showAddTime = _props$showAddTime === void 0 ? false : _props$showAddTime,
    onPromotion = props.onPromotion,
    onGift = props.onGift,
    _onAction = props.onAction,
    onEditProduct = props.onEditProduct,
    propSymbol = props.symbol;
  var symbol = propSymbol || (dataSource === null || dataSource === void 0 ? void 0 : dataSource.symbol) || '$';
  var kind = itemType || (isBookingDataSource(dataSource) ? 'booking' : 'product');
  var themeStyle = useMemo(function () {
    return getLineItemThemeStyle(theme);
  }, [theme]);
  var openAdjustTimeFromContext = useSaleDetailAdjustTime();
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isAddTimeProductsExpanded = _useState2[0],
    setIsAddTimeProductsExpanded = _useState2[1];
  var product = useMemo(function () {
    return kind === 'product' ? dataSource : null;
  }, [dataSource, kind]);
  var booking = useMemo(function () {
    return kind === 'booking' ? getBookingDisplay(dataSource, {
      isFormSubject: isFormSubject
    }) : null;
  }, [dataSource, isFormSubject, kind]);
  var clickTarget = kind === 'booking' ? dataSource : product !== null && product !== void 0 ? product : dataSource;
  var themeModifier = theme === 'light' ? 'light' : 'dark';
  var swipeActions = useMemo(function () {
    var actions = rightActions.map(function (action, index) {
      var _action$key, _action$text;
      return _objectSpread(_objectSpread({}, action), {}, {
        key: (_action$key = action.key) !== null && _action$key !== void 0 ? _action$key : "right-action-".concat(index),
        text: (_action$text = action.text) !== null && _action$text !== void 0 ? _action$text : null
      });
    });
    if (isShowDelete) {
      actions.unshift({
        key: 'delete',
        color: 'danger',
        text: /*#__PURE__*/React.createElement("span", {
          className: "".concat(PREFIX, "__swipe-delete")
        }, /*#__PURE__*/React.createElement("span", {
          className: "".concat(PREFIX, "__swipe-delete-icon")
        }, /*#__PURE__*/React.createElement(Iconfont, {
          type: "pisell2-trash-01"
        })), /*#__PURE__*/React.createElement("span", null, locales.getText('pisell2.sale-cart.line-item.delete'))),
        onClick: function onClick(event) {
          var _event$preventDefault, _event$stopPropagatio;
          event === null || event === void 0 || (_event$preventDefault = event.preventDefault) === null || _event$preventDefault === void 0 || _event$preventDefault.call(event);
          event === null || event === void 0 || (_event$stopPropagatio = event.stopPropagation) === null || _event$stopPropagatio === void 0 || _event$stopPropagatio.call(event);
          onDelete === null || onDelete === void 0 || onDelete(dataSource);
        }
      });
    }
    return actions;
  }, [dataSource, isShowDelete, onDelete, rightActions]);
  if (kind === 'product' && (product !== null && product !== void 0 && product._isGiftPromotion || product !== null && product !== void 0 && product._giftInfo)) {
    return null;
  }
  var renderProductInfo = function renderProductInfo(currentProduct, source) {
    var showDeleteButton = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
    if (!currentProduct || currentProduct._isGiftPromotion || currentProduct._giftInfo) {
      return null;
    }
    var discountReason = toDisplayText(currentProduct.discount_reason);
    var actionText = toDisplayText(currentProduct.actionText);
    var basicInfo = /*#__PURE__*/React.createElement(ProductBasicInfo, {
      product: currentProduct,
      symbol: symbol,
      type: type,
      isShowImage: isShowImage,
      isShowOriginalPrice: isShowOriginalPrice,
      isShowEditProduct: isShowEditProduct,
      disabledEditClick: disabledEditClick,
      maxSpecsCount: maxSpecsCount,
      onEditProduct: onEditProduct
    });
    var promotion = /*#__PURE__*/React.createElement(PromotionTags, {
      product: currentProduct,
      onPromotion: onPromotion
    });
    var note = isShowNote ? /*#__PURE__*/React.createElement(NoteRow, {
      note: currentProduct.note,
      disabledEdit: disabledEdit,
      onClick: function onClick() {
        return onNote === null || onNote === void 0 ? void 0 : onNote(source);
      }
    }) : null;
    var lineAction = currentProduct.isShowAction && actionText ? /*#__PURE__*/React.createElement(LineAction, {
      text: actionText,
      onAction: function onAction() {
        return _onAction === null || _onAction === void 0 ? void 0 : _onAction(source);
      }
    }) : null;
    if (type === 'a2') {
      return /*#__PURE__*/React.createElement(React.Fragment, null, basicInfo, discountReason ? /*#__PURE__*/React.createElement("div", {
        className: "".concat(PREFIX, "__discount-reason")
      }, discountReason) : null, promotion, note, /*#__PURE__*/React.createElement(GiftRow, {
        product: currentProduct,
        onGift: onGift
      }));
    }
    if (type === 'a5') {
      return /*#__PURE__*/React.createElement(React.Fragment, null, showDeleteButton ? /*#__PURE__*/React.createElement(DeleteButton, {
        source: source,
        onDelete: onDelete
      }) : null, basicInfo, /*#__PURE__*/React.createElement(ProductBundleInfo, {
        product: currentProduct,
        symbol: symbol,
        type: type,
        isShowImage: isShowImage
      }), isShowAmountFooter ? /*#__PURE__*/React.createElement(AmountFooter, {
        product: currentProduct,
        symbol: symbol
      }) : null, promotion, note, lineAction, /*#__PURE__*/React.createElement(GiftRow, {
        product: currentProduct,
        onGift: onGift
      }));
    }
    return /*#__PURE__*/React.createElement(React.Fragment, null, showDeleteButton ? /*#__PURE__*/React.createElement(DeleteButton, {
      source: source,
      onDelete: onDelete
    }) : null, basicInfo, /*#__PURE__*/React.createElement(ProductBundleInfo, {
      product: currentProduct,
      symbol: symbol,
      type: type,
      isShowImage: isShowImage
    }), isShowAmountFooter ? /*#__PURE__*/React.createElement(AmountFooter, {
      product: currentProduct,
      symbol: symbol
    }) : null, discountReason ? /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__discount-reason")
    }, discountReason) : null, promotion, note, /*#__PURE__*/React.createElement(GiftRow, {
      product: currentProduct,
      onGift: onGift
    }));
  };
  var renderBookingMeta = function renderBookingMeta() {
    if (kind !== 'booking' || !booking) return null;
    return /*#__PURE__*/React.createElement(React.Fragment, null, isShowBookingHeader ? /*#__PURE__*/React.createElement(BookingHeader, {
      booking: booking,
      source: dataSource,
      statusOptions: statusOptions,
      statusLoading: statusLoading,
      statusDisabled: statusDisabled,
      isShowDelete: isShowDelete,
      onBookingStatusChange: onBookingStatusChange,
      onDelete: onDelete
    }) : null, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__booking-body")
    }, !isShowBookingHeader && booking.timeText ? /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__booking-inline-time")
    }, /*#__PURE__*/React.createElement(ClockCircleOutlined, null), booking.timeText) : null, /*#__PURE__*/React.createElement(BookingMeta, {
      booking: booking
    })));
  };
  var renderBookingEditAction = function renderBookingEditAction() {
    if (kind !== 'booking') return null;
    var showEdit = Boolean(onEdit) && !disabledBookingEdit;
    var handleAddTime = onAddTime !== null && onAddTime !== void 0 ? onAddTime : openAdjustTimeFromContext;
    var showAddTimeButton = showAddTime && Boolean(handleAddTime);
    if (!showEdit && !showAddTimeButton) return null;
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__booking-edit-action")
    }, showEdit ? /*#__PURE__*/React.createElement("button", {
      className: "".concat(PREFIX, "__booking-edit-btn"),
      type: "button",
      onClick: function onClick(event) {
        stopPropagation(event);
        onEdit === null || onEdit === void 0 || onEdit(dataSource);
      }
    }, locales.getText('pisell2.sale-cart.line-item.edit-booking')) : null, showAddTimeButton ? /*#__PURE__*/React.createElement("button", {
      className: "".concat(PREFIX, "__booking-add-time-btn"),
      type: "button",
      onClick: function onClick(event) {
        stopPropagation(event);
        handleAddTime === null || handleAddTime === void 0 || handleAddTime(dataSource);
      }
    }, /*#__PURE__*/React.createElement(HistoryOutlined, {
      className: "".concat(PREFIX, "__booking-add-time-icon"),
      "aria-hidden": true
    }), locales.getText('pisell2.sale-cart.line-item.add-time')) : null);
  };
  var renderBookingRelatedProduct = function renderBookingRelatedProduct() {
    if (!isShowRelatedProduct || !(booking !== null && booking !== void 0 && booking.relatedProduct)) return null;
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__related-product")
    }, renderProductInfo(booking.relatedProduct, booking.relatedProduct));
  };
  var renderAddTimeProducts = function renderAddTimeProducts() {
    var _currentBooking$_exte;
    if (kind !== 'booking') return null;
    var currentBooking = dataSource;
    var addTimeProducts = Array.isArray((_currentBooking$_exte = currentBooking._extend) === null || _currentBooking$_exte === void 0 ? void 0 : _currentBooking$_exte.addTimeProduct) ? currentBooking._extend.addTimeProduct : [];
    if (addTimeProducts.length === 0) return null;
    var shouldCollapse = addTimeProducts.length > ADD_TIME_PRODUCTS_PREVIEW_COUNT;
    var visibleAddTimeProducts = isAddTimeProductsExpanded ? addTimeProducts : addTimeProducts.slice(0, ADD_TIME_PRODUCTS_PREVIEW_COUNT);
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__add-time-products")
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__add-time-products-title")
    }, locales.getText('pisell2.sale-cart.line-item.add-time-products')), visibleAddTimeProducts.map(function (item, index) {
      var _ref2, _ref3, _itemRecord$_identity, _itemRecord$_identity2, _itemRecord$_identity3;
      var productInfo = renderProductInfo(item, item);
      if (!productInfo) return null;
      var itemRecord = item;
      var key = (_ref2 = (_ref3 = (_itemRecord$_identity = (_itemRecord$_identity2 = itemRecord._identity) === null || _itemRecord$_identity2 === void 0 ? void 0 : _itemRecord$_identity2.line_id) !== null && _itemRecord$_identity !== void 0 ? _itemRecord$_identity : (_itemRecord$_identity3 = itemRecord._identity) === null || _itemRecord$_identity3 === void 0 ? void 0 : _itemRecord$_identity3.id) !== null && _ref3 !== void 0 ? _ref3 : itemRecord.id) !== null && _ref2 !== void 0 ? _ref2 : index;
      return /*#__PURE__*/React.createElement("div", {
        className: "".concat(PREFIX, "__add-time-product-info"),
        key: "".concat(key)
      }, productInfo);
    }), shouldCollapse ? /*#__PURE__*/React.createElement("button", {
      className: "".concat(PREFIX, "__add-time-products-show-all"),
      type: "button",
      onClick: function onClick(event) {
        stopPropagation(event);
        setIsAddTimeProductsExpanded(function (expanded) {
          return !expanded;
        });
      }
    }, locales.getText(isAddTimeProductsExpanded ? 'pisell2.sale-cart.line-item.collapse' : 'pisell2.sale-cart.line-item.show-all')) : null);
  };
  var content = /*#__PURE__*/React.createElement("div", {
    className: classNames(PREFIX, "".concat(PREFIX, "--").concat(type), "".concat(PREFIX, "--").concat(kind), "".concat(PREFIX, "--theme-").concat(themeModifier)),
    onClick: function onClick(event) {
      if (disabledClick || !onCard) return;
      stopPropagation(event);
      onCard(clickTarget);
    }
  }, renderBookingMeta(), kind === 'booking' ? /*#__PURE__*/React.createElement(React.Fragment, null, renderBookingRelatedProduct(), renderAddTimeProducts()) : renderProductInfo(product, dataSource, isShowDelete), renderBookingEditAction());
  if (swipeActions.length === 0) {
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "-swipe"),
      style: themeStyle
    }, content);
  }
  return /*#__PURE__*/React.createElement(SwipeAction, {
    className: "".concat(PREFIX, "-swipe"),
    rightActions: swipeActions,
    closeOnAction: false,
    escapeDom: escapeDom,
    style: themeStyle
  }, content);
};
export default LineItem;