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_iconfont = __toESM(require("../../iconfont"));
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var PREFIX = "pisell-line-item";
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 formatBookingTime = (booking, locale) => {
  const start = (0, import_dayjs.default)(`${booking.start_date || ""} ${booking.start_time || ""}`);
  const end = (0, import_dayjs.default)(`${booking.end_date || ""} ${booking.end_time || ""}`);
  if (!start.isValid() || !end.isValid()) return "";
  const isEn = locale === "en-US" || locale === "en";
  const dateFormat = isEn ? "DD/MM/YYYY" : "YYYY-MM-DD";
  const startTime = booking.start_time || start.format("HH:mm");
  const endTime = booking.end_time || end.format("HH:mm");
  if (start.isSame(end, "day")) {
    return isEn ? `${startTime} ~ ${endTime} ${start.format(dateFormat)}` : `${start.format(dateFormat)} ${startTime} ~ ${endTime}`;
  }
  return isEn ? `${startTime} ${start.format(dateFormat)} ~ ${endTime} ${end.format(
    dateFormat
  )}` : `${start.format(dateFormat)} ${startTime} ~ ${end.format(
    dateFormat
  )} ${endTime}`;
};
var translateText = (text, translationOriginal) => {
  if (typeof text === "string") return text;
  if (!text) return "";
  return translationOriginal(text);
};
var getResourceName = (booking, translationOriginal) => {
  if (booking == null ? void 0 : booking.relation_form_name) return booking.relation_form_name;
  return ((booking == null ? void 0 : booking.resources) || []).map(
    (resource) => {
      var _a;
      return translateText((_a = resource == null ? void 0 : resource.metadata) == null ? void 0 : _a.resource_name, translationOriginal);
    }
  ).filter(Boolean).join(", ");
};
var BookingLineItem = (props) => {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  const {
    dataSource,
    type,
    cartSkuType,
    relatedProductDataSource,
    rightActions = [],
    escapeDom,
    isShowImage,
    isShowDelete,
    isShowNote,
    isShowAmountFooter,
    disabledClick,
    disabledEdit,
    statusOptions = [],
    statusLoading = false,
    statusDisabled = false,
    isShowRelatedProduct = true,
    onBookingStatusChange,
    onAction,
    onDelete,
    onNote,
    onCard,
    onPromotion,
    onGift,
    ...other
  } = props;
  const context = (0, import_useEngineContext.default)();
  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 bookingTitle = String((dataSource == null ? void 0 : dataSource.booking_id) || "-");
  const bookingTime = formatBookingTime(dataSource, locale);
  const resourceName = getResourceName(dataSource, translationOriginal) || "-";
  const renderStatusButton = () => /* @__PURE__ */ import_react.default.createElement(
    import_antd.Button,
    {
      className: `${PREFIX}__status`,
      htmlType: "button",
      disabled: !canChangeStatus,
      loading: statusLoading,
      onClick: (e) => {
        e.preventDefault();
        e.stopPropagation();
      }
    },
    /* @__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;
  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);
        }
      },
      /* @__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(), isShowDelete ? /* @__PURE__ */ import_react.default.createElement(
          "button",
          {
            className: `${PREFIX}__delete-btn`,
            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))
      ),
      /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__booking-detail` }, /* @__PURE__ */ import_react.default.createElement(import_Resources.default, { dataSource }), /* @__PURE__ */ import_react.default.createElement(import_TimeRange.default, { dataSource })),
      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` }, /* @__PURE__ */ import_react.default.createElement(
        import_basicInfo.default,
        {
          isShowImage,
          dataSource: relatedProductDataSource,
          type: cartSkuType,
          maxSpecsCount: props.maxSpecsCount || 1
        }
      ), /* @__PURE__ */ import_react.default.createElement(
        import_packages.default,
        {
          dataSource: relatedProductDataSource,
          isShowImage,
          type: cartSkuType
        }
      ), isShowAmountFooter ? /* @__PURE__ */ import_react.default.createElement(
        import_AmountFooter.default,
        {
          item: relatedProductDataSource,
          hideDivider: false,
          type: "total"
        }
      ) : null))) : null,
      renderPromotion,
      isShowNote ? /* @__PURE__ */ import_react.default.createElement(
        import_Note.default,
        {
          ...other,
          item: dataSource,
          disabledEdit,
          openNote: () => onNote == null ? void 0 : onNote(dataSource)
        }
      ) : null,
      isShowAction && actionText ? /* @__PURE__ */ import_react.default.createElement(import_Action.default, { item: dataSource, onAction: () => onAction == null ? void 0 : onAction(dataSource) }) : null
    )
  ), /* @__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;
