import React, { useMemo } from 'react';
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import 'dayjs/locale/en';
import 'dayjs/locale/zh-tw';
import 'dayjs/locale/ja';
import 'dayjs/locale/pt';
import { locales } from '@pisell/utils';
import Iconfont from "../../../../iconfont";
import useEngineContext from "../../../../../hooks/useEngineContext";
import "../../../cartSkuCard/components/timeRange/index.less";
var localeTypes = {
  'en-US': 'en',
  'zh-CN': 'zh-cn',
  'zh-HK': 'zh-tw',
  ja: 'ja',
  pt: 'pt'
};
var PREFIX = 'pisell-cart-sku-card';
var formatMultiDayDate = function formatMultiDayDate(start, end, isEn) {
  var format = isEn ? 'ddd, DD/MM/YYYY' : 'YYYY-MM-DD，ddd';
  return "".concat(start.format(format), " ~ ").concat(end.format(format));
};
var formatBookingTime = function formatBookingTime(booking, locale) {
  var start = dayjs(booking !== null && booking !== void 0 && booking.start_time ? "".concat((booking === null || booking === void 0 ? void 0 : booking.start_date) || '', " ").concat(booking.start_time) : booking === null || booking === void 0 ? void 0 : booking.start_date);
  var end = dayjs(booking !== null && booking !== void 0 && booking.end_time ? "".concat((booking === null || booking === void 0 ? void 0 : booking.end_date) || '', " ").concat(booking.end_time) : booking === null || booking === void 0 ? void 0 : booking.end_date);
  if (!start.isValid() || !end.isValid()) return '';
  var isEn = locale === 'en-US' || locale === 'en';
  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 _duration = end.diff(start, 'day');
    return "".concat(formatMultiDayDate(start, end, isEn), " (").concat(_duration).concat(locales.getText('pisell2.product.card.day')(_duration), ")");
  }
  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 _duration2 = end.diff(start, 'minute');
    var hours = Math.floor(_duration2 / 60);
    var minutes = _duration2 % 60;
    return "".concat(dateTime, " (").concat(locales.getText('pisell2.product.card.hour.minute')(hours, minutes), ")");
  }
  var duration = end.diff(start, durationType);
  return "".concat(dateTime, " (").concat(duration, ")");
};
var LineItemTimeRange = function LineItemTimeRange(props) {
  var _context$appHelper$ut, _context$appHelper$ut2, _context$appHelper$ut3, _context$appHelper$ut4;
  var _props$dataSource = props.dataSource,
    dataSource = _props$dataSource === void 0 ? {} : _props$dataSource;
  var context = useEngineContext();
  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')) || (dataSource === null || dataSource === void 0 ? void 0 : dataSource.locale) || 'en-US';
  var serviceTime = useMemo(function () {
    dayjs.locale(localeTypes[locale]);
    return formatBookingTime(dataSource, locale);
  }, [dataSource, locale]);
  if (!serviceTime) return null;
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__time-range")
  }, /*#__PURE__*/React.createElement(Iconfont, {
    className: "time-icon",
    type: "pisell2-clock"
  }), serviceTime);
};
export default LineItemTimeRange;