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); }
import dayjs from 'dayjs';
import { resolveLocalizedText } from "../../../../salesSdk/utils/resolveLocalizedText";
import { getLocalizedDateFormat } from "../../../utils";
export var toDisplayText = function toDisplayText(value) {
  if (value === undefined || value === null) return '';
  if (typeof value === 'string' || typeof value === 'number') return String(value);
  if (_typeof(value) === 'object') {
    return resolveLocalizedText(value);
  }
  return String(value);
};
var getQuantity = function getQuantity(value) {
  var fallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  var quantity = Math.floor(Number(value));
  return Number.isFinite(quantity) && quantity > 0 ? quantity : fallback;
};
export var isBookingDataSource = function isBookingDataSource(dataSource) {
  return Object.prototype.hasOwnProperty.call((dataSource === null || dataSource === void 0 ? void 0 : dataSource._extend) || {}, 'product');
};
var getBookingTimeText = function getBookingTimeText(booking) {
  var start = dayjs(booking.start_time ? "".concat(booking.start_date || '', " ").concat(booking.start_time) : booking.start_date);
  var end = dayjs(booking.end_time ? "".concat(booking.end_date || '', " ").concat(booking.end_time) : booking.end_date);
  if (!start.isValid() || !end.isValid()) return '';
  var startTime = booking.start_time || start.format('HH:mm');
  var endTime = booking.end_time || end.format('HH:mm');
  var dateFormat = getLocalizedDateFormat();
  if (start.isSame(end, 'day')) {
    return "".concat(start.format(dateFormat), " ").concat(startTime, "-").concat(endTime);
  }
  return "".concat(start.format(dateFormat), " ").concat(startTime, "-").concat(end.format(dateFormat), " ").concat(endTime);
};
var getBookingResources = function getBookingResources(booking) {
  var resources = Array.isArray(booking.resources) ? booking.resources : [];
  return resources.map(function (resource, index) {
    var _resource$metadata, _resource$metadata2, _ref, _ref2, _resource$relation_id;
    var formName = toDisplayText((_resource$metadata = resource.metadata) === null || _resource$metadata === void 0 ? void 0 : _resource$metadata.form_name);
    var resourceName = toDisplayText((_resource$metadata2 = resource.metadata) === null || _resource$metadata2 === void 0 ? void 0 : _resource$metadata2.resource_name);
    return {
      id: String((_ref = (_ref2 = (_resource$relation_id = resource.relation_id) !== null && _resource$relation_id !== void 0 ? _resource$relation_id : resource.form_id) !== null && _ref2 !== void 0 ? _ref2 : resource.id) !== null && _ref !== void 0 ? _ref : index),
      label: resourceName || formName,
      formName: formName,
      resourceName: resourceName
    };
  }).filter(function (resource) {
    return Boolean(resource.formName || resource.resourceName);
  });
};
export var getBookingDisplay = function getBookingDisplay(booking) {
  var _booking$_extend$prod, _booking$_extend, _booking$holderOption, _ref3, _ref4, _booking$holderMaxCou, _relatedProduct$_exte, _options$isFormSubjec, _booking$appointment_, _booking$detail;
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var relatedProduct = (_booking$_extend$prod = (_booking$_extend = booking._extend) === null || _booking$_extend === void 0 ? void 0 : _booking$_extend.product) !== null && _booking$_extend$prod !== void 0 ? _booking$_extend$prod : null;
  var holderOptions = (_booking$holderOption = booking.holderOptions) !== null && _booking$holderOption !== void 0 ? _booking$holderOption : [];
  var holderMaxCount = getQuantity((_ref3 = (_ref4 = (_booking$holderMaxCou = booking.holderMaxCount) !== null && _booking$holderMaxCou !== void 0 ? _booking$holderMaxCou : relatedProduct === null || relatedProduct === void 0 || (_relatedProduct$_exte = relatedProduct._extend) === null || _relatedProduct$_exte === void 0 ? void 0 : _relatedProduct$_exte.displayQuantity) !== null && _ref4 !== void 0 ? _ref4 : relatedProduct === null || relatedProduct === void 0 ? void 0 : relatedProduct.num) !== null && _ref3 !== void 0 ? _ref3 : booking.number);
  var isFormSubject = Boolean((_options$isFormSubjec = options.isFormSubject) !== null && _options$isFormSubjec !== void 0 ? _options$isFormSubjec : booking.isFormSubject);
  var holder_id = booking.holder_id ? Array.isArray(booking.holder_id) ? booking.holder_id : [booking.holder_id] : [];
  return {
    source: booking,
    bookingId: booking.booking_id,
    status: (_booking$appointment_ = booking.appointment_status) !== null && _booking$appointment_ !== void 0 ? _booking$appointment_ : booking.status,
    timeText: getBookingTimeText(booking),
    title: relatedProduct ? toDisplayText(relatedProduct.title) : toDisplayText((_booking$detail = booking.detail) === null || _booking$detail === void 0 ? void 0 : _booking$detail.product_title),
    bookingNumber: getQuantity(booking.number),
    resources: getBookingResources(booking),
    holderOptions: holderOptions,
    holderMaxCount: holderMaxCount,
    holder_id: holder_id,
    showUnassignedHolder: isFormSubject && holder_id.length === 0,
    relatedProduct: relatedProduct
  };
};