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/components/footer/utils.ts
var utils_exports = {};
__export(utils_exports, {
  checkHolderIsError: () => checkHolderIsError,
  checkResourcesFormat: () => checkResourcesFormat,
  checkSubResourcesCapacity: () => checkSubResourcesCapacity,
  formatProductBundle: () => formatProductBundle,
  formatProductOption: () => formatProductOption,
  formatProductPrice: () => formatProductPrice,
  formatValues: () => formatValues
});
module.exports = __toCommonJS(utils_exports);
var import_dayjs = __toESM(require("dayjs"));
var import_utils = require("../../utils");
var import_utils2 = require("../../utils");
var import_utilsByBooking = require("../../info2/utilsByBooking");
var import_utils3 = require("../../utils");
var import_status = require("../../info/service2/status");
var import_utils4 = require("../../info/service2/utils");
var import_utils5 = require("@pisell/utils");
var import_materials = require("@pisell/materials");
var formatNote = (data) => {
  var _a;
  return ((_a = data.notes) == null ? void 0 : _a.orderNote) || "";
};
var formatCustomer = (data) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
  let id = (_b = (_a = data.client) == null ? void 0 : _a.value) == null ? void 0 : _b.id;
  if ((0, import_utils2.isWalkIn)(id)) {
    id = 1;
  }
  return {
    customer_id: id,
    customer_name: (_d = (_c = data.client) == null ? void 0 : _c.value) == null ? void 0 : _d.name,
    email: (_f = (_e = data.client) == null ? void 0 : _e.value) == null ? void 0 : _f.email,
    phone: ((_h = (_g = data.client) == null ? void 0 : _g.value) == null ? void 0 : _h.phone) || ((_j = (_i = data.client) == null ? void 0 : _i.value) == null ? void 0 : _j.mobile),
    country_calling_code: (_l = (_k = data.client) == null ? void 0 : _k.value) == null ? void 0 : _l.country_calling_code
  };
};
var formatProductOption = (option) => {
  if (!option) {
    return [];
  }
  return option.map((d) => {
    return {
      "num": d.num,
      "option_group_item_id": d.product_option_item_id || d.option_group_item_id,
      "option_group_id": d.option_group_id
      // "add_price": Number(d?.add_price),
    };
  });
};
var log = (data) => {
  (0, import_utils5.sendWarningLog)({
    title: "预约2.0下单无效的套餐商品数据",
    content: [
      { key: "bundle", value: JSON.stringify(data) },
      { key: "日期", value: (0, import_dayjs.default)().format("YYYY-MM-DD HH:mm") }
    ]
  });
};
var formatProductBundle = (bundle) => {
  if (!bundle) {
    return;
  }
  try {
    const errorBundles = bundle == null ? void 0 : bundle.filter((d) => !(d == null ? void 0 : d.group_id) || !(d == null ? void 0 : d.id) || !(d == null ? void 0 : d._bundle_product_id));
    if ((errorBundles == null ? void 0 : errorBundles.length) > 0) {
      log(errorBundles);
    }
  } catch (error) {
  }
  return bundle.map((d) => {
    return {
      "bundle_group_id": d.group_id,
      "bundle_id": d.id,
      "bundle_product_id": (d == null ? void 0 : d._bundle_product_id) || (d == null ? void 0 : d.product_id),
      "bundle_variant_id": d.bundle_variant_id,
      "num": d.num,
      "extension_id": d.extension_id,
      "extension_type": d.extension_type,
      // "price": Number(d?.price),
      // "price_type": d?.price_type,
      "option": formatProductOption(d.option)
    };
  });
};
var getSubType = (item) => {
  var _a;
  const _type = (_a = item.duration) == null ? void 0 : _a.type;
  if (_type) {
    return _type;
  }
  return "minutes";
};
var formatRelationDetails = (list) => {
  return (list || []).map((item) => {
    return {
      num: item.product_quantity,
      price: item.payment_price || 0,
      product_id: item.product_id,
      is_charge_tax: item.is_charge_tax,
      order_detail_id: item.order_detail_id
    };
  });
};
var checkSubResourcesCapacity = (resource) => {
  if (resource.children && resource.children.length) {
    let countCapacity = resource.capacity;
    resource.children.forEach((child) => {
      if (child.capacity <= countCapacity) {
        countCapacity -= child.capacity;
      } else {
        child.capacity = countCapacity;
        countCapacity = 0;
      }
    });
  }
};
var checkResourcesFormat = (resources, service, isChild) => {
  return resources.map((resource) => {
    var _a;
    if (!isChild) {
      resource.capacity = (0, import_utilsByBooking.getSumCapacity)({ cacheItem: service }, false);
    }
    checkSubResourcesCapacity(resource);
    return {
      children: ((_a = resource.children) == null ? void 0 : _a.length) ? checkResourcesFormat(resource.children, service, true) : void 0,
      form_id: resource.form_id,
      relation_type: resource.relation_type || "form",
      relation_id: resource.relation_id || 0,
      capacity: resource.capacity,
      like_status: resource.like_status || "common",
      metadata: resource.metadata || {}
    };
  });
};
var formatResource = (service) => {
  if (!service._extend.resource) {
    return void 0;
  }
  try {
    return Object.values(service._extend.resource).reduce((pre, cur) => {
      const newList = checkResourcesFormat(cur, service);
      pre = pre.concat(newList);
      return pre;
    }, []);
  } catch (err) {
    return void 0;
  }
};
var formatHolders = (state, item, values) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
  const form_id = ((_d = (_c = (_b = (_a = state == null ? void 0 : state.bookingConfig) == null ? void 0 : _a.config) == null ? void 0 : _b.basic) == null ? void 0 : _c.appointment_subject) == null ? void 0 : _d.form_id) || 0;
  let form_record = ((_e = item._extend) == null ? void 0 : _e.holder_id) ? [(_f = item._extend) == null ? void 0 : _f.holder_id] : [];
  if (((_g = item._extend) == null ? void 0 : _g.holder_id) && (0, import_utils5.isArr)((_h = item._extend) == null ? void 0 : _h.holder_id)) {
    form_record = (_i = item._extend) == null ? void 0 : _i.holder_id;
  }
  let obj = {
    customer_id: values.customer_id,
    form_id,
    form_record
  };
  if (state.isFranchisee) {
    obj.is_source_shop = 1;
  }
  return obj;
};
var formatProductPrice = (item) => {
  var _a, _b, _c, _d;
  const other = (_a = item._extend) == null ? void 0 : _a.other;
  let price = Number((_b = item._extend) == null ? void 0 : _b.price);
  if ((other == null ? void 0 : other.product_variant_id) > 0 && ((_c = item == null ? void 0 : item.variant) == null ? void 0 : _c.length) > 0) {
    const variant = item.variant.find((d) => d.id === (other == null ? void 0 : other.product_variant_id));
    if (variant) {
      price = Number(variant == null ? void 0 : variant.price) || 0;
    }
  }
  if (other == null ? void 0 : other.option) {
    price = (_d = other == null ? void 0 : other.option) == null ? void 0 : _d.reduce((pre, cur) => {
      return pre + Number(cur.price) * Number(cur.num);
    }, price);
  }
  return price;
};
var formatMetaData = (item) => {
  var _a, _b, _c, _d, _e;
  let metadata = {};
  if ((_a = item._extend) == null ? void 0 : _a.capacity) {
    metadata.capacity = item._extend.capacity;
  }
  if (item._extend.groupId) {
    metadata.groupId = item._extend.groupId;
  }
  if ((_b = item._extend) == null ? void 0 : _b.holder_id) {
    metadata.holder_id = (_c = item._extend) == null ? void 0 : _c.holder_id;
  }
  if ((_d = item._extend) == null ? void 0 : _d.multiDayPrices) {
    metadata.multiDayPrices = (_e = item._extend) == null ? void 0 : _e.multiDayPrices;
  }
  return metadata;
};
var getSourceProductPrice = (item) => {
  var _a, _b, _c;
  const other = (_a = item._extend) == null ? void 0 : _a.other;
  let price = Number((_b = item._extend) == null ? void 0 : _b.price);
  if ((other == null ? void 0 : other.product_variant_id) > 0 && ((_c = item == null ? void 0 : item.variant) == null ? void 0 : _c.length) > 0) {
    const variant = item.variant.find((d) => d.id === (other == null ? void 0 : other.product_variant_id));
    if (variant) {
      price = Number(variant == null ? void 0 : variant.price) || 0;
    }
  }
  return price;
};
var formatProducts = (item, quantity) => {
  var _a, _b, _c, _d, _e, _f;
  const total = Number(item._extend.total);
  const origin_total = Number(item._extend.origin_total);
  const discount_amount = origin_total - total;
  const discount_list = (item == null ? void 0 : item.discount_list) || [];
  const discount_type = discount_amount < 0 ? "change_price" : "discount";
  const notManualDiscount = discount_list.filter((item2) => item2.type !== "product");
  return {
    "num": quantity || item._extend.quantity,
    "price": formatProductPrice(item),
    "source_product_price": getSourceProductPrice(item),
    "product_id": (item == null ? void 0 : item.product_id) || item.id,
    "discount_amount": notManualDiscount.length > 0 ? 0 : discount_amount,
    "product_discount_reason": item._extend.discount_reason || "",
    "is_charge_tax": item.is_charge_tax,
    "note": (item == null ? void 0 : item.note) || "",
    "discountway": item._extend.discountway || "num",
    "discount_per": item._extend.discount_per,
    "discount_type": item._extend.discount_type || discount_type,
    "order_detail_id": item._extend.order_detail_id || 0,
    "product_bundle": formatProductBundle((_b = (_a = item._extend) == null ? void 0 : _a.other) == null ? void 0 : _b.bundle),
    "bundle_edit": item._extend.bundle_edit,
    "product_option_item": formatProductOption((_d = (_c = item._extend) == null ? void 0 : _c.other) == null ? void 0 : _d.option),
    "product_variant_id": ((_f = (_e = item._extend) == null ? void 0 : _e.other) == null ? void 0 : _f.product_variant_id) || 0,
    "discount_list": notManualDiscount,
    metadata: formatMetaData(item)
  };
};
var formatAppointmentProductLists = (data) => {
  var _a;
  try {
    let list = data.service.value;
    list = list.filter((item) => {
      var _a2;
      return ((_a2 = item._extend) == null ? void 0 : _a2.holder_id) && !(0, import_utils4.isNormalProductByDurationSchedule)(item);
    });
    const groupMap = /* @__PURE__ */ new Map();
    list.forEach((item) => {
      var _a2;
      const key = (_a2 = item._extend) == null ? void 0 : _a2.holder_id;
      if (!groupMap.has(key)) {
        groupMap.set(key, []);
      }
      groupMap.get(key).push(item);
    });
    const result = (_a = Array.from(groupMap.values())) == null ? void 0 : _a.reduce((acc, group) => acc.concat(group), []);
    return list || [];
  } catch (error) {
  }
};
var getHolderByIndex = (holder, index) => {
  var _a;
  return {
    ...holder,
    form_record: [(_a = holder == null ? void 0 : holder.form_record) == null ? void 0 : _a[index]].filter(Boolean)
  };
};
var formatBookings = (data, values) => {
  const isDay = (0, import_utils4.isDaysBooking)(data);
  const isEvent = (0, import_utils4.isEventBooking)(data);
  let list = data.service.value;
  if (isDay) {
    list = formatAppointmentProductLists(data);
  }
  if (isEvent) {
    list = list.filter((d) => {
      var _a;
      return (_a = d._extend) == null ? void 0 : _a.holder_id;
    });
  }
  if (!isDay && !isEvent) {
    list = list.filter((item) => !(0, import_utils4.isNormalProductByDurationSchedule)(item));
  }
  let dayParams = {};
  const eventOtherParams = data.eventOtherParams;
  let bookings = [];
  list.forEach((item) => {
    var _a, _b, _c, _d;
    let start_date = item._extend.startDate.format("YYYY-MM-DD");
    let end_date = item._extend.endDate.format("YYYY-MM-DD");
    let end_time = item._extend.endDate.format("HH:mm");
    let start_time = item._extend.startDate.format("HH:mm");
    if ((0, import_utils.isNowTime)(item._extend.start_time)) {
      start_time = (0, import_utils.getNowTime)();
    }
    dayParams.holder = formatHolders(data, item, values);
    if (isDay) {
      const startDate = ((_a = item._extend) == null ? void 0 : _a.startDate) || ((_b = item._extend) == null ? void 0 : _b.start_date);
      const endDate = ((_c = item._extend) == null ? void 0 : _c.endDate) || ((_d = item._extend) == null ? void 0 : _d.end_date);
      const { startDateTime, endDateTime } = (0, import_utils4.formatDayTime)({ startDate, endDate });
      start_date = (0, import_dayjs.default)(startDateTime).format("YYYY-MM-DD");
      start_time = (0, import_dayjs.default)(startDateTime).format("HH:mm");
      end_date = (0, import_dayjs.default)(endDateTime).format("YYYY-MM-DD");
      end_time = (0, import_dayjs.default)(endDateTime).format("HH:mm");
      dayParams.relation_products = (item == null ? void 0 : item.relation_products) || [];
      dayParams.holder = formatHolders(data, item, values);
      dayParams.duration = (0, import_utils3.getDays)({ _extend: { start_date: startDateTime, end_date: endDateTime } }, "minutes");
    }
    const booking = {
      "product": formatProducts(item),
      "relation_products": formatRelationDetails(item._extend.relation_details),
      "id": item.booking_id || 0,
      "relation_id": item._extend.relation_id || 0,
      "like_status": item._extend.like_status || "common",
      "relation_type": item._extend.relation_type || "",
      "is_all": false,
      "schedule_id": item._extend.schedule_id || 0,
      "end_time": end_time,
      "sub_type": isDay ? "days" : getSubType(item),
      "select_date": start_date,
      "start_time": start_time,
      "end_date": end_date,
      "start_date": start_date,
      "duration": isDay ? 0 : import_utils.flexibleObj.getValue(item._extend.duration),
      "number": item._extend.capacity ? (0, import_utilsByBooking.getSumCapacity)({ cacheItem: item }) : item._extend.number || 1,
      "resources": formatResource(item),
      "metadata": formatMetaData(item),
      ...eventOtherParams || {},
      ...dayParams
    };
    if (isDay) {
      bookings.push(booking);
      return;
    }
    for (let i = 0; i < (item._extend.quantity || 1); i++) {
      bookings.push({
        ...booking,
        "product": formatProducts(item, 1),
        "number": item._extend.capacity ? (0, import_utilsByBooking.getSumCapacity)({ cacheItem: item }, false) : item._extend.number || 1,
        "holder": getHolderByIndex(booking.holder, i)
      });
    }
  });
  return bookings;
};
var formatRelationProducts = (data, type) => {
  var _a;
  let list = ((_a = data.service) == null ? void 0 : _a.value) || [];
  list = list.filter(
    (item) => {
      var _a2;
      return (
        // 跨日预约把普通商品加在relationProducts 活动预约按之前逻辑
        type === "day" ? (0, import_utils4.isNormalProductByDurationSchedule)(item) : ((_a2 = item._extend) == null ? void 0 : _a2.holder_id) && import_status.appointmentTypes.includes(item.extension_type)
      );
    }
  );
  let relationProducts = [];
  relationProducts = list.map((item) => {
    return formatProducts(item);
  });
  return relationProducts || [];
};
var formatNormalProducts = (data) => {
  var _a;
  let list = ((_a = data.service) == null ? void 0 : _a.value) || [];
  list = list.filter((item) => (0, import_utils4.isNormalProductByDurationSchedule)(item));
  let relationProducts = [];
  relationProducts = list.map((item) => {
    return formatProducts(item);
  });
  return relationProducts || [];
};
var formatHolder = (state, values) => {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  const form_id = ((_d = (_c = (_b = (_a = state == null ? void 0 : state.bookingConfig) == null ? void 0 : _a.config) == null ? void 0 : _b.basic) == null ? void 0 : _c.appointment_subject) == null ? void 0 : _d.form_id) || 0;
  const petValue = ((_e = state.pet.value) == null ? void 0 : _e[form_id]) || [];
  let obj = {
    customer_id: values.customer_id,
    form_id,
    form_record: petValue.map((d) => d.form_record_id)
  };
  if (!petValue.length && ((_h = (_g = (_f = state.pet) == null ? void 0 : _f.originValue) == null ? void 0 : _g.form_record) == null ? void 0 : _h.length)) {
    obj.form_record = state.pet.originValue.form_record;
  }
  if (state.isFranchisee) {
    obj.is_source_shop = 1;
  }
  return obj;
};
var formatValues = (state) => {
  var _a, _b, _c, _d, _e, _f, _g;
  let values = {
    type: state.renderType === "eventBooking" ? "event_booking_v2" : "appointment_booking",
    platform: state.isBookingTicket2 ? "pos" : "shop",
    sales_channel: "my_pisel",
    order_sales_channel: state.isBookingTicket2 ? "pos" : "online_store",
    bookings: [],
    customer_id: 1,
    customer_name: "",
    holder: {},
    relation_products: [],
    metadata: {}
  };
  const isDay = (0, import_utils4.isDaysBooking)(state);
  const isEvent = (0, import_utils4.isEventBooking)(state);
  if (!state.bookingId || isDay) {
    values.shop_note = formatNote(state);
  }
  if (state.bookingId) {
    values.shop_discount = state.bookingDetail.shop_discount;
    values.shop_discount_reason = state.bookingDetail.shop_discount_reason;
  }
  if (state.contacts) {
    values.contacts = ((_a = state.contacts) == null ? void 0 : _a.value) || [];
  }
  const customer = formatCustomer(state);
  values = {
    ...values,
    ...customer
  };
  values.holder = formatHolder(state, values);
  if (isDay || isEvent) {
    values.relation_products = formatRelationProducts(state, isDay ? "day" : "event");
  } else {
    values.relation_products = formatNormalProducts(state);
    if (!((_b = values.relation_products) == null ? void 0 : _b.length)) {
      values.relation_products = void 0;
    }
  }
  values.bookings = formatBookings(state, values);
  if (((_d = (_c = values == null ? void 0 : values.bookings) == null ? void 0 : _c[0]) == null ? void 0 : _d.select_date) && !(state == null ? void 0 : state.bookingId)) {
    let startTime = `${(_f = (_e = values == null ? void 0 : values.bookings) == null ? void 0 : _e[0]) == null ? void 0 : _f.start_time}`;
    startTime = `${startTime || "00:00"}:00`;
    values.schedule_date = `${values.bookings[0].select_date} ${startTime}`;
  }
  if (isDay) {
    values.metadata.is_multiday_appointment = 1;
  }
  if (!((_g = values == null ? void 0 : values.bookings) == null ? void 0 : _g.length)) {
    values.type = "virtual";
  }
  return values;
};
var checkHolderIsError = (state) => {
  var _a, _b;
  const holderError = state.service.value.some((item) => {
    var _a2, _b2;
    if ((0, import_utils4.isNormalProductByDurationSchedule)(item)) {
      return false;
    }
    if (item._extend.quantity === 1 && !item._extend.holder_id) {
      return true;
    }
    if (item._extend.quantity > 1 && ((_b2 = (_a2 = item._extend) == null ? void 0 : _a2.holder_id) == null ? void 0 : _b2.length) !== item._extend.quantity) {
      return true;
    }
    return false;
  });
  const clientId = (_b = (_a = state.client) == null ? void 0 : _a.value) == null ? void 0 : _b.id;
  if (holderError && (!clientId || clientId === 0 || clientId === 1)) {
    (0, import_materials.PisellToast)({
      content: import_utils5.locales.getText("pisell2.text.please-select-the-customer-first")
    });
  }
  return holderError;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  checkHolderIsError,
  checkResourcesFormat,
  checkSubResourcesCapacity,
  formatProductBundle,
  formatProductOption,
  formatProductPrice,
  formatValues
});
