function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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 _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 { flexibleObj, getDurationValue } from "./flexible";

/**
 * 判断商品是否为跨日预约（duration.type === 'days'）。与 info2 isMultiDayProduct 等价。
 */
export function isMultiDayProduct(product) {
  var _product$duration;
  return (product === null || product === void 0 || (_product$duration = product.duration) === null || _product$duration === void 0 ? void 0 : _product$duration.type) === 'days';
}

/**
 * 判断 cacheItem 是否为 session 商品（extension_type ∈ session_product / session_ticket）。
 */
export function isSessionProduct(cacheItem) {
  return ['session_product', 'session_ticket'].includes(cacheItem === null || cacheItem === void 0 ? void 0 : cacheItem.extension_type);
}

/** 从 catalog `cartDetailValue` 或 `_extend.other` 取 session 快照。 */
export function getSessionFromItem(item) {
  var _item$cartDetailValue, _item$cartDetailValue2, _item$_extend;
  var session = (_item$cartDetailValue = item === null || item === void 0 || (_item$cartDetailValue2 = item.cartDetailValue) === null || _item$cartDetailValue2 === void 0 ? void 0 : _item$cartDetailValue2.session) !== null && _item$cartDetailValue !== void 0 ? _item$cartDetailValue : item === null || item === void 0 || (_item$_extend = item._extend) === null || _item$_extend === void 0 || (_item$_extend = _item$_extend.other) === null || _item$_extend === void 0 ? void 0 : _item$_extend.session;
  return session && _typeof(session) === 'object' ? session : null;
}

/**
 * 将 cartDetailValue.session 写入 _extend 的起止时间（与 info2 getServiceStartTimeAndEndTime 的 session 分支一致）。
 * 日历日优先用 ctx.date，时分取自 session.start_at / end_at 或 session.start_time / end_time。
 */
export function applySessionTimesToExtend(item, extend, ctx) {
  var _extend$other$session, _extend$other, _session$start_time, _session$end_time;
  var session = (_extend$other$session = extend === null || extend === void 0 || (_extend$other = extend.other) === null || _extend$other === void 0 ? void 0 : _extend$other.session) !== null && _extend$other$session !== void 0 ? _extend$other$session : getSessionFromItem(item);
  if (!session || !isSessionProduct(item)) {
    return extend;
  }
  var baseDate = ctx !== null && ctx !== void 0 && ctx.date ? dayjs(ctx.date) : dayjs();
  var startAt = dayjs("".concat(baseDate.format('YYYY-MM-DD'), " ").concat(dayjs(session.start_at).format('HH:mm:ss')));
  var endAt = dayjs("".concat(baseDate.format('YYYY-MM-DD'), " ").concat(dayjs(session.end_at).format('HH:mm:ss')));
  var nextExtend = _objectSpread(_objectSpread({}, extend), {}, {
    other: _objectSpread(_objectSpread({}, extend.other || {}), {}, {
      session: session
    }),
    start_date: startAt,
    startDate: startAt,
    start_time: (_session$start_time = session.start_time) !== null && _session$start_time !== void 0 ? _session$start_time : dayjs(session.start_at).format('HH:mm'),
    end_date: endAt,
    endDate: endAt,
    end_time: (_session$end_time = session.end_time) !== null && _session$end_time !== void 0 ? _session$end_time : dayjs(session.end_at).format('HH:mm')
  });
  return _objectSpread(_objectSpread({}, nextExtend), {}, {
    duration: getDays({
      _extend: nextExtend
    }, 'minutes')
  });
}

/**
 * 是否应写入 booking（与 Product.utils isNormalProduct 对齐，并覆盖 session_ticket / cartDetailValue.session）。
 */
export function isBookingProduct(item) {
  var _item$schedules, _item$scheduleIds;
  if (!item) return false;
  if (item.duration) return true;
  if (item !== null && item !== void 0 && (_item$schedules = item.schedules) !== null && _item$schedules !== void 0 && _item$schedules.length) return true;
  if (item !== null && item !== void 0 && (_item$scheduleIds = item['schedule.ids']) !== null && _item$scheduleIds !== void 0 && _item$scheduleIds.length) return true;
  if (isSessionProduct(item)) return true;
  if (getSessionFromItem(item)) return true;
  return false;
}

/**
 * 处理 'now' 时间占位：'now' → 当前 HH:mm；其他原样返回。
 * 与 info2 `getServeStartTime` 等价。
 */
