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,
  formatOriginalPrice: () => formatOriginalPrice,
  formatProductBundle: () => formatProductBundle,
  formatProductOption: () => formatProductOption,
  formatProductPrice: () => formatProductPrice,
  formatSellingPrice: () => formatSellingPrice,
  formatValues: () => formatValues,
  getBundleDiscountList: () => getBundleDiscountList,
  getHasMultiDayProduct: () => getHasMultiDayProduct,
  getMainProductSellingPrice: () => getMainProductSellingPrice,
  getSourceProductPrice: () => getSourceProductPrice
});
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 import_utils6 = require("../../../../plus/clientName/utils");
var import_lodash = require("lodash");
var import_decimal = __toESM(require("decimal.js"));
var import_amount = require("./amount");
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;
  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: (0, import_utils6.getClientName)((_c = data.client) == null ? void 0 : _c.value),
    email: (_e = (_d = data.client) == null ? void 0 : _d.value) == null ? void 0 : _e.email,
    phone: ((_g = (_f = data.client) == null ? void 0 : _f.value) == null ? void 0 : _g.phone) || ((_i = (_h = data.client) == null ? void 0 : _h.value) == null ? void 0 : _i.mobile),
    country_calling_code: (_k = (_j = data.client) == null ? void 0 : _j.value) == null ? void 0 : _k.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,
      custom_add_price: Number(d == null ? void 0 : 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, is_price_include_tax) => {
  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) => {
    const option = formatProductOption(d.option);
    const option_price = option.reduce((temp, item) => {
      return temp + Number(item.num) * item.custom_add_price;
    }, 0);
    const product_discount_difference = ((d == null ? void 0 : d.discount_list) || []).reduce((pre, cur) => {
      var _a;
      return pre + (((_a = cur == null ? void 0 : cur.metadata) == null ? void 0 : _a.product_discount_difference) || 0);
    }, 0);
    const metadata = {};
    const _metadata = (d == null ? void 0 : d.metadata) ?? {};
    const targetFields = [
      "custom_product_bundle_map_id",
      "unique_identification_number"
    ];
    targetFields.forEach((field) => {
      if (_metadata[field]) {
        metadata[field] = _metadata[field];
      }
    });
    return {
      "is_charge_tax": d.is_charge_tax,
      "tax_fee": d.tax_fee,
      "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 == null ? void 0 : d.product_price),
      "price_type": d == null ? void 0 : d.price_type,
      "price_type_ext": d == null ? void 0 : d.price_type_ext,
      "custom_price": (d == null ? void 0 : d.custom_price) || Math.abs(new import_decimal.default((d == null ? void 0 : d.original_price) ?? (d == null ? void 0 : d.product_price)).sub(option_price).toNumber()).toFixed(2),
      "custom_price_type": d == null ? void 0 : d.price_type,
      "option": option,
      "bundle_selling_price": Math.abs(d == null ? void 0 : d.price),
      // "bundle_sum_price": d?.original_price || d?.original_total,
      is_price_include_tax,
      "metadata": {
        custom_product_bundle_map_id: d._id,
        // 子商品单数量附加费
        "surcharge_fee": d.surcharge_fee,
        // 可选，附加费均摊舍入金额
        "surcharge_rounding_remainder": d.surcharge_rounding_remainder,
        // 可选，税费均摊舍入金额（折后税费）
        "tax_fee_rounding_remainder": d.tax_fee_rounding_remainder,
        relation_surcharge_ids: d.relation_surcharge_ids || [],
        product_discount_difference,
        ...metadata
      }
    };
  });
};
var getBundleDiscountList = (bundle) => {
  if (!bundle) {
    return [];
  }
  let discountList = [];
  bundle.forEach((d) => {
    if (d.discount_list && (0, import_utils5.isArr)(d.discount_list)) {
      d.discount_list.forEach((item) => {
        if (item == null ? void 0 : item.id) {
          item.order_discount_id = item.id;
        }
      });
      discountList.push(...d.discount_list.filter((item) => !item.id));
    }
  });
  return discountList;
};
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.original_price || 0,
      product_id: item.product_id,
      is_charge_tax: item.is_charge_tax,
      order_detail_id: item.order_detail_id,
      tax_fee: item.tax_fee,
      metadata: {
        // 可选，税费均摊舍入金额（折后税费）
        tax_fee_rounding_remainder: item.tax_fee_rounding_remainder
      }
    };
  });
};
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, _e, _f, _g;
  const other = (_a = item._extend) == null ? void 0 : _a.other;
  let price = Number((item == null ? void 0 : item.price) ?? ((_b = item == null ? void 0 : item._extend) == null ? void 0 : _b.price));
  if ((0, import_utils5.isString)((_c = item._extend) == null ? void 0 : _c.priceOverride) || (0, import_utils5.isNumber)((_d = item._extend) == null ? void 0 : _d.priceOverride)) {
    price = Number((_e = item == null ? void 0 : item._extend) == null ? void 0 : _e.price);
  }
  if ((other == null ? void 0 : other.product_variant_id) > 0 && ((_f = item == null ? void 0 : item.variant) == null ? void 0 : _f.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 = (_g = other == null ? void 0 : other.option) == null ? void 0 : _g.reduce((pre, cur) => {
      return pre + Number(cur.price) * Number(cur.num);
    }, price);
  }
  return price;
};
var formatOriginalPrice = (item) => {
  var _a;
  return Number((_a = item._extend) == null ? void 0 : _a.origin_total);
};
var formatSellingPrice = (item) => {
  var _a;
  return Number((_a = item._extend) == null ? void 0 : _a.total);
};
var formatMetaData = (item, isMultiDayPrices) => {
  var _a, _b, _c, _d, _e, _f;
  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;
  }
  metadata.holder_id = ((_b = item._extend) == null ? void 0 : _b.holder_id) ?? null;
  if (isMultiDayPrices && ((_c = item._extend) == null ? void 0 : _c.multiDayPrices)) {
    metadata.multiDayPrices = (_d = item._extend) == null ? void 0 : _d.multiDayPrices;
  }
  if ((_e = item._extend) == null ? void 0 : _e.unique_identification_number) {
    metadata.unique_identification_number = (_f = item._extend) == null ? void 0 : _f.unique_identification_number;
  }
  if (item.discount_list && (0, import_utils5.isArr)(item.discount_list)) {
    metadata.product_discount_difference = item.discount_list.filter((item2) => {
      var _a2;
      return !((_a2 = item2 == null ? void 0 : item2.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id);
    }).reduce((pre, cur) => {
      var _a2;
      return pre + (((_a2 = cur == null ? void 0 : cur.metadata) == null ? void 0 : _a2.product_discount_difference) || 0);
    }, 0);
  }
  return metadata;
};
var formatProductMetadata = (item, list) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
  let metadata = {};
  if (((_a = item._extend) == null ? void 0 : _a.surcharge_fee) || ((_b = item.metadata) == null ? void 0 : _b.main_product_attached_bundle_surcharge_fee)) {
    metadata.main_product_attached_bundle_surcharge_fee = ((_c = item._extend) == null ? void 0 : _c.surcharge_fee) || ((_d = item.metadata) == null ? void 0 : _d.main_product_attached_bundle_surcharge_fee);
  }
  if (((_e = item._extend) == null ? void 0 : _e.main_product_attached_bundle_tax_fee) || ((_f = item.metadata) == null ? void 0 : _f.main_product_attached_bundle_tax_fee)) {
    metadata.main_product_attached_bundle_tax_fee = ((_g = item._extend) == null ? void 0 : _g.main_product_attached_bundle_tax_fee) || ((_h = item.metadata) == null ? void 0 : _h.main_product_attached_bundle_tax_fee);
  }
  if (((_i = item._extend) == null ? void 0 : _i.surcharge_rounding_remainder) || ((_j = item.metadata) == null ? void 0 : _j.surcharge_rounding_remainder)) {
    metadata.surcharge_rounding_remainder = ((_k = item._extend) == null ? void 0 : _k.surcharge_rounding_remainder) || ((_l = item.metadata) == null ? void 0 : _l.surcharge_rounding_remainder);
  }
  if ((_m = item._extend) == null ? void 0 : _m.tax_fee_rounding_remainder) {
    metadata.tax_fee_rounding_remainder = (_n = item._extend) == null ? void 0 : _n.tax_fee_rounding_remainder;
  }
  if (((_o = item._extend) == null ? void 0 : _o.relation_surcharge_ids) || ((_p = item.metadata) == null ? void 0 : _p.relation_surcharge_ids)) {
    metadata.relation_surcharge_ids = ((_q = item._extend) == null ? void 0 : _q.relation_surcharge_ids) || ((_r = item.metadata) == null ? void 0 : _r.relation_surcharge_ids);
  }
  if (((_s = item._extend) == null ? void 0 : _s.unique_identification_number) || ((_t = item.metadata) == null ? void 0 : _t.unique_identification_number)) {
    metadata.unique_identification_number = ((_u = item._extend) == null ? void 0 : _u.unique_identification_number) || ((_v = item.metadata) == null ? void 0 : _v.unique_identification_number);
  }
  if (item.discount_list && (0, import_utils5.isArr)(item.discount_list)) {
    metadata.product_discount_difference = item.discount_list.filter((item2) => {
      var _a2;
      return !((_a2 = item2 == null ? void 0 : item2.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id);
    }).reduce((pre, cur) => {
      var _a2;
      return pre + (((_a2 = cur == null ? void 0 : cur.metadata) == null ? void 0 : _a2.product_discount_difference) || 0);
    }, 0);
  }
  if (item._giftInfo) {
    metadata.gift_card_type = "promotion";
    if (list == null ? void 0 : list.length) {
      const parentProduct = list.find(
        (listItem) => {
          var _a2;
          return listItem._id === ((_a2 = item._giftInfo) == null ? void 0 : _a2.sourceProductId);
        }
      );
      if (parentProduct) {
        metadata.parent_unique_identification_number = ((_w = parentProduct._extend) == null ? void 0 : _w.unique_identification_number) || "";
        metadata.parent_product_id = (parentProduct == null ? void 0 : parentProduct.product_id) || parentProduct.product_id === 0 ? parentProduct.product_id : parentProduct.id;
      }
    }
  }
  if ((_x = item._extend) == null ? void 0 : _x.unique_identification_number) {
    metadata.unique_identification_number = (_y = item._extend) == null ? void 0 : _y.unique_identification_number;
  }
  return metadata;
};
var getSourceProductPrice = (item) => {
  var _a, _b, _c;
  const other = (_a = item._extend) == null ? void 0 : _a.other;
  let price = Number((item == null ? void 0 : item.price) ?? ((_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 getMainProductSellingPrice = (item) => {
  var _a, _b, _c, _d, _e, _f;
  if (!(0, import_utils5.isNumber)((_a = item._extend) == null ? void 0 : _a.main_product_selling_price) && !(0, import_utils5.isString)((_b = item._extend) == null ? void 0 : _b.main_product_selling_price)) {
    return void 0;
  }
  let optionPrice = new import_decimal.default(0);
  if (((_c = item._extend.other) == null ? void 0 : _c.option) && Array.isArray((_d = item._extend.other) == null ? void 0 : _d.option)) {
    optionPrice = (_e = item._extend.other) == null ? void 0 : _e.option.reduce((accumulator, currentValue) => {
      const currentPrice = new import_decimal.default(currentValue.price || 0);
      const currentNum = new import_decimal.default(currentValue.num || 0);
      return accumulator.add(currentPrice.mul(currentNum));
    }, optionPrice);
    if (item == null ? void 0 : item.booking_id) {
      optionPrice = optionPrice.sub((0, import_amount.getOptionDiscountAmount)((item == null ? void 0 : item.discount_list) || []));
    }
  }
  return new import_decimal.default((_f = item._extend) == null ? void 0 : _f.main_product_selling_price).add(optionPrice).toNumber();
};
var formatWeightInfo = (weighing) => {
  var _a, _b;
  if (!weighing) {
    return void 0;
  }
  const unitsMap = {
    KILOGRAMS: "kg",
    GRAMS: "g",
    POUNDS: "lb",
    OUNCES: "oz"
  };
  return {
    open_sold_weight: !!weighing ? 1 : 0,
    // 商品信息快照
    unit_data_snapshot: weighing.scalePriceProduct,
    // 转化为标准单位的信息
    unit: unitsMap[weighing.unit] || weighing.unit,
    unit_weight: 1,
    unit_price: weighing.unit_price,
    tare_name: ((_a = weighing.tare) == null ? void 0 : _a.label) || "",
    tare_weight: ((_b = weighing.tare) == null ? void 0 : _b.value) || 0,
    weight: weighing.weight || 0,
    net_weight: weighing.net_weight || 0
  };
};
var formatProducts = (item, quantity, list) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
  if (item == null ? void 0 : item.discount_list) {
    item == null ? void 0 : item.discount_list.forEach((d) => {
      if (d == null ? void 0 : d.id) {
        d.order_discount_id = d.id;
      }
    });
  }
  const total = Number(item._extend.total);
  const origin_total = Number(item._extend.origin_total);
  const discount_amount = origin_total - total;
  const bundleDiscountList = getBundleDiscountList(((_b = (_a = item._extend) == null ? void 0 : _a.other) == null ? void 0 : _b.bundle) || []);
  const discount_list = [...(item == null ? void 0 : item.discount_list) || []];
  discount_list.push(...bundleDiscountList);
  const discount_type = discount_amount < 0 ? "change_price" : "discount";
  discount_list.forEach((d) => {
    d.metadata = {
      ...d.metadata,
      unique_identification_number: `${item._extend.unique_identification_number}_${d.discount.resource_id}`
    };
  });
  const notManualDiscount = discount_list.filter(
    (item2) => item2.type !== "product"
  );
  if (item.isCustomItem) {
    item.product_id = 0;
  }
  return {
    "name": item.name,
    "num": quantity || item._extend.quantity,
    "price": formatProductPrice(item),
    "main_product_selling_price": getMainProductSellingPrice(item) ?? formatProductPrice(item),
    "source_product_price": getSourceProductPrice(item),
    "product_id": (item == null ? void 0 : item.product_id) || item.product_id === 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": ((_c = item._extend) == null ? void 0 : _c.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((_e = (_d = item._extend) == null ? void 0 : _d.other) == null ? void 0 : _e.bundle, item.is_price_include_tax),
    "bundle_edit": item._extend.bundle_edit || bundleDiscountList.length ? 1 : 0,
    "product_option_item": formatProductOption((_g = (_f = item._extend) == null ? void 0 : _f.other) == null ? void 0 : _g.option),
    "product_variant_id": ((_i = (_h = item._extend) == null ? void 0 : _h.other) == null ? void 0 : _i.product_variant_id) || 0,
    "discount_list": notManualDiscount,
    metadata: formatProductMetadata(item, list),
    // checkout 需求新增字段
    "calculated_original_price": formatOriginalPrice(item),
    "calculated_selling_price": formatSellingPrice(item),
    "tax_fee": item._extend.tax_fee ?? ((_j = item.metadata) == null ? void 0 : _j.main_product_attached_bundle_tax_fee),
    "custom_deposit_data": item.custom_deposit_data,
    is_price_include_tax: item.is_price_include_tax,
    gift_card: item._giftInfo ? 1 : 0,
    product_sku: formatWeightInfo((_k = item._extend) == null ? void 0 : _k.weighing)
  };
};
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 getHasMultiDayProduct = (data) => {
  const list = data.service.value;
  const isHas = list.some((item) => {
    var _a;
    return (_a = item._extend) == null ? void 0 : _a.groupId;
  });
  return isHas;
};
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 = [];
  let obj = {};
  list.forEach((item) => {
    var _a;
    if (item._extend && !((_a = item._extend) == null ? void 0 : _a.unique_identification_number)) {
      item._extend.unique_identification_number = (0, import_utils5.getUniqueId)();
    }
  });
  list.forEach((item) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
    if (item.isCustomItem) {
      bookings.push({
        ...item._extend,
        product_id: 0,
        // 确保后端接收到的 product_id 为 0
        appointment_status: ((_a = item == null ? void 0 : item._extend) == null ? void 0 : _a.appointment_status) || (data == null ? void 0 : data.appointment_status) || "new"
      });
      return;
    }
    const isMultiDay = (_b = item._extend) == null ? void 0 : _b.groupId;
    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 = ((_c = item._extend) == null ? void 0 : _c.startDate) || ((_d = item._extend) == null ? void 0 : _d.start_date);
      const endDate = ((_e = item._extend) == null ? void 0 : _e.endDate) || ((_f = item._extend) == null ? void 0 : _f.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 isSetGroupId = obj[(_g = item == null ? void 0 : item._extend) == null ? void 0 : _g.groupId];
    const booking = {
      appointment_status: ((_h = item == null ? void 0 : item._extend) == null ? void 0 : _h.appointment_status) || (data == null ? void 0 : data.appointment_status) || "new",
      schedule_event_id: item == null ? void 0 : item.booking_id,
      product: formatProducts(item, void 0, list),
      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,
      sub_type: isDay ? "days" : getSubType(item),
      select_date: start_date,
      start_time,
      end_date,
      start_date,
      duration: isDay || isMultiDay ? 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),
      note: item._extend.note,
      metadata: formatMetaData(item, !isSetGroupId),
      ...eventOtherParams || {},
      ...dayParams
    };
    if (!isSetGroupId) {
      obj[item._extend.groupId] = true;
    }
    if (isDay || isMultiDay) {
      bookings.push(booking);
      return;
    }
    for (let i = 0; i < (item._extend.quantity || 1); i++) {
      const newItem = (0, import_lodash.cloneDeep)(item);
      newItem._extend.unique_identification_number = `${item._extend.unique_identification_number}_${i}`;
      if (i > 0) {
        newItem._extend.surcharge_rounding_remainder = void 0;
        if (((_k = (_j = (_i = newItem._extend) == null ? void 0 : _i.other) == null ? void 0 : _j.bundle) == null ? void 0 : _k.length) > 0) {
          newItem._extend.other.bundle.forEach((bundleItem) => {
            bundleItem.surcharge_rounding_remainder = void 0;
          });
        }
      }
      bookings.push({
        ...booking,
        "product": formatProducts(newItem, 1, list),
        "number": item._extend.capacity ? (0, import_utilsByBooking.getSumCapacity)({ cacheItem: newItem }, false) : newItem._extend.number || 1,
        "holder": getHolderByIndex(booking.holder, i),
        "metadata": formatMetaData(newItem, false)
      });
    }
  });
  return bookings;
};
var formatRelationProducts = (data, type) => {
  var _a;
  const allList = ((_a = data.service) == null ? void 0 : _a.value) || [];
  let list = allList.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) => {
    var _a2;
    if (!((_a2 = item._extend) == null ? void 0 : _a2.unique_identification_number)) {
      const unique_identification_number = (0, import_utils5.getUniqueId)();
      item._extend.unique_identification_number = unique_identification_number;
    }
    return formatProducts(item, void 0, allList);
  });
  return relationProducts || [];
};
var formatNormalProducts = (data) => {
  var _a;
  const allList = ((_a = data.service) == null ? void 0 : _a.value) || [];
  allList.forEach((item) => {
    var _a2;
    if (!((_a2 = item._extend) == null ? void 0 : _a2.unique_identification_number)) {
      const unique_identification_number = (0, import_utils5.getUniqueId)();
      item._extend.unique_identification_number = unique_identification_number;
    }
  });
  let list = allList.filter((item) => (0, import_utils4.isNormalProductByDurationSchedule)(item));
  let relationProducts = [];
  relationProducts = list.map((item) => {
    return formatProducts(item, void 0, allList);
  });
  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, _h;
  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: {},
    business_code: (state == null ? void 0 : state.businessCode) || void 0
  };
  const isDay = (0, import_utils4.isDaysBooking)(state);
  const isHasMultiDay = getHasMultiDayProduct(state);
  const isEvent = (0, import_utils4.isEventBooking)(state);
  if (!state.bookingId || isDay) {
    values.shop_note = formatNote(state);
  }
  if (state.shop_discount && Number(state.shop_discount) > 0) {
    values.shop_discount = state.shop_discount;
    values.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 (isEvent) {
    values.relation_products = formatRelationProducts(
      state,
      "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 || isHasMultiDay) {
    values.metadata.is_multiday_appointment = 1;
  }
  if (!((_g = values == null ? void 0 : values.bookings) == null ? void 0 : _g.length) && !state.order_type) {
    values.type = "virtual";
  }
  values.contacts_info = ((_h = state.contacts_info) == null ? void 0 : _h.value) || null;
  if (state.order_type) {
    values.type = state.order_type;
  }
  return values;
};
var checkHolderIsError = (state) => {
  var _a, _b;
  let errorRowKey = "";
  const holderError = state.service.value.some((item) => {
    var _a2, _b2, _c, _d, _e, _f, _g;
    if ((0, import_utils4.isNormalProductByDurationSchedule)(item)) {
      return false;
    }
    if (item._extend.quantity === 1 && (!item._extend.holder_id || ((_a2 = item._extend.holder_id) == null ? void 0 : _a2.length) === 0)) {
      errorRowKey = (0, import_utils4.genRowKey)((_c = (_b2 = item == null ? void 0 : item._extend) == null ? void 0 : _b2.other) == null ? void 0 : _c.rowKey);
      return true;
    }
    if (item._extend.quantity > 1 && ((_e = (_d = item._extend) == null ? void 0 : _d.holder_id) == null ? void 0 : _e.length) !== item._extend.quantity) {
      errorRowKey = (0, import_utils4.genRowKey)((_g = (_f = item == null ? void 0 : item._extend) == null ? void 0 : _f.other) == null ? void 0 : _g.rowKey);
      return true;
    }
    return false;
  });
  const clientId = (_b = (_a = state.client) == null ? void 0 : _a.value) == null ? void 0 : _b.id;
  if (holderError) {
    const dom = document.getElementById(errorRowKey);
    if (dom) {
      dom.scrollIntoView({
        behavior: "smooth",
        block: "start"
      });
    }
  }
  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,
  formatOriginalPrice,
  formatProductBundle,
  formatProductOption,
  formatProductPrice,
  formatSellingPrice,
  formatValues,
  getBundleDiscountList,
  getHasMultiDayProduct,
  getMainProductSellingPrice,
  getSourceProductPrice
});
