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 { flexibleObj, isNowTime, getNowTime } from "../../utils";
import { isWalkIn } from "../../utils";
import { getSumCapacity } from "../../info2/utilsByBooking";
import { getDays } from "../../utils";
import { appointmentTypes } from "../../info/service2/status";
import { isDaysBooking, formatDayTime, isEventBooking, isNormalProductByDurationSchedule, genRowKey } from "../../info/service2/utils";
import { isArr, locales, sendWarningLog, getUniqueId } from '@pisell/utils';
import { PisellToast } from "@pisell/materials";
import { getClientName } from "../../../../plus/clientName/utils";
import { cloneDeep } from 'lodash';
var formatNote = function formatNote(data) {
  var _data$notes;
  return ((_data$notes = data.notes) === null || _data$notes === void 0 ? void 0 : _data$notes.orderNote) || "";
};
var formatCustomer = function formatCustomer(data) {
  var _data$client, _data$client2, _data$client3, _data$client4, _data$client5, _data$client6;
  var id = (_data$client = data.client) === null || _data$client === void 0 || (_data$client = _data$client.value) === null || _data$client === void 0 ? void 0 : _data$client.id;
  if (isWalkIn(id)) {
    id = 1;
  }
  return {
    customer_id: id,
    customer_name: getClientName((_data$client2 = data.client) === null || _data$client2 === void 0 ? void 0 : _data$client2.value),
    email: (_data$client3 = data.client) === null || _data$client3 === void 0 || (_data$client3 = _data$client3.value) === null || _data$client3 === void 0 ? void 0 : _data$client3.email,
    phone: ((_data$client4 = data.client) === null || _data$client4 === void 0 || (_data$client4 = _data$client4.value) === null || _data$client4 === void 0 ? void 0 : _data$client4.phone) || ((_data$client5 = data.client) === null || _data$client5 === void 0 || (_data$client5 = _data$client5.value) === null || _data$client5 === void 0 ? void 0 : _data$client5.mobile),
    country_calling_code: (_data$client6 = data.client) === null || _data$client6 === void 0 || (_data$client6 = _data$client6.value) === null || _data$client6 === void 0 ? void 0 : _data$client6.country_calling_code
  };
};

/**
 * @title: 套餐单规格信息
 * @description: 
 * @param {any} option
 * @return {*}
 * @Author: zhiwei.Wang
 * @Date: 2024-02-19 09:26
 */
export var formatProductOption = function formatProductOption(option) {
  if (!option) {
    return [];
  }
  return option.map(function (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 || d === void 0 ? void 0 : d.add_price)
    };
  });
};

/**
 * @title: 添加套餐警告
 * @description: 
 * @param {any} data
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-03-05 18:23
 */
var log = function log(data) {
  sendWarningLog({
    title: '预约2.0下单无效的套餐商品数据',
    content: [{
      key: 'bundle',
      value: JSON.stringify(data)
    }, {
      key: '日期',
      value: dayjs().format('YYYY-MM-DD HH:mm')
    }]
  });
};

/**
 * @title: 套餐信息
 * @description: 
 * @param {any} bundle
 * @return {*}
 * @Author: zhiwei.Wang
 * @Date: 2024-02-19 09:26
 */
