function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
import dayjs from "dayjs";
import { getDays, getIsEdit, isSessionProduct } from "../utils";
import { isUsableProduct } from "../info2/service/addTimeModal/utils";
// 商品是套餐或单规格多规格
export var getIsBundleOrCombination = function getIsBundleOrCombination(productDetail) {
  var _productDetail$bundle;
  if (productDetail !== null && productDetail !== void 0 && (_productDetail$bundle = productDetail.bundle_group) !== null && _productDetail$bundle !== void 0 && _productDetail$bundle.length) {
    return true;
  }
  return false;
};
export var uniqueByField = function uniqueByField(arr, field) {
  var seen = new Set();
  return arr.filter(function (item) {
    var fieldValue = item[field];
    if (seen.has(fieldValue)) {
      return false;
    } else {
      seen.add(fieldValue);
      return true;
    }
  });
};
export var moveItemToFront = function moveItemToFront(arr, targetId) {
  var index = arr.findIndex(function (item) {
    return item.id === targetId;
  });
  if (index !== -1) {
    var targetItem = arr[index];
    arr.splice(index, 1);
    arr.unshift(targetItem);
  }
  return arr;
};

/**
 * @title:  判断两个时间是否在同一个时间段内
 * @description: 
 * @param {number} time1 开始时间
 * @param {number} time2 结束时间
 * @param {number} interval 时间间隔
 * @return {*}
 * @Author: zhiwei.Wang
 * @Date: 2024-08-13 19:37
 */
export var isInterval = function isInterval(time1, time2) {
  var interval = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1000;
  // 相同的为复制的, 不影响
  if (time2 === time1) {
    return false;
  }
  return Math.abs(time2 - time1) < interval;
};

/**
 * @title: 根据时间间隔格式化服务列表
 * @description: 防止短时间内添加多个相同服务, 如果两个服务的时间间隔小于1秒，则合并为一个服务
 * @param {any} list 服务列表
 * @return {*}
 * @Author: zhiwei.Wang
 * @Date: 2024-08-13 19:36
 */
export var formatServiceListByTime = function formatServiceListByTime(list) {
  try {
    var _list = _toConsumableArray(list);
    var length = _list.length;

    // 如果只有一个服务，则直接返回
    if (length > 1) {
      var _list2, _list3;
      var start = (_list2 = _list[length - 2]) === null || _list2 === void 0 ? void 0 : _list2._time;
      var end = (_list3 = _list[length - 1]) === null || _list3 === void 0 ? void 0 : _list3._time;

      // 如果两个服务都有time  并且在同一个时间段内，则合并为一个服务
      if (start && end && isInterval(start, end)) {
        _list.pop();
        return _list;
      } else {
        return list;
      }
    } else {
      return list;
    }
  } catch (err) {
    return list;
  }
};
export var isBooking4Shop = function isBooking4Shop(shopId, renderType, businessUtils) {
  if (renderType === 'eventBooking') {
    return false;
  }
  var booking4Shops = [8390, 1350, 1401, 3013, 7154, 7572, 7007, 8119, 7320, 7362, 6497, 6087, 6303, 6726, 8191, 6812, 6813, 6814, 7651, 8586, 8581, 9, 2155, 8030, 1749, 8194, 2327, 8609, 8610, 8782, 1285, 6124, 8727, 5538, 6988, 8872, 6930, 8824, 8913, 8918, 6413, 8943];

  // 固定的shopId 或者安装 Resource Kit 应用，则认为是booking4Shop
  return booking4Shops.includes(shopId) || (businessUtils === null || businessUtils === void 0 ? void 0 : businessUtils.getAppByModal('resourcekit'));
};
function generateDateRange(start, end) {
  var dateRange = [];
  var currentDate = dayjs(start);
  while (currentDate.isBefore(dayjs(end).add(1, "day"))) {
    dateRange.push(currentDate.format("YYYY-MM-DD"));
    currentDate = currentDate.add(1, "day");
  }
  return dateRange;
}
export var getMultiDayPrices = function getMultiDayPrices(data) {
  var result = {};
  data.forEach(function (entry) {
    var dateRange = generateDateRange(entry.start_date, entry.end_date);
    dateRange.forEach(function (date) {
      result[date] = entry;
    });
  });
  return result;
};

/**
 * @title: 是否是跨日预约
 * @description: 
 * @param {number} shopId
 * @return {*}
 * @Author: WangHan
 * @Date: 2024-11-29 11:19
 */
export var isDayBooking = function isDayBooking(shopId) {
  var dayBookingShops = [9];
  return dayBookingShops.includes(shopId);
};
var getAddTimeProducts = function getAddTimeProducts(state, currentService) {
  return (state.addTimeProducts || []).some(function (item) {
    return isUsableProduct(item.add_time, currentService);
  });
};
export var isOpenAddTime = function isOpenAddTime(state) {
  var _state$bookingConfig;
  var appointment_card = (state === null || state === void 0 || (_state$bookingConfig = state.bookingConfig) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.config) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.information_tab) === null || _state$bookingConfig === void 0 ? void 0 : _state$bookingConfig.appointment_card) || {
    quick_time: 1
  };
  // event预约不需要
  if ((state === null || state === void 0 ? void 0 : state.renderType) === 'eventBooking') {
    return false;
  }

  // 禁止编辑
  if (state !== null && state !== void 0 && state.disabledEdit) {
    return false;
  }

  // 未开启加时
  if (appointment_card.quick_time !== 1) {
    return false;
  }
  return true;
};

// 是否展示加时按钮
export var isShowAddTime = function isShowAddTime(state, currentService) {
  if (!isOpenAddTime(state)) {
    return false;
  }

  // 如果在编辑态, 不展示加时
  if (getIsEdit(state)) {
    return false;
  }
  if (!getAddTimeProducts(state, currentService)) {
    return false;
  }
  return true;
};
export var editSessionProduct = function editSessionProduct(cacheItem, session) {
  if (isSessionProduct(cacheItem) && session) {
    cacheItem._extend.start_date = session.start_at;
    cacheItem._extend.startDate = session.start_at;
    cacheItem._extend.start_time = session.start_time;
    cacheItem._extend.end_time = session.end_time;
    cacheItem._extend.end_date = session.end_at;
    cacheItem._extend.endDate = session.end_at;
    cacheItem._extend.duration = getDays({
      _extend: cacheItem._extend
    }, 'minutes');
    return {
      duration: cacheItem._extend.duration,
      start_time: session.start_time
    };
  }
};