function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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 _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
export var toDate = function toDate(value) {
  var fallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date();
  if (!value) return fallback;
  if (value instanceof Date) return value;
  var parsed = new Date(value);
  return Number.isNaN(parsed.getTime()) ? fallback : parsed;
};
export var addMinutes = function addMinutes(date, minutes) {
  return new Date(date.getTime() + minutes * 60 * 1000);
};
export var minutesBetween = function minutesBetween(start, end) {
  return Math.max(0, Math.ceil((end.getTime() - start.getTime()) / 60000));
};

/** 对齐到当前展示分钟，避免当前时间显示 14:14 时派生时长按 14:15 计算。 */
export var resolveBillableMinuteDate = function resolveBillableMinuteDate(date) {
  var minute = new Date(date);
  minute.setSeconds(0, 0);
  return minute;
};
export var getDelayToNextBillableMinuteChange = function getDelayToNextBillableMinuteChange() {
  var date = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Date();
  if (date.getSeconds() === 0 && date.getMilliseconds() === 0) {
    return 60 * 1000;
  }
  var nextMinute = new Date(date);
  nextMinute.setMinutes(nextMinute.getMinutes() + 1, 0, 0);
  return Math.max(100, nextMinute.getTime() - date.getTime());
};
var pad = function pad(value) {
  return "".concat(value).padStart(2, '0');
};
export var formatTime = function formatTime(date) {
  return "".concat(pad(date.getHours()), ":").concat(pad(date.getMinutes()));
};

/** 带秒数的时钟展示（Compare 区 Current time） */
export var formatClockTime = function formatClockTime(date) {
  return "".concat(formatTime(date), ":").concat(pad(date.getSeconds()));
};
export var formatDate = function formatDate(date) {
  return "".concat(date.getDate(), "/").concat(date.getMonth() + 1, "/").concat(date.getFullYear());
};
export var formatRange = function formatRange(start, end) {
  return "".concat(formatTime(start), " ").concat(formatDate(start), "-").concat(formatTime(end), " ").concat(formatDate(end));
};
export var formatDuration = function formatDuration(minutes) {
  var total = Math.max(0, Math.ceil(minutes));
  var days = Math.floor(total / 1440);
  var hours = Math.floor(total % 1440 / 60);
  var mins = total % 60;
  var parts = [];
  if (days) parts.push("".concat(days, " ").concat(days === 1 ? 'day' : 'days'));
  if (hours) parts.push("".concat(hours, " ").concat(hours === 1 ? 'hour' : 'hours'));
  if (mins || !parts.length) parts.push("".concat(mins, " min"));
  return parts.join(' ');
};

/** 将 HH:mm 应用到指定日期的本地时间 */
export var applyTimeToDate = function applyTimeToDate(baseDate, timeStr) {
  var _timeStr$split$map = timeStr.split(':').map(function (part) {
      return Number(part) || 0;
    }),
    _timeStr$split$map2 = _slicedToArray(_timeStr$split$map, 2),
    _timeStr$split$map2$ = _timeStr$split$map2[0],
    hours = _timeStr$split$map2$ === void 0 ? 0 : _timeStr$split$map2$,
    _timeStr$split$map2$2 = _timeStr$split$map2[1],
    minutes = _timeStr$split$map2$2 === void 0 ? 0 : _timeStr$split$map2$2;
  var result = new Date(baseDate);
  result.setHours(hours, minutes, 0, 0);
  return result;
};

/** 读取店铺营业结束时刻，与 booking getShopOpeningHours 一致 */
export var getShopClosingTime = function getShopClosingTime() {
  if (typeof localStorage !== 'undefined') {
    return localStorage.getItem('shopOpeningHours') || '23:59';
  }
  return '23:59';
};

/**
 * 解析预约所属营业日的结束时间；加时暂时只受店铺营业结束限制。
 * @param bookingStart 预约开始时间（决定营业日）
 */
export var resolveFlexibleBusinessEnd = function resolveFlexibleBusinessEnd(bookingStart, _resourceEndTime, shopClosingTime) {
  var shopClose = shopClosingTime || getShopClosingTime();
  var businessEnd = applyTimeToDate(bookingStart, shopClose);
  if (bookingStart.getTime() > businessEnd.getTime()) {
    businessEnd = addMinutes(businessEnd, 24 * 60);
  }
  return businessEnd;
};

/**
 * Pay overtime：只补付从原结束时间到当前时间的超时费用。
 */
export var resolvePayOvertimeEnd = function resolvePayOvertimeEnd(current, _resourceEndTime, _shopClosingTime) {
  return current;
};

/** unlimited 加时商品：从预约结束延至营业结束 */
export var resolveUnlimitedExtension = function resolveUnlimitedExtension(bookingStart, bookingEnd, resourceEndTime, shopClosingTime) {
  var newEnd = resolveFlexibleBusinessEnd(bookingStart, resourceEndTime, shopClosingTime);
  var targetMinutes = minutesBetween(bookingEnd, newEnd);
  return {
    newEnd: newEnd,
    targetMinutes: targetMinutes
  };
};

/**
 * 根据参考日与手动选择的结束时刻，解析新的结束时间。
 * 若所选时刻不晚于 notBefore，则顺延至下一日同一时刻。
 */
export var resolveManualEndDate = function resolveManualEndDate(referenceDate, timeStr, notBefore) {
  var candidate = applyTimeToDate(referenceDate, timeStr);
  if (candidate.getTime() <= notBefore.getTime()) {
    return addMinutes(candidate, 24 * 60);
  }
  return candidate;
};

/** Manual Add time 默认结束时刻：基准结束时间 + offsetMinutes */
export var getDefaultManualEndTime = function getDefaultManualEndTime(baseEnd) {
  var offsetMinutes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 90;
  return formatTime(addMinutes(baseEnd, offsetMinutes));
};