export var formatProductBundle = function formatProductBundle(bundle) {
  if (!bundle) {
    return;
  }

  // 无效的商品套餐信息
  try {
    var errorBundles = bundle === null || bundle === void 0 ? void 0 : bundle.filter(function (d) {
      return !(d !== null && d !== void 0 && d.group_id) || !(d !== null && d !== void 0 && d.id) || !(d !== null && d !== void 0 && d._bundle_product_id);
    });
    if ((errorBundles === null || errorBundles === void 0 ? void 0 : errorBundles.length) > 0) {
      log(errorBundles);
    }
  } catch (error) {}
  return bundle.map(function (d) {
    return {
      "bundle_group_id": d.group_id,
      "bundle_id": d.id,
      "bundle_product_id": (d === null || d === void 0 ? void 0 : d._bundle_product_id) || (d === null || d === void 0 ? 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 || d === void 0 ? void 0 : d.price),
      // checkout 2.0 新增
      "price_type": d === null || d === void 0 ? void 0 : d.price_type,
      // checkout 2.0 新增
      "option": formatProductOption(d.option)
    };
  });
};
var getSubType = function getSubType(item) {
  var _item$duration;
  var _type = (_item$duration = item.duration) === null || _item$duration === void 0 ? void 0 : _item$duration.type;
  // 目前不支持跨天
  if (_type) {
    return _type;
  }
  return 'minutes';
};
var formatRelationDetails = function formatRelationDetails(list) {
  return (list || []).map(function (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
    };
  });
};
/**
 * @title: 传入资源，如果有子资源，会根据组合资源的 capacity 计算修改子资源的 capacity
 * @description:
 * @param {object} resource
 * @return {*}
 * @Author: jinglin.tan
 */