export function getServeStartTime(startTime) {
  if (startTime === 'now') return dayjs().format('HH:mm');
  return startTime || '';
}

/**
 * 计算商品的开始结束时间（统一为 dayjs 实例）。
 * 与 info2 `getServiceTimes` 行为等价：
 *   - 跨日商品：startDate = start_date + start_time，endDate = _extend.end_date 原样
 *   - session 商品：从 _extend.other.session 取 start_at / end_at
 *   - 普通商品：startDate + duration（含 flexible 处理）
 */
export function getServiceTimes(service) {
  var _service$_extend, _service$_extend2, _service$_extend6, _service$_extend7;
  var startDate = dayjs("".concat(dayjs(service === null || service === void 0 || (_service$_extend = service._extend) === null || _service$_extend === void 0 ? void 0 : _service$_extend.start_date).format('YYYY-MM-DD'), " ").concat(getServeStartTime(service === null || service === void 0 || (_service$_extend2 = service._extend) === null || _service$_extend2 === void 0 ? void 0 : _service$_extend2.start_time)));
  if (isMultiDayProduct(service)) {
    var _service$_extend3, _service$_extend$end_, _service$_extend4, _service$_extend5;
    var startTime = getServeStartTime(service === null || service === void 0 || (_service$_extend3 = service._extend) === null || _service$_extend3 === void 0 ? void 0 : _service$_extend3.start_time);
    var endDateRaw = (_service$_extend$end_ = service === null || service === void 0 || (_service$_extend4 = service._extend) === null || _service$_extend4 === void 0 ? void 0 : _service$_extend4.end_date) !== null && _service$_extend$end_ !== void 0 ? _service$_extend$end_ : service === null || service === void 0 || (_service$_extend5 = service._extend) === null || _service$_extend5 === void 0 ? void 0 : _service$_extend5.endDate;
    var endBase = dayjs(endDateRaw);
    // 跨日结束日保留与开始相同的时刻（如 11:24），避免 end_date 仅日期时被解析为 00:00
    var _endDate = endBase.isValid() ? dayjs("".concat(endBase.format('YYYY-MM-DD'), " ").concat(startTime)) : endBase;
    return {
      startDate: startDate,
      endDate: _endDate
    };
  }
  var session = service === null || service === void 0 || (_service$_extend6 = service._extend) === null || _service$_extend6 === void 0 || (_service$_extend6 = _service$_extend6.other) === null || _service$_extend6 === void 0 ? void 0 : _service$_extend6.session;
  if (isSessionProduct(service) && session) {
    return {
      startDate: dayjs(session.start_at),
      endDate: dayjs(session.end_at)
    };
  }
  var endDate = startDate.add(flexibleObj.getValue(service === null || service === void 0 || (_service$_extend7 = service._extend) === null || _service$_extend7 === void 0 ? void 0 : _service$_extend7.duration), 'minute');
  return {
    startDate: startDate,
    endDate: endDate
  };
}

/**
 * 跨日预约相关：把 _extend.start_date / end_date 都对齐到 dayjs startOf('day')。
 * 与 info2 `formatDayDate` 等价。
 */
export function formatDayDate(_extend) {
  return {
    start_date: dayjs(_extend === null || _extend === void 0 ? void 0 : _extend.start_date).startOf('day'),
    end_date: dayjs(_extend === null || _extend === void 0 ? void 0 : _extend.end_date).startOf('day')
  };
}

/**
 * 计算跨日预约时长：
 *   - type='days' → end - start 的天数
 *   - type='minutes' → end - start 的分钟数
 * 与 info2 `getDays` 等价。
 */
export function getDays(cacheItem, type) {
  var _cacheItem$_extend, _cacheItem$_extend2;
  if (type === 'days') {
    var _formatDayDate = formatDayDate(cacheItem._extend),
      start_date = _formatDayDate.start_date,
      end_date = _formatDayDate.end_date;
    return end_date.diff(start_date, 'day');
  }
  var startDate = dayjs(cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend = cacheItem._extend) === null || _cacheItem$_extend === void 0 ? void 0 : _cacheItem$_extend.start_date);
  var endDate = dayjs(cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend2 = cacheItem._extend) === null || _cacheItem$_extend2 === void 0 ? void 0 : _cacheItem$_extend2.end_date);
  return endDate.diff(startDate, type);
}
export { getDurationValue };