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); }
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); }
import dayjs from "dayjs";
import { getDiscountTime, getEndDate } from "../../../utils";
import { locales } from '@pisell/utils';
import { intersectionBy } from 'lodash';
var getIsAddH = function getIsAddH(pre, current) {
  if (current < 60) {
    return '';
  }
  var preH = Math.floor(pre / 60);
  var currentH = Math.floor(current / 60);
  if (current % 60 !== 0) {
    return '';
  }
  return getDiscountTime(currentH * 60);
};

/**
 * @title: 根据商品生成加时列表
 * @description: 
 * @param {any} config
 * @param {number} price
 * @return {*}
 * @Author: zhiwei.Wang
 * @Date: 2024-07-01 09:34
 */
export var getAddTimeSplits = function getAddTimeSplits(product, cacheItem, currentResourceTimeEndDate) {
  var _product$add_time = product.add_time,
    add_time = _product$add_time === void 0 ? {
      calculate_type: "select_quantity",
      // 最大时长
      max_add_time: 30,
      // 最小时长
      min_add_time: 30,
      // 关联产品
      relation_product: {
        type: "product_all",
        products: []
      },
      // 单位切片
      unit_duration: 30
    } : _product$add_time,
    _product$price = product.price,
    price = _product$price === void 0 ? 0 : _product$price;

  // 灵活时长
  if (add_time.additional_time_options === 'unlimited') {
    // 1. 当前服务的时长和结束时间
    // 2. 当前资源的结束时间
    // 3. 当前店铺的结束时间
    var maxEndDate = getEndDate(cacheItem._extend.startDate, 'flexible', currentResourceTimeEndDate);
    var _value = maxEndDate.diff(cacheItem._extend.endDate, 'minute');
    // 店铺营业时间大于当前服务的结束时间, 则取店铺营业时间, 否则就不可选
    if (_value > 0) {
      return [{
        label: locales.getText('page.booking.text.unlimited'),
        value: _value,
        price: price
      }];
    } else {
      // 如果当前服务已经选择到了最大时间, 则不可选
      return [];
    }
  }

  // 开始时间
  var startTime = dayjs().startOf("day");

  // 最大加时时间
  var endTime = startTime.add(add_time.max_add_time, "minute");
  var timeSlice = (add_time === null || add_time === void 0 ? void 0 : add_time.unit_duration) || 5; // 切片时间间隔（分钟）

  var arr = [];
  var currentTime = startTime;
  var lastDuration = 0;
  while (currentTime.isBefore(endTime)) {
    var nextTime = currentTime;

    // 从最小加时时间开始计算
    if (lastDuration === 0) {
      nextTime = nextTime.add(add_time.min_add_time, "minute");
    } else {
      nextTime = currentTime.add(timeSlice, "minute");
    }

    // 根据切片生成新的时间
    var durationValue = nextTime.diff(startTime, "minute");

    // 根据时间 生成label
    var durationLabel = "".concat(durationValue, " ").concat(locales.getText('pisell2.text.min'));

    // 大于60分钟的格式化成小时
    if (durationValue >= 60) {
      durationLabel = getDiscountTime(durationValue);
    }
    arr.push({
      label: durationLabel,
      value: durationValue,
      price: durationValue / timeSlice * price
    });
    lastDuration = durationValue;
    currentTime = nextTime;
  }
  return arr;
};

/**
 * @title: 判断当前商品是否可用
 * @description: 
 * @param {any} relationProduct
 * @param {any} currentItem
 * @return {*}
 * @Author: zhiwei.Wang
 * @Date: 2024-07-01 09:33
 */
export var isUsableProduct = function isUsableProduct(addTime, currentItem) {
  var _addTime$relation_pro;
  // 商品类型
  var type = (_addTime$relation_pro = addTime.relation_product) === null || _addTime$relation_pro === void 0 ? void 0 : _addTime$relation_pro.type;
  var relationProduct = addTime.relation_product || [];
  if (!currentItem) {
    return false;
  }
  if (addTime.additional_time_options === 'unlimited') {
    var maxEndDate = getEndDate(currentItem._extend.startDate, 'flexible', '');
    // 当前服务的结束时间大于店铺的营业时间, 过滤
    if (currentItem._extend.endDate.isAfter(maxEndDate)) {
      return false;
    }
  }

  // 全部商品
  if (type === "product_all") {
    return true;
  } else {
    if (type === "product_collection") {
      return !!intersectionBy(relationProduct.products.map(function (item) {
        return _objectSpread(_objectSpread({}, item), {}, {
          id: item.product_collection_id
        });
      }) || [], currentItem.collection || [], "id").length;
    } else {
      return relationProduct.products.some(function (item) {
        // 判断商品
        if (type === "products") {
          if (item.product_id == currentItem.product_id) {
            return true;
          }
        }
      });
    }
  }
};
export var addTimeRowKey = {
  // 创建, 增加前缀
  create: function create(product_id, time) {
    return "".concat(product_id, "_").concat(time);
  },
  // 格式化, 去除前缀
  getValue: function getValue(str) {
    var arr = str.split("_");
    var id = arr[0] || 0;
    var time = arr[1] || 0;
    return {
      product_id: Number(id),
      time: time === 'flexible' ? time : Number(arr[1] || 0)
    };
  }
};