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/components/productCard/lineItem/BookingLineItem.tsx
var BookingLineItem_exports = {};
__export(BookingLineItem_exports, {
  default: () => BookingLineItem_default
});
module.exports = __toCommonJS(BookingLineItem_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_antd = require("antd");
var import_icons = require("@ant-design/icons");
var import_dayjs = __toESM(require("dayjs"));
var import_utils = require("@pisell/utils");
var import_basicInfo = __toESM(require("../cartSkuCard/components/basicInfo"));
var import_packages = __toESM(require("../cartSkuCard/components/packages"));
var import_AmountFooter = __toESM(require("../cartSkuCard/components/AmountFooter"));
var import_Note = __toESM(require("../cartSkuCard/components/Note"));
var import_Promotion = __toESM(require("../cartSkuCard/components/Promotion"));
var import_Gift = __toESM(require("../cartSkuCard/components/Gift"));
var import_swipeAction = require("../cartSkuCard/components/swipeAction");
var import_Action = __toESM(require("../components/Action"));
var import_TimeRange = __toESM(require("./components/TimeRange"));
var import_Resources = __toESM(require("./components/Resources"));
var import_Holders = __toESM(require("./components/Holders"));
var import_iconfont = __toESM(require("../../iconfont"));
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var PREFIX = "pisell-line-item";
var ADD_TIME_PRODUCTS_PREVIEW_COUNT = 2;
var DEFAULT_STATUS_COLOR = "#91caff";
var getStatusOption = (status, statusOptions = []) => {
  return statusOptions.find((option) => option.key === status) || statusOptions[0] || {
    key: status || "",
    label: status || "-",
    color: DEFAULT_STATUS_COLOR
  };
};
var BookingLineItem = (props) => {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  const {
    dataSource,
    type,
    cartSkuType,
    isShowBookingHeader,
    relatedProductDataSource,
    addTimeProductDataSources = [],
    rightActions = [],
    escapeDom,
    isShowImage,
    isFormSubject,
    isShowDelete,
    isShowNote,
    isShowAmountFooter,
    disabledClick,
    disabledEdit,
    statusOptions = [],
    statusLoading = false,
    statusDisabled = false,
    isShowRelatedProduct = true,
    holderOptions,
    onBookingStatusChange,
    onAddTimeProductCard,
    onAction,
    onDelete,
    onNote,
    onCard,
    onPromotion,
    onGift,
    ...other
  } = props;
  const context = (0, import_useEngineContext.default)();
  const [isAddTimeProductsExpanded, setIsAddTimeProductsExpanded] = (0, import_react.useState)(false);
  const translationOriginal = context.appHelper.utils.translationOriginal;
  const locale = ((_e = (_d = (_c = (_b = (_a = context.appHelper.utils) == null ? void 0 : _a.getApp) == null ? void 0 : _b.call(_a)) == null ? void 0 : _c.locales) == null ? void 0 : _d.getLocale) == null ? void 0 : _e.call(_d)) || ((_g = (_f = context.appHelper.utils) == null ? void 0 : _f.storage) == null ? void 0 : _g.get("umi_locale")) || "en-US";
  const { isShowAction, actionText } = dataSource;
  const currentStatus = (0, import_react.useMemo)(
    () => getStatusOption(dataSource == null ? void 0 : dataSource.appointment_status, statusOptions),
    [dataSource == null ? void 0 : dataSource.appointment_status, statusOptions]
  );
  const statusMenuItems = (0, import_react.useMemo)(
    () => statusOptions.filter((option) => option.key !== currentStatus.key).map((option) => ({
      key: option.key,
      label: option.label,
      danger: option.danger
    })),
    [currentStatus.key, statusOptions]
  );
  const canChangeStatus = !!onBookingStatusChange && !statusDisabled && !statusLoading && !!(statusMenuItems == null ? void 0 : statusMenuItems.length);
  const deleteAction = {
    key: "delete",
    text: /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__swipe-delete` }, /* @__PURE__ */ import_react.default.createElement("span", { className: `${PREFIX}__swipe-delete-icon` }, /* @__PURE__ */ import_react.default.createElement(import_iconfont.default, { type: "pisell2-trash-01" })), /* @__PURE__ */ import_react.default.createElement("span", null, import_utils.locales.getText("pisell2.cart.sku-card.actions.delete"))),
    color: "danger",
    onClick: (e) => {
      e.preventDefault();
      e.stopPropagation();
      onDelete == null ? void 0 : onDelete(dataSource);
    }
  };
  const bookingRightActions = isShowDelete ? [deleteAction, ...rightActions] : rightActions;
  const resolvedHolderOptions = holderOptions ?? (dataSource == null ? void 0 : dataSource.holderOptions) ?? (dataSource == null ? void 0 : dataSource.holders) ?? [];
  const resolvedIsFormSubject = isFormSubject ?? (dataSource == null ? void 0 : dataSource.isFormSubject);
  const bookingTitle = String((dataSource == null ? void 0 : dataSource.booking_id) || "");
  const renderDeleteButton = (inline = false) => isShowDelete ? /* @__PURE__ */ import_react.default.createElement(
    "button",
    {
      className: (0, import_classnames.default)(`${PREFIX}__delete-btn`, {
        [`${PREFIX}__delete-btn--inline`]: inline
      }),
      type: "button",
      onClick: (e) => {
        e.preventDefault();
        e.stopPropagation();
        onDelete == null ? void 0 : onDelete(dataSource);
      }
    },
    /* @__PURE__ */ import_react.default.createElement(import_iconfont.default, { type: "pisell2-trash-01" })
  ) : null;
  const renderStatusButton = () => {
    const statusButtonProps = {
      onClick: (e) => {
        e.preventDefault();
        e.stopPropagation();
      }
    };
    return /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        className: `${PREFIX}__status`,
        htmlType: "button",
        disabled: !canChangeStatus,
        loading: statusLoading,
        ...statusButtonProps
      },
      /* @__PURE__ */ import_react.default.createElement("span", { className: `${PREFIX}__status-label` }, currentStatus.label),
      canChangeStatus ? /* @__PURE__ */ import_react.default.createElement(import_icons.DownOutlined, null) : null
    );
  };
  const renderPromotion = ((_h = dataSource == null ? void 0 : dataSource.promotions) == null ? void 0 : _h.length) ? /* @__PURE__ */ import_react.default.createElement(
    import_Promotion.default,
    {
      promotions: dataSource == null ? void 0 : dataSource.promotions,
      onClick: (item) => onPromotion == null ? void 0 : onPromotion(item)
    }
  ) : null;
  const renderProductCard = (productDataSource, source, options = {}) => {
    var _a2;
    if (!productDataSource || (productDataSource == null ? void 0 : productDataSource.isGift)) return null;
    const { showProductPromotion = true } = options;
    const productPromotion = ((_a2 = productDataSource == null ? void 0 : productDataSource.promotions) == null ? void 0 : _a2.length) ? /* @__PURE__ */ import_react.default.createElement(
      import_Promotion.default,
      {
        promotions: productDataSource.promotions,
        onClick: (item) => onPromotion == null ? void 0 : onPromotion(item)
      }
    ) : null;
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
      import_basicInfo.default,
      {
        isShowImage,
        dataSource: productDataSource,
        type: cartSkuType,
        maxSpecsCount: props.maxSpecsCount || 1
      }
    ), /* @__PURE__ */ import_react.default.createElement(
      import_packages.default,
      {
        dataSource: productDataSource,
        isShowImage,
        type: cartSkuType
      }
    ), isShowAmountFooter ? /* @__PURE__ */ import_react.default.createElement(
      import_AmountFooter.default,
      {
        item: productDataSource,
        hideDivider: false,
        type: "total"
      }
    ) : null, showProductPromotion ? productPromotion : null, isShowNote ? /* @__PURE__ */ import_react.default.createElement(
      import_Note.default,
      {
        ...other,
        item: productDataSource,
        disabledEdit,
        openNote: () => onNote == null ? void 0 : onNote(source)
      }
    ) : null);
  };
  const renderAddTimeProducts = () => {
    const displayAddTimeProducts = addTimeProductDataSources.filter(
      (item) => item && !item.isGift
    );
    if (displayAddTimeProducts.length === 0) return null;
    const shouldCollapse = displayAddTimeProducts.length > ADD_TIME_PRODUCTS_PREVIEW_COUNT;
    const visibleAddTimeProducts = isAddTimeProductsExpanded ? displayAddTimeProducts : displayAddTimeProducts.slice(0, ADD_TIME_PRODUCTS_PREVIEW_COUNT);
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(import_antd.Divider, { style: { margin: 0 } }), /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__add-time-products` }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__section-title` }, import_utils.locales.getText("pisell2.line-item.add-time-products")), visibleAddTimeProducts.map((item, index) => {
      const key = (item == null ? void 0 : item._id) ?? (item == null ? void 0 : item.id) ?? index;
      return /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: `${PREFIX}__add-time-product-card`,
          key: `${key}`,
          onClick: (e) => {
            e.preventDefault();
            e.stopPropagation();
            console.log("item xxxxxxxxxx", item);
            if (disabledClick) return;
            onAddTimeProductCard == null ? void 0 : onAddTimeProductCard((item == null ? void 0 : item._origin) ?? item);
          }
        },
        renderProductCard(item, item)
      );
    }), shouldCollapse ? /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        className: `${PREFIX}__add-time-products-toggle`,
        type: "button",
        onClick: (e) => {
          e.preventDefault();
          e.stopPropagation();
          setIsAddTimeProductsExpanded((expanded) => !expanded);
        }
      },
      import_utils.locales.getText(
        isAddTimeProductsExpanded ? "pisell2.line-item.collapse" : "pisell2.line-item.show-all"
      )
    ) : null));
  };
  if (dataSource == null ? void 0 : dataSource.isGift) return null;
  return /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}-wrapper-container` }, /* @__PURE__ */ import_react.default.createElement(
    import_swipeAction.SwipeAction,
    {
      closeOnAction: false,
      className: (0, import_classnames.default)(`${PREFIX}-wrapper`),
      rightActions: bookingRightActions,
      escapeDom
    },
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: (0, import_classnames.default)(
          PREFIX,
          `${PREFIX}--booking`,
          `${PREFIX}--${type}`
        ),
        onClick: (e) => {
          if (disabledClick) return;
          e.preventDefault();
          e.stopPropagation();
          onCard == null ? void 0 : onCard(dataSource);
        }
      },
      isShowBookingHeader ? /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: `${PREFIX}__booking-header`,
          style: {
            "--pisell-line-item-header-color": currentStatus.color || DEFAULT_STATUS_COLOR
          }
        },
        /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__booking-main` }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__booking-title` }, bookingTitle), /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__booking-actions` }, canChangeStatus ? /* @__PURE__ */ import_react.default.createElement(
          import_antd.Dropdown,
          {
            menu: {
              items: statusMenuItems,
              onClick: ({ key }) => onBookingStatusChange == null ? void 0 : onBookingStatusChange(String(key), dataSource)
            },
            trigger: ["click"]
          },
          renderStatusButton()
        ) : renderStatusButton(), renderDeleteButton()))
      ) : null,
      /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: (0, import_classnames.default)(`${PREFIX}__booking-detail`, {
            [`${PREFIX}__booking-detail--with-delete`]: !isShowBookingHeader && isShowDelete
          })
        },
        /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__booking-detail-content` }, /* @__PURE__ */ import_react.default.createElement(import_Resources.default, { dataSource }), /* @__PURE__ */ import_react.default.createElement(import_TimeRange.default, { dataSource }), resolvedIsFormSubject ? /* @__PURE__ */ import_react.default.createElement(
          import_Holders.default,
          {
            dataSource,
            holderOptions: resolvedHolderOptions,
            isFormSubject: resolvedIsFormSubject
          }
        ) : null),
        !isShowBookingHeader ? renderDeleteButton(true) : null
      ),
      isShowRelatedProduct && relatedProductDataSource ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(import_antd.Divider, { style: { margin: 0 } }), /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__related-product` }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__related-product-card` }, renderProductCard(relatedProductDataSource, dataSource, {
        showProductPromotion: false
      }), renderPromotion, isShowAction && actionText ? /* @__PURE__ */ import_react.default.createElement(
        import_Action.default,
        {
          item: dataSource,
          onAction: () => onAction == null ? void 0 : onAction(dataSource)
        }
      ) : null))) : null,
      renderAddTimeProducts()
    )
  ), /* @__PURE__ */ import_react.default.createElement(
    import_Gift.default,
    {
      ...(dataSource == null ? void 0 : dataSource.giftData) || {},
      onClick: () => onGift == null ? void 0 : onGift(dataSource)
    }
  ));
};
var BookingLineItem_default = BookingLineItem;
