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); }
var _excluded = ["dataSource", "type", "cartSkuType", "isShowBookingHeader", "relatedProductDataSource", "rightActions", "escapeDom", "isShowImage", "isFormSubject", "isShowDelete", "isShowNote", "isShowAmountFooter", "disabledClick", "disabledEdit", "statusOptions", "statusLoading", "statusDisabled", "isShowRelatedProduct", "holderOptions", "onBookingStatusChange", "onAction", "onDelete", "onNote", "onCard", "onPromotion", "onGift"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useMemo } from 'react';
import classNames from 'classnames';
import { Button, Divider, Dropdown } from 'antd';
import { DownOutlined } from '@ant-design/icons';
import dayjs from 'dayjs';
import { locales } from '@pisell/utils';
import BasicInfo from "../cartSkuCard/components/basicInfo";
import Packages from "../cartSkuCard/components/packages";
import AmountFooter from "../cartSkuCard/components/AmountFooter";
import Note from "../cartSkuCard/components/Note";
import Promotion from "../cartSkuCard/components/Promotion";
import Gift from "../cartSkuCard/components/Gift";
import { SwipeAction } from "../cartSkuCard/components/swipeAction";
import Action from "../components/Action";
import TimeRange from "./components/TimeRange";
import Resources from "./components/Resources";
import Holders from "./components/Holders";
import Iconfont from "../../iconfont";
import useEngineContext from "../../../hooks/useEngineContext";
var PREFIX = 'pisell-line-item';
var DEFAULT_STATUS_COLOR = '#91caff';
var getStatusOption = function getStatusOption(status) {
  var statusOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  return statusOptions.find(function (option) {
    return option.key === status;
  }) || statusOptions[0] || {
    key: status || '',
    label: status || '-',
    color: DEFAULT_STATUS_COLOR
  };
};
var formatBookingTime = function formatBookingTime(booking, locale) {
  var start = dayjs("".concat(booking.start_date || '', " ").concat(booking.start_time || ''));
  var end = dayjs("".concat(booking.end_date || '', " ").concat(booking.end_time || ''));
  if (!start.isValid() || !end.isValid()) return '';
  var isEn = locale === 'en-US' || locale === 'en';
  var dateFormat = isEn ? 'DD/MM/YYYY' : 'YYYY-MM-DD';
  var startTime = booking.start_time || start.format('HH:mm');
  var endTime = booking.end_time || end.format('HH:mm');
  if (start.isSame(end, 'day')) {
    return isEn ? "".concat(startTime, " ~ ").concat(endTime, " ").concat(start.format(dateFormat)) : "".concat(start.format(dateFormat), " ").concat(startTime, " ~ ").concat(endTime);
  }
  return isEn ? "".concat(startTime, " ").concat(start.format(dateFormat), " ~ ").concat(endTime, " ").concat(end.format(dateFormat)) : "".concat(start.format(dateFormat), " ").concat(startTime, " ~ ").concat(end.format(dateFormat), " ").concat(endTime);
};
var translateText = function translateText(text, translationOriginal) {
  if (typeof text === 'string') return text;
  if (!text) return '';
  return translationOriginal(text);
};
var getResourceName = function getResourceName(booking, translationOriginal) {
  if (booking !== null && booking !== void 0 && booking.relation_form_name) return booking.relation_form_name;
  return ((booking === null || booking === void 0 ? void 0 : booking.resources) || []).map(function (resource) {
    var _resource$metadata;
    return translateText(resource === null || resource === void 0 || (_resource$metadata = resource.metadata) === null || _resource$metadata === void 0 ? void 0 : _resource$metadata.resource_name, translationOriginal);
  }).filter(Boolean).join(', ');
};
var BookingLineItem = function BookingLineItem(props) {
  var _context$appHelper$ut, _context$appHelper$ut2, _context$appHelper$ut3, _context$appHelper$ut4, _ref, _ref2, _dataSource$promotion;
  var dataSource = props.dataSource,
    type = props.type,
    cartSkuType = props.cartSkuType,
    isShowBookingHeader = props.isShowBookingHeader,
    relatedProductDataSource = props.relatedProductDataSource,
    _props$rightActions = props.rightActions,
    rightActions = _props$rightActions === void 0 ? [] : _props$rightActions,
    escapeDom = props.escapeDom,
    isShowImage = props.isShowImage,
    isFormSubject = props.isFormSubject,
    isShowDelete = props.isShowDelete,
    isShowNote = props.isShowNote,
    isShowAmountFooter = props.isShowAmountFooter,
    disabledClick = props.disabledClick,
    disabledEdit = props.disabledEdit,
    _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,
    _props$isShowRelatedP = props.isShowRelatedProduct,
    isShowRelatedProduct = _props$isShowRelatedP === void 0 ? true : _props$isShowRelatedP,
    holderOptions = props.holderOptions,
    onBookingStatusChange = props.onBookingStatusChange,
    _onAction = props.onAction,
    onDelete = props.onDelete,
    onNote = props.onNote,
    onCard = props.onCard,
    onPromotion = props.onPromotion,
    onGift = props.onGift,
    other = _objectWithoutProperties(props, _excluded);
  var context = useEngineContext();
  var translationOriginal = context.appHelper.utils.translationOriginal;
  var locale = ((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut.getApp) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.call(_context$appHelper$ut)) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.locales) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut2.getLocale) === null || _context$appHelper$ut3 === void 0 ? void 0 : _context$appHelper$ut3.call(_context$appHelper$ut2)) || ((_context$appHelper$ut4 = context.appHelper.utils) === null || _context$appHelper$ut4 === void 0 || (_context$appHelper$ut4 = _context$appHelper$ut4.storage) === null || _context$appHelper$ut4 === void 0 ? void 0 : _context$appHelper$ut4.get('umi_locale')) || 'en-US';
  var isShowAction = dataSource.isShowAction,
    actionText = dataSource.actionText;
  var currentStatus = useMemo(function () {
    return getStatusOption(dataSource === null || dataSource === void 0 ? void 0 : dataSource.appointment_status, statusOptions);
  }, [dataSource === null || dataSource === void 0 ? void 0 : dataSource.appointment_status, statusOptions]);
  var statusMenuItems = useMemo(function () {
    return statusOptions.filter(function (option) {
      return option.key !== currentStatus.key;
    }).map(function (option) {
      return {
        key: option.key,
        label: option.label,
        danger: option.danger
      };
    });
  }, [currentStatus.key, statusOptions]);
  var canChangeStatus = !!onBookingStatusChange && !statusDisabled && !statusLoading && !!(statusMenuItems !== null && statusMenuItems !== void 0 && statusMenuItems.length);
  var deleteAction = {
    key: 'delete',
    text: /*#__PURE__*/React.createElement("div", {
      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.cart.sku-card.actions.delete'))),
    color: 'danger',
    onClick: function onClick(e) {
      e.preventDefault();
      e.stopPropagation();
      onDelete === null || onDelete === void 0 || onDelete(dataSource);
    }
  };
  var bookingRightActions = isShowDelete ? [deleteAction].concat(_toConsumableArray(rightActions)) : rightActions;
  var resolvedHolderOptions = (_ref = (_ref2 = holderOptions !== null && holderOptions !== void 0 ? holderOptions : dataSource === null || dataSource === void 0 ? void 0 : dataSource.holderOptions) !== null && _ref2 !== void 0 ? _ref2 : dataSource === null || dataSource === void 0 ? void 0 : dataSource.holders) !== null && _ref !== void 0 ? _ref : [];
  var resolvedIsFormSubject = isFormSubject !== null && isFormSubject !== void 0 ? isFormSubject : dataSource === null || dataSource === void 0 ? void 0 : dataSource.isFormSubject;
  var bookingTitle = String((dataSource === null || dataSource === void 0 ? void 0 : dataSource.booking_id) || '');
  var renderDeleteButton = function renderDeleteButton() {
    var inline = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
    return isShowDelete ? /*#__PURE__*/React.createElement("button", {
      className: classNames("".concat(PREFIX, "__delete-btn"), _defineProperty({}, "".concat(PREFIX, "__delete-btn--inline"), inline)),
      type: "button",
      onClick: function onClick(e) {
        e.preventDefault();
        e.stopPropagation();
        onDelete === null || onDelete === void 0 || onDelete(dataSource);
      }
    }, /*#__PURE__*/React.createElement(Iconfont, {
      type: "pisell2-trash-01"
    })) : null;
  };
  var renderStatusButton = function renderStatusButton() {
    return /*#__PURE__*/React.createElement(Button, {
      className: "".concat(PREFIX, "__status"),
      htmlType: "button",
      disabled: !canChangeStatus,
      loading: statusLoading,
      onClick: function onClick(e) {
        e.preventDefault();
        e.stopPropagation();
      }
    }, /*#__PURE__*/React.createElement("span", {
      className: "".concat(PREFIX, "__status-label")
    }, currentStatus.label), canChangeStatus ? /*#__PURE__*/React.createElement(DownOutlined, null) : null);
  };
  var renderPromotion = dataSource !== null && dataSource !== void 0 && (_dataSource$promotion = dataSource.promotions) !== null && _dataSource$promotion !== void 0 && _dataSource$promotion.length ? /*#__PURE__*/React.createElement(Promotion, {
    promotions: dataSource === null || dataSource === void 0 ? void 0 : dataSource.promotions,
    onClick: function onClick(item) {
      return onPromotion === null || onPromotion === void 0 ? void 0 : onPromotion(item);
    }
  }) : null;
  if (dataSource !== null && dataSource !== void 0 && dataSource.isGift) return null;
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "-wrapper-container")
  }, /*#__PURE__*/React.createElement(SwipeAction, {
    closeOnAction: false,
    className: classNames("".concat(PREFIX, "-wrapper")),
    rightActions: bookingRightActions,
    escapeDom: escapeDom
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames(PREFIX, "".concat(PREFIX, "--booking"), "".concat(PREFIX, "--").concat(type)),
    onClick: function onClick(e) {
      if (disabledClick) return;
      e.preventDefault();
      e.stopPropagation();
      onCard === null || onCard === void 0 || onCard(dataSource);
    }
  }, isShowBookingHeader ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__booking-header"),
    style: {
      '--pisell-line-item-header-color': currentStatus.color || DEFAULT_STATUS_COLOR
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__booking-main")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__booking-title")
  }, bookingTitle), /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__booking-actions")
  }, canChangeStatus ? /*#__PURE__*/React.createElement(Dropdown, {
    menu: {
      items: statusMenuItems,
      onClick: function onClick(_ref3) {
        var key = _ref3.key;
        return onBookingStatusChange === null || onBookingStatusChange === void 0 ? void 0 : onBookingStatusChange(String(key), dataSource);
      }
    },
    trigger: ['click']
  }, renderStatusButton()) : renderStatusButton(), renderDeleteButton()))) : null, /*#__PURE__*/React.createElement("div", {
    className: classNames("".concat(PREFIX, "__booking-detail"), _defineProperty({}, "".concat(PREFIX, "__booking-detail--with-delete"), !isShowBookingHeader && isShowDelete))
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__booking-detail-content")
  }, /*#__PURE__*/React.createElement(Resources, {
    dataSource: dataSource
  }), /*#__PURE__*/React.createElement(TimeRange, {
    dataSource: dataSource
  }), resolvedIsFormSubject ? /*#__PURE__*/React.createElement(Holders, {
    dataSource: dataSource,
    holderOptions: resolvedHolderOptions,
    isFormSubject: resolvedIsFormSubject
  }) : null), !isShowBookingHeader ? renderDeleteButton(true) : null), isShowRelatedProduct && relatedProductDataSource ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Divider, {
    style: {
      margin: 0
    }
  }), /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__related-product")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__related-product-card")
  }, /*#__PURE__*/React.createElement(BasicInfo, {
    isShowImage: isShowImage,
    dataSource: relatedProductDataSource,
    type: cartSkuType,
    maxSpecsCount: props.maxSpecsCount || 1
  }), /*#__PURE__*/React.createElement(Packages, {
    dataSource: relatedProductDataSource,
    isShowImage: isShowImage,
    type: cartSkuType
  }), isShowAmountFooter ? /*#__PURE__*/React.createElement(AmountFooter, {
    item: relatedProductDataSource,
    hideDivider: false,
    type: "total"
  }) : null, renderPromotion, isShowNote ? /*#__PURE__*/React.createElement(Note, _extends({}, other, {
    item: relatedProductDataSource,
    disabledEdit: disabledEdit,
    openNote: function openNote() {
      return onNote === null || onNote === void 0 ? void 0 : onNote(dataSource);
    }
  })) : null, isShowAction && actionText ? /*#__PURE__*/React.createElement(Action, {
    item: dataSource,
    onAction: function onAction() {
      return _onAction === null || _onAction === void 0 ? void 0 : _onAction(dataSource);
    }
  }) : null))) : null)), /*#__PURE__*/React.createElement(Gift, _extends({}, (dataSource === null || dataSource === void 0 ? void 0 : dataSource.giftData) || {}, {
    onClick: function onClick() {
      return onGift === null || onGift === void 0 ? void 0 : onGift(dataSource);
    }
  })));
};
export default BookingLineItem;