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/ticketBooking/utils/index.ts
var utils_exports = {};
__export(utils_exports, {
  addService: () => addService,
  addServiceScroll: () => addServiceScroll,
  createSimpleCacheItem: () => createSimpleCacheItem,
  formatBookingList: () => formatBookingList,
  formatDateToStr: () => formatDateToStr,
  genServiceKey: () => genServiceKey,
  getCartProductOverlay: () => getCartProductOverlay,
  getDefaultStartParam: () => getDefaultStartParam,
  getDisabledEdit: () => getDisabledEdit,
  getIsOverlayProduct: () => getIsOverlayProduct,
  getIsShowNumber: () => getIsShowNumber,
  getNextTimeSlice: () => getNextTimeSlice,
  isSameProduct: () => isSameProduct,
  restoreProductOtherData: () => restoreProductOtherData
});
module.exports = __toCommonJS(utils_exports);
var import_dayjs = __toESM(require("dayjs"));
var import_utils = require("../../booking/info/service2/utils");
var import_utils2 = require("@pisell/utils");
var formatDateToStr = (date) => {
  const format = (val) => {
    return (0, import_dayjs.default)(val).format("YYYY-MM-DD HH:mm:ss");
  };
  if (Array.isArray(date)) {
    return date.map(format);
  }
  return format(date);
};
var getDefaultStartParam = () => {
  return formatDateToStr([(0, import_dayjs.default)().startOf("day"), (0, import_dayjs.default)().endOf("day")]);
};
var formatBookingList = (bookingList) => {
  return bookingList.map((item) => {
    var _a, _b, _c, _d, _e, _f;
    return {
      ...item,
      startDateTime: `${item.start_date} ${item.start_time}`,
      endDateTime: `${item.end_date} ${item.end_time}`,
      total_amount: (_a = item == null ? void 0 : item.order) == null ? void 0 : _a.total_amount,
      customer: ((_d = (_c = (_b = item == null ? void 0 : item.order) == null ? void 0 : _b.customer) == null ? void 0 : _c.display_name) == null ? void 0 : _d.trim()) || (((_f = (_e = item == null ? void 0 : item.order) == null ? void 0 : _e.customer) == null ? void 0 : _f.nickname) || "").trim()
      // durationStr: this.formatDurationStr(item),
      // durationNum: this.calcDuration(item),
    };
  });
};
var getNextTimeSlice = (sliceMinutes) => {
  const currentTime = (0, import_dayjs.default)();
  const nextSlice = Math.ceil(currentTime.minute() / sliceMinutes) * sliceMinutes;
  const nextTime = currentTime.minute(nextSlice);
  return nextTime;
};
var getDisabledEdit = ({ shop_discount, bookings, payment_status }) => {
  if (bookings.list.some((d) => !d.detail)) {
    return true;
  }
  if (shop_discount && Number(shop_discount)) {
    return true;
  }
  let list = ["unfulfilled", "paid", "unpaid", "payment_processing"];
  const ps = payment_status === "partially_paid" ? "unpaid" : payment_status;
  if (bookings.appointment_status === "cancelled" || !list.includes(ps)) {
    return true;
  }
  return false;
};
var restoreProductOtherData = (data) => {
  let { detail, ...resetD } = data;
  if (!detail) {
    return;
  }
  return {
    product_id: detail.product_id,
    option: [],
    bundle: (detail.product_bundle || []).map((bundleItem) => {
      return {
        ...bundleItem,
        title: bundleItem.bundle_title,
        group_id: bundleItem.bundle_group_id,
        id: bundleItem.bundle_id,
        product_id: bundleItem.bundle_product_id,
        bundle_variant_id: bundleItem.bundle_variant_id,
        quantity: bundleItem.num,
        option: bundleItem.option.map((optionItem) => {
          return {
            ...optionItem,
            name: optionItem.name["auto"],
            quantity: bundleItem.num,
            product_option_item_id: optionItem.option_group_item_id
          };
        }),
        price: (bundleItem == null ? void 0 : bundleItem.bundle_sum_type) === "markdown" ? `-${Number(bundleItem.bundle_sum_price)}` : Number(bundleItem.bundle_sum_price)
      };
    }),
    product_variant_id: 0,
    quantity: Number(detail.product_quantity)
  };
};
var isSameProduct = (a, b) => {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  if ((0, import_utils.isNormalProductByDurationSchedule)(a) && (0, import_utils.isNormalProductByDurationSchedule)(b)) {
    return ((_b = (_a = a._extend) == null ? void 0 : _a.other) == null ? void 0 : _b.rowKey) === ((_d = (_c = b._extend) == null ? void 0 : _c.other) == null ? void 0 : _d.rowKey);
  }
  return ((_f = (_e = a._extend) == null ? void 0 : _e.other) == null ? void 0 : _f.rowKey) === ((_h = (_g = b._extend) == null ? void 0 : _g.other) == null ? void 0 : _h.rowKey) && a._serviceKey === b._serviceKey;
};
var addService = (list, addItem, state) => {
  let value = list;
  let count;
  if (!(0, import_utils.isNormalProductByDurationSchedule)(addItem) && !getIsOverlayProduct(addItem, state)) {
    value.push(addItem);
    count = addItem._extend.quantity;
  } else {
    let isNotSame = true;
    let isSetCount = false;
    value = list.map((item) => {
      var _a, _b, _c, _d, _e;
      const isGoodPassDiscountProduct = (item.discount_list || []).some((item2) => item2.type === "good_pass");
      const bundleDiscountList = [];
      (((_b = (_a = item == null ? void 0 : item._extend) == null ? void 0 : _a.other) == null ? void 0 : _b.bundle) || []).forEach((item2) => {
        bundleDiscountList.push(...(item2 == null ? void 0 : item2.discount_list) || []);
      });
      const isGoodPassBundleProduct = bundleDiscountList.some((item2) => item2.type === "good_pass");
      if (isSameProduct(item, addItem) && !isSetCount && !isGoodPassDiscountProduct && !isGoodPassBundleProduct) {
        isNotSame = false;
        const quantity = (((_c = item == null ? void 0 : item._extend) == null ? void 0 : _c.quantity) || 1) + (((_d = addItem == null ? void 0 : addItem._extend) == null ? void 0 : _d.quantity) || 1);
        count = quantity;
        isSetCount = true;
        return {
          ...item,
          _extend: {
            ...item._extend,
            quantity,
            other: {
              ...((_e = item._extend) == null ? void 0 : _e.other) || {},
              quantity
            }
          }
        };
      }
      return item;
    });
    if (isNotSame) {
      value.push(addItem);
      count = addItem._extend.quantity;
    }
  }
  return { list: value, quantity: count };
};
var addServiceScroll = (rowKey, cacheItem) => {
  const list = document.querySelectorAll(`#rowKey-${rowKey}`);
  setTimeout(() => {
    if (list.length && (0, import_utils.isNormalProductByDurationSchedule)(cacheItem)) {
      list[list.length - 1].scrollIntoView({
        behavior: "smooth",
        block: "center"
      });
    } else {
      setTimeout(() => {
        const bookingWrap = document.querySelectorAll(`.booking-info-warp`);
        if (bookingWrap.length) {
          bookingWrap[0].scrollTo({
            top: 99999,
            behavior: "smooth"
          });
        }
      }, 100);
    }
  });
};
var getCartProductOverlay = (state) => {
  var _a, _b, _c;
  return ((_c = (_b = (_a = state == null ? void 0 : state.bookingConfig) == null ? void 0 : _a.config) == null ? void 0 : _b.basic) == null ? void 0 : _c.cart_product_overlay) || { type: "product_all" };
};
var getIsOverlayProduct = (item, state) => {
  const { type, product_ids } = getCartProductOverlay(state);
  if (type === "product_all") {
    return true;
  }
  return product_ids.includes(item.product_id || item.id);
};
var getIsShowNumber = (item, state) => {
  const isBookingTicket2 = state.isBookingTicket2;
  return (0, import_utils.isNormalProductByDurationSchedule)(item) || isBookingTicket2 && getIsOverlayProduct(item, state);
};
var genServiceKey = (item) => {
  var _a, _b, _c;
  const product_id = item.product_id;
  const durationKey = (_a = item._extend) == null ? void 0 : _a.duration;
  const start_time = (_b = item._extend) == null ? void 0 : _b.start_time;
  const total = (_c = item._extend) == null ? void 0 : _c.total;
  let resourceKey = "";
  Object.values(item._extend.resource).forEach((resources) => {
    resources.forEach((resource) => {
      resourceKey += `${resource.relation_id}`;
    });
  });
  return `${product_id}-${durationKey}-${resourceKey}-${start_time}-${total}`;
};
var createSimpleCacheItem = (item, e, state) => {
  let cacheItem = {
    ...item,
    product_id: item.id,
    _id: (0, import_utils2.getUniqueId)(),
    _key: e.key,
    _extend: {
      start_date: state.date.value,
      quantity: (e == null ? void 0 : e.quantity) || 1,
      price: item.price,
      product_name: item.title,
      other: e
    },
    new: 1
  };
  return cacheItem;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  addService,
  addServiceScroll,
  createSimpleCacheItem,
  formatBookingList,
  formatDateToStr,
  genServiceKey,
  getCartProductOverlay,
  getDefaultStartParam,
  getDisabledEdit,
  getIsOverlayProduct,
  getIsShowNumber,
  getNextTimeSlice,
  isSameProduct,
  restoreProductOtherData
});