export var checkSubResourcesCapacity = function checkSubResourcesCapacity(resource) {
  if (resource.children && resource.children.length) {
    var countCapacity = resource.capacity;
    resource.children.forEach(function (child) {
      if (child.capacity <= countCapacity) {
        countCapacity -= child.capacity;
      } else {
        child.capacity = countCapacity;
        countCapacity = 0;
      }
    });
  }
};
export var checkResourcesFormat = function checkResourcesFormat(resources, service, isChild) {
  return resources.map(function (resource) {
    var _resource$children;
    if (!isChild) {
      resource.capacity = getSumCapacity({
        cacheItem: service
      }, false);
    }
    checkSubResourcesCapacity(resource);
    return {
      children: (_resource$children = resource.children) !== null && _resource$children !== void 0 && _resource$children.length ? checkResourcesFormat(resource.children, service, true) : undefined,
      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 = function formatResource(service) {
  if (!service._extend.resource) {
    return undefined;
  }
  try {
    return Object.values(service._extend.resource).reduce(function (pre, cur) {
      var newList = checkResourcesFormat(cur, service);
      pre = pre.concat(newList);
      return pre;
    }, []);
  } catch (err) {
    return undefined;
  }
};

/**
 * @title: 格式化子预约的holder数据
 * @description: 
 * @param {any} state
 * @param {any} item
 * @param {any} values
 * @return {*}
 * @Author: WangHan
 * @Date: 2024-12-07 14:52
 */
var formatHolders = function formatHolders(state, item, values) {
  var _state$bookingConfig, _item$_extend, _item$_extend2, _item$_extend3, _item$_extend4;
  var form_id = (state === null || state === void 0 || (_state$bookingConfig = state.bookingConfig) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.config) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.basic) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.appointment_subject) === null || _state$bookingConfig === void 0 ? void 0 : _state$bookingConfig.form_id) || 0;
  var form_record = (_item$_extend = item._extend) !== null && _item$_extend !== void 0 && _item$_extend.holder_id ? [(_item$_extend2 = item._extend) === null || _item$_extend2 === void 0 ? void 0 : _item$_extend2.holder_id] : [];
  if ((_item$_extend3 = item._extend) !== null && _item$_extend3 !== void 0 && _item$_extend3.holder_id && isArr((_item$_extend4 = item._extend) === null || _item$_extend4 === void 0 ? void 0 : _item$_extend4.holder_id)) {
    var _item$_extend5;
    form_record = (_item$_extend5 = item._extend) === null || _item$_extend5 === void 0 ? void 0 : _item$_extend5.holder_id;
  }
  var obj = {
    customer_id: values.customer_id,
    form_id: form_id,
    form_record: form_record
  };

  // 连锁加盟子店铺 增加参数
  if (state.isFranchisee) {
    obj.is_source_shop = 1;
  }
  return obj;
};

/**
 * @title: 格式化商品单价
 * @description: 
 * @param {any} item
 * @return {*}
 * @Author: WangHan
 * @Date: 2024-12-23 12:05
 */
export var formatProductPrice = function formatProductPrice(item) {
  var _item$_extend6, _item$_extend7, _item$variant;
  var other = (_item$_extend6 = item._extend) === null || _item$_extend6 === void 0 ? void 0 : _item$_extend6.other;
  var price = Number((_item$_extend7 = item._extend) === null || _item$_extend7 === void 0 ? void 0 : _item$_extend7.price);

  // 组合规格: 根据当前选择的组合规格id 和商品的组合规格列表 获取当前商品的价格
  if ((other === null || other === void 0 ? void 0 : other.product_variant_id) > 0 && (item === null || item === void 0 || (_item$variant = item.variant) === null || _item$variant === void 0 ? void 0 : _item$variant.length) > 0) {
    var variant = item.variant.find(function (d) {
      return d.id === (other === null || other === void 0 ? void 0 : other.product_variant_id);
    });
    if (variant) {
      price = Number(variant === null || variant === void 0 ? void 0 : variant.price) || 0;
    }
  }

  // 单规格， 单价 + 单规格价格
  if (other !== null && other !== void 0 && other.option) {
    var _other$option;
    price = other === null || other === void 0 || (_other$option = other.option) === null || _other$option === void 0 ? void 0 : _other$option.reduce(function (pre, cur) {
      return pre + Number(cur.price) * Number(cur.num);
    }, price);
  }
  return price;
};

/**
 * checkout2.0: 单个数量商品总价（原价）,包含所有套餐子商品金额、报价单等等
 * @param item 
 * @returns 
 */
var formatOriginalPrice = function formatOriginalPrice(item) {
  var _item$_extend8;
  return Number((_item$_extend8 = item._extend) === null || _item$_extend8 === void 0 ? void 0 : _item$_extend8.origin_total);
};

/**
 * checkout2.0: 商品维度折扣后价格, original_price - 商品折扣金额（商品手动折扣等等）
 * @param item 
 * @returns 
 */
var formatSellingPrice = function formatSellingPrice(item) {
  var _item$_extend9;
  return Number((_item$_extend9 = item._extend) === null || _item$_extend9 === void 0 ? void 0 : _item$_extend9.total);
};
var formatMetaData = function formatMetaData(item) {
  var _item$_extend10, _item$_extend11, _item$_extend13, _item$_extend15;
  var metadata = {};
  if ((_item$_extend10 = item._extend) !== null && _item$_extend10 !== void 0 && _item$_extend10.capacity) {
    metadata.capacity = item._extend.capacity;
  }
  if (item._extend.groupId) {
    metadata.groupId = item._extend.groupId;
  }
  if ((_item$_extend11 = item._extend) !== null && _item$_extend11 !== void 0 && _item$_extend11.holder_id) {
    var _item$_extend12;
    metadata.holder_id = (_item$_extend12 = item._extend) === null || _item$_extend12 === void 0 ? void 0 : _item$_extend12.holder_id;
  }
  if ((_item$_extend13 = item._extend) !== null && _item$_extend13 !== void 0 && _item$_extend13.multiDayPrices) {
    var _item$_extend14;
    metadata.multiDayPrices = (_item$_extend14 = item._extend) === null || _item$_extend14 === void 0 ? void 0 : _item$_extend14.multiDayPrices;
  }
  if ((_item$_extend15 = item._extend) !== null && _item$_extend15 !== void 0 && _item$_extend15.unique_identification_number) {
    var _item$_extend16;
    metadata.unique_identification_number = (_item$_extend16 = item._extend) === null || _item$_extend16 === void 0 ? void 0 : _item$_extend16.unique_identification_number;
  }
  return metadata;
};

// const formatDiscountList = (list: any[]) => {
//   return list.map(item => {
//     return {
//       amount: item.amount,
//       product_id: item.product_id || item.discount?.product_id,
//       resource_id: item.resource_id || item.discount?.resource_id,
//       title: item.title || item.discount?.title,
//       original_amount: item.original_amount || item.discount?.original_amount,
//       id: item.id,
//       type: item.type,
//     }
//   })
// }

// 获取商品价格 包含组合规格
var getSourceProductPrice = function getSourceProductPrice(item) {
  var _item$_extend17, _item$_extend18, _item$variant2;
  var other = (_item$_extend17 = item._extend) === null || _item$_extend17 === void 0 ? void 0 : _item$_extend17.other;
  var price = Number((_item$_extend18 = item._extend) === null || _item$_extend18 === void 0 ? void 0 : _item$_extend18.price);

  // 组合规格: 根据当前选择的组合规格id 和商品的组合规格列表 获取当前商品的价格
  if ((other === null || other === void 0 ? void 0 : other.product_variant_id) > 0 && (item === null || item === void 0 || (_item$variant2 = item.variant) === null || _item$variant2 === void 0 ? void 0 : _item$variant2.length) > 0) {
    var variant = item.variant.find(function (d) {
      return d.id === (other === null || other === void 0 ? void 0 : other.product_variant_id);
    });
    if (variant) {
      price = Number(variant === null || variant === void 0 ? void 0 : variant.price) || 0;
    }
  }
  return price;
};

/**
 * @title: 格式化商品数据
 * @description: 
 * @param {any} item
 * @return {*}
 * @Author: WangHan
 * @Date: 2024-12-07 14:21
 */
var formatProducts = function formatProducts(item, quantity) {
  var _item$_extend19, _item$_extend20, _item$_extend21, _item$_extend22;
  var total = Number(item._extend.total);
  var origin_total = Number(item._extend.origin_total);
  var discount_amount = origin_total - total;
  var discount_list = (item === null || item === void 0 ? void 0 : item.discount_list) || [];

  // 加价时type使用change_price
  var discount_type = discount_amount < 0 ? 'change_price' : 'discount';
  discount_list.forEach(function (d) {
    d.metadata = _objectSpread(_objectSpread({}, d.metadata), {}, {
      unique_identification_number: "".concat(item._extend.unique_identification_number, "_").concat(d.discount.resource_id)
    });
  });

  // 非手动折扣
  var notManualDiscount = discount_list.filter(function (item) {
    return item.type !== 'product';
  });
  if (item.isCustomItem) {
    item.product_id = 0;
  }
  return {
    "name": item.name,
    "num": quantity || item._extend.quantity,
    "price": formatProductPrice(item),
    "source_product_price": getSourceProductPrice(item),
    "product_id": item !== null && item !== 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": ((_item$_extend19 = item._extend) === null || _item$_extend19 === void 0 ? void 0 : _item$_extend19.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((_item$_extend20 = item._extend) === null || _item$_extend20 === void 0 || (_item$_extend20 = _item$_extend20.other) === null || _item$_extend20 === void 0 ? void 0 : _item$_extend20.bundle),
    "bundle_edit": item._extend.bundle_edit,
    "product_option_item": formatProductOption((_item$_extend21 = item._extend) === null || _item$_extend21 === void 0 || (_item$_extend21 = _item$_extend21.other) === null || _item$_extend21 === void 0 ? void 0 : _item$_extend21.option),
    "product_variant_id": ((_item$_extend22 = item._extend) === null || _item$_extend22 === void 0 || (_item$_extend22 = _item$_extend22.other) === null || _item$_extend22 === void 0 ? void 0 : _item$_extend22.product_variant_id) || 0,
    "discount_list": notManualDiscount,
    metadata: formatMetaData(item),
    // checkout 需求新增字段
    "calculated_original_price": formatOriginalPrice(item),
    "calculated_selling_price": formatSellingPrice(item),
    "tax_fee": item._extend.tax_fee,
    "custom_deposit_data": item.custom_deposit_data
  };
};

/**
 * @title: 格式化跨日预约下的普通商品
 * @description: 
 * @param {any} data
 * @param {any} booking_item
 * @return {*}
 * @Author: WangHan
 * @Date: 2024-12-07 14:48
 */
var formatDayRelationProducts = function formatDayRelationProducts(data, booking_item) {
  var list = data.service.value;

  // 过滤出卡片中的普通商品
  var relation_products = list === null || list === void 0 ? void 0 : list.filter(function (item) {
    var _item$_extend23;
    var holderId = (_item$_extend23 = item._extend) === null || _item$_extend23 === void 0 ? void 0 : _item$_extend23.holder_id;
    var bookingHolderId = booking_item._extend.holder_id;
    var isDayBookingProduct = appointmentTypes.includes(item.extension_type);
    return holderId && holderId === bookingHolderId && !isDayBookingProduct;
  });
  relation_products = relation_products.map(function (item) {
    return formatProducts(item);
  });
  return relation_products || [];
};

/**
 * @title: 格式化跨日预约商品列表
 * @description: 
 * @param {any} data
 * @return {*}
 * @Author: WangHan
 * @Date: 2024-12-17 11:06
 */
var formatAppointmentProductLists = function formatAppointmentProductLists(data) {
  try {
    var _Array$from;
    var list = data.service.value;

    // 找出预约商品
    list = list.filter(function (item) {
      var _item$_extend24;
      return ((_item$_extend24 = item._extend) === null || _item$_extend24 === void 0 ? void 0 : _item$_extend24.holder_id) && !isNormalProductByDurationSchedule(item);
    });

    // 对预约商品列表根据holder_id进行分组
    var groupMap = new Map();
    list.forEach(function (item) {
      var _item$_extend25;
      var key = (_item$_extend25 = item._extend) === null || _item$_extend25 === void 0 ? void 0 : _item$_extend25.holder_id; // 分组键
      if (!groupMap.has(key)) {
        groupMap.set(key, []);
      }
      groupMap.get(key).push(item);
    });

    // 处理每个分组
    // groupMap.forEach(group => {
    //   // 只给第一个数据增加 relation_products
    //   group[0].relation_products = formatDayRelationProducts(data, group[0])
    // });

    // 将分组的结果还原为数组
    var result = (_Array$from = Array.from(groupMap.values())) === null || _Array$from === void 0 ? void 0 : _Array$from.reduce(function (acc, group) {
      return acc.concat(group);
    }, []);
    return list || [];
  } catch (error) {}
};
var getHolderByIndex = function getHolderByIndex(holder, index) {
  var _holder$form_record;
  return _objectSpread(_objectSpread({}, holder), {}, {
    form_record: [holder === null || holder === void 0 || (_holder$form_record = holder.form_record) === null || _holder$form_record === void 0 ? void 0 : _holder$form_record[index]].filter(Boolean)
  });
};

/**
 * @title: 预约商品信息
 * @description: 
 * @param {any} data
 * @return {*}
 * @Author: zhiwei.Wang
 * @Date: 2024-02-19 09:26
 */
var formatBookings = function formatBookings(data, values) {
  // 是否是跨日预约
  var isDay = isDaysBooking(data);
  var isEvent = isEventBooking(data);
  // todo 预约商品 普通商品 字段添加
  var list = data.service.value;

  // 格式化跨日预约商品列表
  if (isDay) {
    list = formatAppointmentProductLists(data);
  }
  if (isEvent) {
    list = list.filter(function (d) {
      var _d$_extend;
      return (_d$_extend = d._extend) === null || _d$_extend === void 0 ? void 0 : _d$_extend.holder_id;
    });
  }
  if (!isDay && !isEvent) {
    list = list.filter(function (item) {
      return !isNormalProductByDurationSchedule(item);
    });
  }
  var dayParams = {};
  var eventOtherParams = data.eventOtherParams;
  var bookings = [];
  list.forEach(function (item) {
    var _item$_extend30;
    // 自定义商品直接使用 _extend 中的数据，并将 id 转换为 0 提交给后端
    if (item.isCustomItem) {
      bookings.push(_objectSpread(_objectSpread({}, item._extend), {}, {
        product_id: 0 // 确保后端接收到的 product_id 为 0
      }));
      return;
    }
    var start_date = item._extend.startDate.format('YYYY-MM-DD');
    var end_date = item._extend.endDate.format('YYYY-MM-DD');
    var end_time = item._extend.endDate.format("HH:mm");
    var start_time = item._extend.startDate.format("HH:mm");

    // 更新当前时间
    if (isNowTime(item._extend.start_time)) {
      start_time = getNowTime();
    }
    dayParams.holder = formatHolders(data, item, values);

    // 跨日预约下的普通商品
    if (isDay) {
      var _item$_extend26, _item$_extend27, _item$_extend28, _item$_extend29;
      var startDate = ((_item$_extend26 = item._extend) === null || _item$_extend26 === void 0 ? void 0 : _item$_extend26.startDate) || ((_item$_extend27 = item._extend) === null || _item$_extend27 === void 0 ? void 0 : _item$_extend27.start_date);
      var endDate = ((_item$_extend28 = item._extend) === null || _item$_extend28 === void 0 ? void 0 : _item$_extend28.endDate) || ((_item$_extend29 = item._extend) === null || _item$_extend29 === void 0 ? void 0 : _item$_extend29.end_date);
      var _formatDayTime = formatDayTime({
          startDate: startDate,
          endDate: endDate
        }),
        startDateTime = _formatDayTime.startDateTime,
        endDateTime = _formatDayTime.endDateTime;

      // 根据营业日时间更新时间
      start_date = dayjs(startDateTime).format('YYYY-MM-DD');
      start_time = dayjs(startDateTime).format('HH:mm');
      end_date = dayjs(endDateTime).format('YYYY-MM-DD');
      end_time = dayjs(endDateTime).format('HH:mm');
      dayParams.relation_products = (item === null || item === void 0 ? void 0 : item.relation_products) || [];
      dayParams.holder = formatHolders(data, item, values);
      dayParams.duration = getDays({
        _extend: {
          start_date: startDateTime,
          end_date: endDateTime
        }
      }, 'minutes');
    }
    if (!((_item$_extend30 = item._extend) !== null && _item$_extend30 !== void 0 && _item$_extend30.unique_identification_number)) {
      var unique_identification_number = getUniqueId();
      item._extend.unique_identification_number = unique_identification_number;
    }
    var booking = _objectSpread(_objectSpread({
      "product": formatProducts(item),
      "metadata": formatMetaData(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 : flexibleObj.getValue(item._extend.duration),
      "number": item._extend.capacity ? getSumCapacity({
        cacheItem: item
      }) : item._extend.number || 1,
      "resources": formatResource(item),
      "note": item._extend.note
    }, eventOtherParams || {}), dayParams);
    if (isDay) {
      bookings.push(booking);
      return;
    }
    for (var i = 0; i < (item._extend.quantity || 1); i++) {
      var newItem = cloneDeep(item);
      newItem._extend.unique_identification_number = "".concat(item._extend.unique_identification_number, "_").concat(i);
      bookings.push(_objectSpread(_objectSpread({}, booking), {}, {
        "product": formatProducts(newItem, 1),
        "number": item._extend.capacity ? getSumCapacity({
          cacheItem: newItem
        }, false) : newItem._extend.number || 1,
        "holder": getHolderByIndex(booking.holder, i),
        "metadata": formatMetaData(newItem)
      }));
    }
  });
  return bookings;
};

/**
 * @title: 普通商品
 * @description: 
 * @param {any} data
 * @return {*}
 * @Author: WangHan
 * @Date: 2024-12-06 21:26
 */
var formatRelationProducts = function formatRelationProducts(data, type) {
  var _data$service;
  var list = ((_data$service = data.service) === null || _data$service === void 0 ? void 0 : _data$service.value) || [];
  // open items 下的商品
  list = list.filter(function (item) {
    var _item$_extend31;
    return (
      // 跨日预约把普通商品加在relationProducts 活动预约按之前逻辑
      type === 'day' ? isNormalProductByDurationSchedule(item) : ((_item$_extend31 = item._extend) === null || _item$_extend31 === void 0 ? void 0 : _item$_extend31.holder_id) && appointmentTypes.includes(item.extension_type)
    );
  });
  var relationProducts = [];
  relationProducts = list.map(function (item) {
    var _item$_extend32;
    if (!((_item$_extend32 = item._extend) !== null && _item$_extend32 !== void 0 && _item$_extend32.unique_identification_number)) {
      var unique_identification_number = getUniqueId();
      item._extend.unique_identification_number = unique_identification_number;
    }
    return formatProducts(item);
  });
  return relationProducts || [];
};

// 非预约商品格式化
var formatNormalProducts = function formatNormalProducts(data) {
  var _data$service2;
  var list = ((_data$service2 = data.service) === null || _data$service2 === void 0 ? void 0 : _data$service2.value) || [];
  // 非预约的商品
  list = list.filter(function (item) {
    return isNormalProductByDurationSchedule(item);
  });
  var relationProducts = [];
  relationProducts = list.map(function (item) {
    var _item$_extend33;
    if (!((_item$_extend33 = item._extend) !== null && _item$_extend33 !== void 0 && _item$_extend33.unique_identification_number)) {
      var unique_identification_number = getUniqueId();
      item._extend.unique_identification_number = unique_identification_number;
    }
    return formatProducts(item);
  });
  return relationProducts || [];
};

/**
 * @title: 预约主体信息
 * @description: 
 * @param {any} state
 * @param {any} values
 * @return {*}
 * @Author: zhiwei.Wang
 * @Date: 2024-02-19 09:26
 */
var formatHolder = function formatHolder(state, values) {
  var _state$bookingConfig2, _state$pet$value, _state$pet;
  var form_id = (state === null || state === void 0 || (_state$bookingConfig2 = state.bookingConfig) === null || _state$bookingConfig2 === void 0 || (_state$bookingConfig2 = _state$bookingConfig2.config) === null || _state$bookingConfig2 === void 0 || (_state$bookingConfig2 = _state$bookingConfig2.basic) === null || _state$bookingConfig2 === void 0 || (_state$bookingConfig2 = _state$bookingConfig2.appointment_subject) === null || _state$bookingConfig2 === void 0 ? void 0 : _state$bookingConfig2.form_id) || 0;
  var petValue = ((_state$pet$value = state.pet.value) === null || _state$pet$value === void 0 ? void 0 : _state$pet$value[form_id]) || [];
  var obj = {
    customer_id: values.customer_id,
    form_id: form_id,
    form_record: petValue.map(function (d) {
      return d.form_record_id;
    })
  };

  // 如果 value 为 空 并且originValue有值, 说明接口还没返回 这时候直接用接口的值
  if (!petValue.length && (_state$pet = state.pet) !== null && _state$pet !== void 0 && (_state$pet = _state$pet.originValue) !== null && _state$pet !== void 0 && (_state$pet = _state$pet.form_record) !== null && _state$pet !== void 0 && _state$pet.length) {
    obj.form_record = state.pet.originValue.form_record;
  }

  // 连锁加盟子店铺 增加参数
  if (state.isFranchisee) {
    obj.is_source_shop = 1;
  }
  return obj;
};

// https://menutech.w.eolink.com/home/api-studio/inside/K2bI3n465fe7244b3dbd6679d87caedfbb0636bf8c29eda/api/2778666/detail/53001549?spaceKey=menutech
export var formatValues = function formatValues(state) {
  var _values, _values3;
  var 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: {}
  };
  var isDay = isDaysBooking(state);
  var isEvent = isEventBooking(state);
  if (!state.bookingId || isDay) {
    // 备注
    values.shop_note = formatNote(state);
  }

  // 提交手动价格调整（shop_discount）
  if (state.shop_discount && Number(state.shop_discount) > 0) {
    values.shop_discount = state.shop_discount;
    values.shop_discount_reason = '';
  }
  if (state.contacts) {
    var _state$contacts;
    values.contacts = ((_state$contacts = state.contacts) === null || _state$contacts === void 0 ? void 0 : _state$contacts.value) || [];
  }
  // 客户
  var customer = formatCustomer(state);
  values = _objectSpread(_objectSpread({}, values), customer);

  // 主体
  values.holder = formatHolder(state, values);
  // todo
  if (isDay || isEvent) {
    values.relation_products = formatRelationProducts(state, isDay ? "day" : "event");
  } else {
    var _values$relation_prod;
    values.relation_products = formatNormalProducts(state);
    if (!((_values$relation_prod = values.relation_products) !== null && _values$relation_prod !== void 0 && _values$relation_prod.length)) {
      values.relation_products = undefined;
    }
  }
  // 服务
  values.bookings = formatBookings(state, values);

  // 日期
  if ((_values = values) !== null && _values !== void 0 && (_values = _values.bookings) !== null && _values !== void 0 && (_values = _values[0]) !== null && _values !== void 0 && _values.select_date && !(state !== null && state !== void 0 && state.bookingId)) {
    var _values2;
    // 默认用第一个预约的选择日期并且是新建，编辑目前先不处理，用于解决附加费问题
    var startTime = "".concat((_values2 = values) === null || _values2 === void 0 || (_values2 = _values2.bookings) === null || _values2 === void 0 || (_values2 = _values2[0]) === null || _values2 === void 0 ? void 0 : _values2.start_time);
    startTime = "".concat(startTime || '00:00', ":00");
    values.schedule_date = "".concat(values.bookings[0].select_date, " ").concat(startTime);
  }

  // 跨日预约增加多余参数区分
  if (isDay) {
    values.metadata.is_multiday_appointment = 1;
    // is_bundle_price_calculated 用于区分商品的price是总价，还是商品的单价
    // values.is_bundle_price_calculated = 1
  }
  if (!((_values3 = values) !== null && _values3 !== void 0 && (_values3 = _values3.bookings) !== null && _values3 !== void 0 && _values3.length)) {
    values.type = 'virtual';
  }
  return values;
};

// 检测预约主体是否为空
export var checkHolderIsError = function checkHolderIsError(state) {
  var _state$client;
  var errorRowKey = "";
  var holderError = state.service.value.some(function (item) {
    var _item$_extend35;
    // 普通商品不需要检测holder
    if (isNormalProductByDurationSchedule(item)) {
      return false;
    }
    if (item._extend.quantity === 1 && !item._extend.holder_id) {
      var _item$_extend34;
      errorRowKey = genRowKey(item === null || item === void 0 || (_item$_extend34 = item._extend) === null || _item$_extend34 === void 0 || (_item$_extend34 = _item$_extend34.other) === null || _item$_extend34 === void 0 ? void 0 : _item$_extend34.rowKey);
      return true;
    }
    if (item._extend.quantity > 1 && ((_item$_extend35 = item._extend) === null || _item$_extend35 === void 0 || (_item$_extend35 = _item$_extend35.holder_id) === null || _item$_extend35 === void 0 ? void 0 : _item$_extend35.length) !== item._extend.quantity) {
      var _item$_extend36;
      errorRowKey = genRowKey(item === null || item === void 0 || (_item$_extend36 = item._extend) === null || _item$_extend36 === void 0 || (_item$_extend36 = _item$_extend36.other) === null || _item$_extend36 === void 0 ? void 0 : _item$_extend36.rowKey);
      return true;
    }
    return false;
  });
  var clientId = (_state$client = state.client) === null || _state$client === void 0 || (_state$client = _state$client.value) === null || _state$client === void 0 ? void 0 : _state$client.id;
  if (holderError) {
    var dom = document.getElementById(errorRowKey);
    if (dom) {
      dom.scrollIntoView({
        behavior: 'smooth',
        block: 'start'
      });
    }
  }
  if (holderError && (!clientId || clientId === 0 || clientId === 1)) {
    PisellToast({
      content: locales.getText('pisell2.text.please-select-the-customer-first')
    });
  }
  return holderError;
};