import dayjs from 'dayjs';
import { locales } from '@pisell/utils';
import 'dayjs/locale/zh-cn';
import 'dayjs/locale/en';
import 'dayjs/locale/zh-tw';
import 'dayjs/locale/ja';
import 'dayjs/locale/pt';
var LOCALE_TYPES = {
  'en-US': 'en',
  'zh-CN': 'zh-cn',
  'zh-HK': 'zh-tw',
  ja: 'ja',
  pt: 'pt'
};
var getLocaleConfig = function getLocaleConfig() {
  var locale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'en-US';
  dayjs.locale(LOCALE_TYPES[locale] || LOCALE_TYPES['en-US']);
  return {
    isEn: locale === 'en-US' || locale === 'en'
  };
};
var getBookingDateTime = function getBookingDateTime(date, time) {
  if (!date) return dayjs('');
  return dayjs(time ? "".concat(String(date), " ").concat(String(time)) : String(date));
};
var getDuration = function getDuration(booking, start, end, unit) {
  var duration = Number(booking === null || booking === void 0 ? void 0 : booking.duration);
  return Number.isFinite(duration) && duration > 0 ? duration : end.diff(start, unit);
};
var callLocaleText = function callLocaleText(key) {
  var text = locales.getText(key);
  for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
    args[_key - 1] = arguments[_key];
  }
  return typeof text === 'function' ? text.apply(void 0, args) : text;
};

/**
 * 格式化 bigSale CDS 使用的预约时间。
 *
 * 结构参考 ticketBooking 的 formatTime：同日展示「日期 + 开始结束时间」，
 * 跨日展示「开始日期时间 ~ 结束日期时间」，并根据预约类型追加时长。
 */
export var formatBigSaleBookingTime = function formatBigSaleBookingTime(booking, locale) {
  var start = getBookingDateTime(booking === null || booking === void 0 ? void 0 : booking.start_date, booking === null || booking === void 0 ? void 0 : booking.start_time);
  var end = getBookingDateTime(booking === null || booking === void 0 ? void 0 : booking.end_date, booking === null || booking === void 0 ? void 0 : booking.end_time);
  if (!start.isValid() || !end.isValid()) return '';
  var _getLocaleConfig = getLocaleConfig(locale),
    isEn = _getLocaleConfig.isEn;
  var durationType = (booking === null || booking === void 0 ? void 0 : booking.sub_type) || (booking === null || booking === void 0 ? void 0 : booking.duration_type);
  var startTime = (booking === null || booking === void 0 ? void 0 : booking.start_time) || start.format('HH:mm');
  var endTime = (booking === null || booking === void 0 ? void 0 : booking.end_time) || end.format('HH:mm');
  if (durationType === 'days' && !start.isSame(end, 'day')) {
    var format = isEn ? 'ddd, DD/MM/YYYY' : 'YYYY-MM-DD，ddd';
    var duration = getDuration(booking, start, end, 'day');
    var durationText = "".concat(duration).concat(callLocaleText('pisell2.big-sale.day', duration));
    return "".concat(start.format(format), " ~ ").concat(end.format(format), " (").concat(durationText, ")");
  }
  var dateFormat = isEn ? 'DD/MM/YYYY' : 'YYYY-MM-DD';
  var dateTime = start.isSame(end, 'day') ? isEn ? "".concat(startTime, " ~ ").concat(endTime, " ").concat(start.format(dateFormat)) : "".concat(start.format(dateFormat), " ").concat(startTime, " ~ ").concat(endTime) : 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);
  if (!durationType) return dateTime;
  if (durationType === 'minutes' || durationType === 'flexible') {
    var _duration = getDuration(booking, start, end, 'minute');
    var hours = Math.floor(_duration / 60);
    var minutes = _duration % 60;
    return "".concat(dateTime, " (").concat(callLocaleText('pisell2.big-sale.hour-minute', hours, minutes), ")");
  }
  return "".concat(dateTime, " (").concat(end.diff(start, durationType), ")");
};