var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/booking/info2/service/addTimeModal/utils.ts
var utils_exports = {};
__export(utils_exports, {
  addTimeRowKey: () => addTimeRowKey,
  getAddTimeSplits: () => getAddTimeSplits,
  isUsableProduct: () => isUsableProduct
});
module.exports = __toCommonJS(utils_exports);
var import_dayjs = __toESM(require("dayjs"));
var import_utils = require("../../../utils");
var import_utils2 = require("@pisell/utils");
var import_lodash = require("lodash");
var import_utils3 = require("../addService/utils");
var getAddTimeSplits = (product, cacheItem) => {
  var _a, _b, _c;
  const { add_time = {
    calculate_type: "select_quantity",
    // 最大时长
    max_add_time: 30,
    // 最小时长
    min_add_time: 30,
    // 关联产品
    relation_product: { type: "product_all", products: [] },
    // 单位切片
    unit_duration: 30
  }, price = 0 } = product;
  if (add_time.additional_time_options === "unlimited") {
    let maxEndDate = (0, import_utils.getEndDate)(cacheItem._extend.startDate, "flexible", (_c = (_b = (_a = (0, import_utils3.getUsableTime)(cacheItem)) == null ? void 0 : _a._extend) == null ? void 0 : _b.usableTime) == null ? void 0 : _c.end_at);
    let _value = maxEndDate.diff(cacheItem._extend.endDate, "minute");
    if (_value > 0) {
      return [
        {
          label: import_utils2.locales.getText("page.booking.text.unlimited"),
          value: _value,
          price
        }
      ];
    } else {
      return [];
    }
  }
  const startTime = (0, import_dayjs.default)().startOf("day");
  const endTime = startTime.add(add_time.max_add_time, "minute");
  const timeSlice = (add_time == null ? void 0 : add_time.unit_duration) || 5;
  let arr = [];
  let currentTime = startTime;
  let lastDuration = 0;
  while (currentTime.isBefore(endTime)) {
    let nextTime = currentTime;
    if (lastDuration === 0) {
      nextTime = nextTime.add(add_time.min_add_time, "minute");
    } else {
      nextTime = currentTime.add(timeSlice, "minute");
    }
    const durationValue = nextTime.diff(startTime, "minute");
    let durationLabel = `${durationValue} ${import_utils2.locales.getText("pisell2.text.min")}`;
    if (durationValue >= 60) {
      durationLabel = (0, import_utils.getDiscountTime)(durationValue);
    }
    arr.push({
      label: durationLabel,
      value: durationValue,
      price: durationValue / timeSlice * price
    });
    lastDuration = durationValue;
    currentTime = nextTime;
  }
  return arr;
};
var isUsableProduct = (addTime, currentItem) => {
  var _a;
  const type = (_a = addTime.relation_product) == null ? void 0 : _a.type;
  const relationProduct = addTime.relation_product || [];
  if (!currentItem) {
    return false;
  }
  if (addTime.additional_time_options === "unlimited") {
    let maxEndDate = (0, import_utils.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 !!(0, import_lodash.intersectionBy)(
        relationProduct.products.map((item) => ({
          ...item,
          id: item.product_collection_id
        })) || [],
        currentItem.collection || [],
        "id"
      ).length;
    } else {
      return relationProduct.products.some(
        (item) => {
          if (type === "products") {
            if (item.product_id == currentItem.product_id) {
              return true;
            }
          }
        }
      );
    }
  }
};
var addTimeRowKey = {
  // 创建, 增加前缀
  create: (product_id, time) => {
    return `${product_id}_${time}`;
  },
  // 格式化, 去除前缀
  getValue: (str) => {
    let arr = str.split("_");
    let id = arr[0] || 0;
    let time = arr[1] || 0;
    return {
      product_id: Number(id),
      time: time === "flexible" ? time : Number(arr[1] || 0)
    };
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  addTimeRowKey,
  getAddTimeSplits,
  isUsableProduct
});
