var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/saleDetail/components/LineItem/index.tsx
var LineItem_exports = {};
__export(LineItem_exports, {
  default: () => LineItem_default
});
module.exports = __toCommonJS(LineItem_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_icons = require("@ant-design/icons");
var import_utils = require("@pisell/utils");
var import_materials = require("@pisell/materials");
var import_AmountFooter = __toESM(require("./components/AmountFooter"));
var import_BookingHeader = __toESM(require("./components/BookingHeader"));
var import_BookingMeta = __toESM(require("./components/BookingMeta"));
var import_GiftRow = __toESM(require("./components/GiftRow"));
var import_LineAction = __toESM(require("./components/LineAction"));
var import_NoteRow = __toESM(require("./components/NoteRow"));
var import_ProductBasicInfo = __toESM(require("./components/ProductBasicInfo"));
var import_ProductBundleInfo = __toESM(require("./components/ProductBundleInfo"));
var import_PromotionTags = __toESM(require("./components/PromotionTags"));
var import_swipeAction = require("../../../../../../materials/src/components/productCard/cartSkuCard/components/swipeAction");
var import_constants = require("./constants");
var import_theme = require("./theme");
var import_display = require("./utils/display");
var import_render = require("./utils/render");
var import_SaleDetailAdjustTimeContext = require("../../context/SaleDetailAdjustTimeContext");
var import_index = require("./index.less");
var ADD_TIME_PRODUCTS_PREVIEW_COUNT = 2;
var DeleteButton = ({
  source,
  onDelete
}) => /* @__PURE__ */ import_react.default.createElement(
  "button",
  {
    className: `${import_constants.PREFIX}__delete-btn`,
    type: "button",
    onClick: (event) => {
      (0, import_render.stopPropagation)(event);
      onDelete == null ? void 0 : onDelete(source);
    }
  },
  /* @__PURE__ */ import_react.default.createElement(import_materials.Iconfont, { type: "pisell2-trash-01" })
);
var LineItem = (props) => {
  const {
    dataSource,
    itemType,
    type = "a9",
    theme = "dark",
    isShowBookingHeader = false,
    rightActions = [],
    escapeDom,
    statusOptions = [],
    statusLoading = false,
    statusDisabled = false,
    isFormSubject,
    isShowRelatedProduct = true,
    isShowImage = true,
    isShowDelete = false,
    isShowNote = false,
    isShowAmountFooter = false,
    disabledClick = false,
    disabledEdit = false,
    disabledBookingEdit = disabledEdit,
    disabledEditClick = false,
    isShowOriginalPrice = true,
    isShowEditProduct = false,
    maxSpecsCount = 1,
    onBookingStatusChange,
    onDelete,
    onNote,
    onCard,
    onEdit,
    onAddTime,
    showAddTime = false,
    onPromotion,
    onGift,
    onAction,
    onEditProduct,
    symbol: propSymbol
  } = props;
  const symbol = propSymbol || (dataSource == null ? void 0 : dataSource.symbol) || "$";
  const kind = itemType || ((0, import_display.isBookingDataSource)(dataSource) ? "booking" : "product");
  const themeStyle = (0, import_react.useMemo)(() => (0, import_theme.getLineItemThemeStyle)(theme), [theme]);
  const openAdjustTimeFromContext = (0, import_SaleDetailAdjustTimeContext.useSaleDetailAdjustTime)();
  const [isAddTimeProductsExpanded, setIsAddTimeProductsExpanded] = (0, import_react.useState)(false);
  const product = (0, import_react.useMemo)(
    () => kind === "product" ? dataSource : null,
    [dataSource, kind]
  );
  const booking = (0, import_react.useMemo)(
    () => kind === "booking" ? (0, import_display.getBookingDisplay)(dataSource, {
      isFormSubject
    }) : null,
    [dataSource, isFormSubject, kind]
  );
  const clickTarget = kind === "booking" ? dataSource : product ?? dataSource;
  const themeModifier = theme === "light" ? "light" : "dark";
  const swipeActions = (0, import_react.useMemo)(() => {
    const actions = rightActions.map((action, index) => ({
      ...action,
      key: action.key ?? `right-action-${index}`,
      text: action.text ?? null
    }));
    if (isShowDelete) {
      actions.unshift({
        key: "delete",
        color: "danger",
        text: /* @__PURE__ */ import_react.default.createElement("span", { className: `${import_constants.PREFIX}__swipe-delete` }, /* @__PURE__ */ import_react.default.createElement("span", { className: `${import_constants.PREFIX}__swipe-delete-icon` }, /* @__PURE__ */ import_react.default.createElement(import_materials.Iconfont, { type: "pisell2-trash-01" })), /* @__PURE__ */ import_react.default.createElement("span", null, import_utils.locales.getText("pisell2.sale-cart.line-item.delete"))),
        onClick: (event) => {
          var _a, _b;
          (_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
          (_b = event == null ? void 0 : event.stopPropagation) == null ? void 0 : _b.call(event);
          onDelete == null ? void 0 : onDelete(dataSource);
        }
      });
    }
    return actions;
  }, [dataSource, isShowDelete, onDelete, rightActions]);
  if (kind === "product" && ((product == null ? void 0 : product._isGiftPromotion) || (product == null ? void 0 : product._giftInfo))) {
    return null;
  }
  const renderProductInfo = (currentProduct, source, showDeleteButton = false) => {
    if (!currentProduct || currentProduct._isGiftPromotion || currentProduct._giftInfo) {
      return null;
    }
    const discountReason = (0, import_display.toDisplayText)(
      currentProduct.discount_reason
    );
    const actionText = (0, import_display.toDisplayText)(
      currentProduct.actionText
    );
    const basicInfo = /* @__PURE__ */ import_react.default.createElement(
      import_ProductBasicInfo.default,
      {
        product: currentProduct,
        symbol,
        type,
        isShowImage,
        isShowOriginalPrice,
        isShowEditProduct,
        disabledEditClick,
        maxSpecsCount,
        onEditProduct
      }
    );
    const promotion = /* @__PURE__ */ import_react.default.createElement(import_PromotionTags.default, { product: currentProduct, onPromotion });
    const note = isShowNote ? /* @__PURE__ */ import_react.default.createElement(
      import_NoteRow.default,
      {
        note: currentProduct.note,
        disabledEdit,
        onClick: () => onNote == null ? void 0 : onNote(source)
      }
    ) : null;
    const lineAction = currentProduct.isShowAction && actionText ? /* @__PURE__ */ import_react.default.createElement(
      import_LineAction.default,
      {
        text: actionText,
        onAction: () => onAction == null ? void 0 : onAction(source)
      }
    ) : null;
    if (type === "a2") {
      return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, basicInfo, discountReason ? /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_constants.PREFIX}__discount-reason` }, discountReason) : null, promotion, note, /* @__PURE__ */ import_react.default.createElement(import_GiftRow.default, { product: currentProduct, onGift }));
    }
    if (type === "a5") {
      return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, showDeleteButton ? /* @__PURE__ */ import_react.default.createElement(DeleteButton, { source, onDelete }) : null, basicInfo, /* @__PURE__ */ import_react.default.createElement(
        import_ProductBundleInfo.default,
        {
          product: currentProduct,
          symbol,
          type,
          isShowImage
        }
      ), isShowAmountFooter ? /* @__PURE__ */ import_react.default.createElement(import_AmountFooter.default, { product: currentProduct, symbol }) : null, promotion, note, lineAction, /* @__PURE__ */ import_react.default.createElement(import_GiftRow.default, { product: currentProduct, onGift }));
    }
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, showDeleteButton ? /* @__PURE__ */ import_react.default.createElement(DeleteButton, { source, onDelete }) : null, basicInfo, /* @__PURE__ */ import_react.default.createElement(
      import_ProductBundleInfo.default,
      {
        product: currentProduct,
        symbol,
        type,
        isShowImage
      }
    ), isShowAmountFooter ? /* @__PURE__ */ import_react.default.createElement(import_AmountFooter.default, { product: currentProduct, symbol }) : null, discountReason ? /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_constants.PREFIX}__discount-reason` }, discountReason) : null, promotion, note, /* @__PURE__ */ import_react.default.createElement(import_GiftRow.default, { product: currentProduct, onGift }));
  };
  const renderBookingMeta = () => {
    if (kind !== "booking" || !booking) return null;
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, isShowBookingHeader ? /* @__PURE__ */ import_react.default.createElement(
      import_BookingHeader.default,
      {
        booking,
        source: dataSource,
        statusOptions,
        statusLoading,
        statusDisabled,
        isShowDelete,
        onBookingStatusChange,
        onDelete
      }
    ) : null, /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_constants.PREFIX}__booking-body` }, !isShowBookingHeader && booking.timeText ? /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_constants.PREFIX}__booking-inline-time` }, /* @__PURE__ */ import_react.default.createElement(import_icons.ClockCircleOutlined, null), booking.timeText) : null, /* @__PURE__ */ import_react.default.createElement(import_BookingMeta.default, { booking })));
  };
  const renderBookingEditAction = () => {
    if (kind !== "booking") return null;
    const showEdit = Boolean(onEdit) && !disabledBookingEdit;
    const handleAddTime = onAddTime ?? openAdjustTimeFromContext;
    const showAddTimeButton = showAddTime && Boolean(handleAddTime);
    if (!showEdit && !showAddTimeButton) return null;
    return /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_constants.PREFIX}__booking-edit-action` }, showEdit ? /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        className: `${import_constants.PREFIX}__booking-edit-btn`,
        type: "button",
        onClick: (event) => {
          (0, import_render.stopPropagation)(event);
          onEdit == null ? void 0 : onEdit(dataSource);
        }
      },
      import_utils.locales.getText("pisell2.sale-cart.line-item.edit-booking")
    ) : null, showAddTimeButton ? /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        className: `${import_constants.PREFIX}__booking-add-time-btn`,
        type: "button",
        onClick: (event) => {
          (0, import_render.stopPropagation)(event);
          handleAddTime == null ? void 0 : handleAddTime(dataSource);
        }
      },
      /* @__PURE__ */ import_react.default.createElement(import_icons.HistoryOutlined, { className: `${import_constants.PREFIX}__booking-add-time-icon`, "aria-hidden": true }),
      import_utils.locales.getText("pisell2.sale-cart.line-item.add-time")
    ) : null);
  };
  const renderBookingRelatedProduct = () => {
    if (!isShowRelatedProduct || !(booking == null ? void 0 : booking.relatedProduct)) return null;
    return /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_constants.PREFIX}__related-product` }, renderProductInfo(booking.relatedProduct, booking.relatedProduct));
  };
  const renderAddTimeProducts = () => {
    var _a;
    if (kind !== "booking") return null;
    const currentBooking = dataSource;
    const addTimeProducts = Array.isArray((_a = currentBooking._extend) == null ? void 0 : _a.addTimeProduct) ? currentBooking._extend.addTimeProduct : [];
    if (addTimeProducts.length === 0) return null;
    const shouldCollapse = addTimeProducts.length > ADD_TIME_PRODUCTS_PREVIEW_COUNT;
    const visibleAddTimeProducts = isAddTimeProductsExpanded ? addTimeProducts : addTimeProducts.slice(0, ADD_TIME_PRODUCTS_PREVIEW_COUNT);
    return /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_constants.PREFIX}__add-time-products` }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_constants.PREFIX}__add-time-products-title` }, import_utils.locales.getText("pisell2.sale-cart.line-item.add-time-products")), visibleAddTimeProducts.map((item, index) => {
      var _a2, _b;
      const productInfo = renderProductInfo(item, item);
      if (!productInfo) return null;
      const itemRecord = item;
      const key = ((_a2 = itemRecord._identity) == null ? void 0 : _a2.line_id) ?? ((_b = itemRecord._identity) == null ? void 0 : _b.id) ?? itemRecord.id ?? index;
      return /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_constants.PREFIX}__add-time-product-info`, key: `${key}` }, productInfo);
    }), shouldCollapse ? /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        className: `${import_constants.PREFIX}__add-time-products-show-all`,
        type: "button",
        onClick: (event) => {
          (0, import_render.stopPropagation)(event);
          setIsAddTimeProductsExpanded((expanded) => !expanded);
        }
      },
      import_utils.locales.getText(
        isAddTimeProductsExpanded ? "pisell2.sale-cart.line-item.collapse" : "pisell2.sale-cart.line-item.show-all"
      )
    ) : null);
  };
  const content = /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)(
        import_constants.PREFIX,
        `${import_constants.PREFIX}--${type}`,
        `${import_constants.PREFIX}--${kind}`,
        `${import_constants.PREFIX}--theme-${themeModifier}`
      ),
      onClick: (event) => {
        if (disabledClick || !onCard) return;
        (0, import_render.stopPropagation)(event);
        onCard(clickTarget);
      }
    },
    renderBookingMeta(),
    kind === "booking" ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, renderBookingRelatedProduct(), renderAddTimeProducts()) : renderProductInfo(product, dataSource, isShowDelete),
    renderBookingEditAction()
  );
  if (swipeActions.length === 0) {
    return /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_constants.PREFIX}-swipe`, style: themeStyle }, content);
  }
  return /* @__PURE__ */ import_react.default.createElement(
    import_swipeAction.SwipeAction,
    {
      className: `${import_constants.PREFIX}-swipe`,
      rightActions: swipeActions,
      closeOnAction: false,
      escapeDom,
      style: themeStyle
    },
    content
  );
};
var LineItem_default = LineItem;
