var _excluded = ["detail"],
  _excluded2 = ["detail", "product"];
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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
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); }
// 迁移自： https://v2.pisell.com/lowcode/33

import dayjs from 'dayjs';
var _isMultiDayProduct = function _isMultiDayProduct(item) {
  var _item$metadata;
  return item === null || item === void 0 || (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.groupId;
};
var _formatHolder = function _formatHolder(detail) {
  var _detail$holder;
  return (detail === null || detail === void 0 || (_detail$holder = detail.holder) === null || _detail$holder === void 0 || (_detail$holder = _detail$holder.form_record) === null || _detail$holder === void 0 ? void 0 : _detail$holder[0]) || [];
};
var _formatBookingMetadata = function _formatBookingMetadata(item) {
  var _item$holder;
  var holder_id = 0;
  if ((_item$holder = item.holder) !== null && _item$holder !== void 0 && (_item$holder = _item$holder.form_record) !== null && _item$holder !== void 0 && _item$holder.length) {
    holder_id = item.holder.form_record[0];
  }
  if (!holder_id) {
    var _item$metadata2;
    holder_id = (_item$metadata2 = item.metadata) === null || _item$metadata2 === void 0 ? void 0 : _item$metadata2.holder_id;
  }
  return _objectSpread(_objectSpread({}, item.metadata || {}), {}, {
    holder_id: holder_id
  });
};
var _getDisabledEdit = function _getDisabledEdit(_ref) {
  var shop_discount = _ref.shop_discount,
    bookings = _ref.bookings,
    payment_status = _ref.payment_status;
  var bookingList = Array.isArray(bookings === null || bookings === void 0 ? void 0 : bookings.list) ? bookings.list : [];
  var appointmentStatus = bookings === null || bookings === void 0 ? void 0 : bookings.appointment_status;

  // 如果预约列表里没有商品新信息, 则任何信息都不能编辑
  if (bookingList.some(function (d) {
    return !d.detail;
  })) {
    return true;
  }

  //有整单折扣 不可编辑
  if (shop_discount && Number(shop_discount)) {
    // return true;
  }
  var list = ["unfulfilled", "paid", "partially_paid", "unpaid", "payment_processing"];
  // (支付状态 = 已支付 || 部分付款 || 未付款 || 支付处理中) && 预约状态 !== 已取消
  if (appointmentStatus === "cancelled" || !list.includes(payment_status)) {
    return true;
  }
  return false;
};
var _formatKey = function _formatKey(data) {
  var _data$product_sku, _data$product_bundle;
  var _data = _objectSpread({}, data);
  var key = "".concat(_data.product_id);
  var rowKey = key;

  // 组合规格
  if (_data.product_variant_id) {
    var str = "_".concat(_data.product_variant_id);
    rowKey += str;
  }

  // 单规格
  if (_data !== null && _data !== void 0 && (_data$product_sku = _data.product_sku) !== null && _data$product_sku !== void 0 && (_data$product_sku = _data$product_sku.option) !== null && _data$product_sku !== void 0 && _data$product_sku.length) {
    var _data$product_sku2;
    var _iterator = _createForOfIteratorHelper(_data === null || _data === void 0 || (_data$product_sku2 = _data.product_sku) === null || _data$product_sku2 === void 0 ? void 0 : _data$product_sku2.option),
      _step;
    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var item = _step.value;
        var _str = "_".concat(item.option_group_id, "_").concat(item.product_option_item_id || item.option_group_item_id);
        rowKey += "".concat(_str, "_").concat(item.quantity || item.num);
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
  }

  // 套餐
  if (_data !== null && _data !== void 0 && (_data$product_bundle = _data.product_bundle) !== null && _data$product_bundle !== void 0 && _data$product_bundle.length) {
    var _iterator2 = _createForOfIteratorHelper(_data.product_bundle),
      _step2;
    try {
      for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
        var _item = _step2.value;
        var _str2 = "_".concat(_item.bundle_group_id, "_").concat(_item.bundle_product_id);
        rowKey += "".concat(_str2, "_").concat(_item.quantity || _item.num);
        // 如果有单规格
        if (_item !== null && _item !== void 0 && _item.option) {
          var _iterator3 = _createForOfIteratorHelper(_item.option),
            _step3;
          try {
            for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
              var opt = _step3.value;
              var _str3 = "_".concat(opt.option_group_id, "_").concat(opt.product_option_item_id);
              key += _str3;
              rowKey += "".concat(_str3, "_").concat(opt.quantity || opt.num);
            }
          } catch (err) {
            _iterator3.e(err);
          } finally {
            _iterator3.f();
          }
        }
      }
    } catch (err) {
      _iterator2.e(err);
    } finally {
      _iterator2.f();
    }
  }

  // session time
  if (_data !== null && _data !== void 0 && _data.session) {
    var _data$session;
    var _str4 = "_".concat(_data === null || _data === void 0 || (_data$session = _data.session) === null || _data$session === void 0 ? void 0 : _data$session.key);
    rowKey += _str4;
  }
  if (_data !== null && _data !== void 0 && _data.schedule) {
    var _data$schedule;
    var _str5 = "_".concat(_data === null || _data === void 0 || (_data$schedule = _data.schedule) === null || _data$schedule === void 0 ? void 0 : _data$schedule.key);
    rowKey += _str5;
  }
  return rowKey;
};
var _formatOptions = function _formatOptions(detail) {
  var _detail$product_sku;
  return ((detail === null || detail === void 0 || (_detail$product_sku = detail.product_sku) === null || _detail$product_sku === void 0 ? void 0 : _detail$product_sku.option) || []).map(function (item) {
    return _objectSpread(_objectSpread({}, item), {}, {
      price: item.price || item.add_price
    });
  });
};

// 还原商品other数据
var _restoreProductOtherData = function _restoreProductOtherData(data) {
  var detail = data.detail,
    resetD = _objectWithoutProperties(data, _excluded);
  if (!detail) {
    return;
  }
  var discount_list = detail.discount_list;
  return {
    rowKey: _formatKey(data),
    product_id: detail.product_id,
    option: _formatOptions(detail),
    bundle: (detail.product_bundle || []).map(function (bundleItem) {
      var _bundleItem$bundle_se, _bundleItem$bundle_se2, _bundleItem$bundle_se3, _bundleItem$bundle_se4;
      var price = (bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.bundle_sum_type) === 'markdown' ? "-".concat(Number(bundleItem.bundle_sum_price)) : Number(bundleItem.bundle_sum_price);
      var discountList = (discount_list || []).filter(function (item) {
        var _item$metadata3, _bundleItem$metadata;
        return (item === null || item === void 0 || (_item$metadata3 = item.metadata) === null || _item$metadata3 === void 0 ? void 0 : _item$metadata3.custom_product_bundle_map_id) === ((_bundleItem$metadata = bundleItem.metadata) === null || _bundleItem$metadata === void 0 ? void 0 : _bundleItem$metadata.custom_product_bundle_map_id);
      });
      return _objectSpread(_objectSpread({}, bundleItem), {}, {
        booking_id: data.id,
        title: bundleItem.bundle_title,
        discount_list: discountList,
        group_id: bundleItem.bundle_group_id,
        id: bundleItem.bundle_id,
        product_id: bundleItem.bundle_product_id,
        _bundle_product_id: bundleItem.bundle_product_id,
        bundle_variant_id: bundleItem.bundle_variant_id,
        quantity: bundleItem.num,
        option: bundleItem.option.map(function (optionItem) {
          return _objectSpread(_objectSpread({}, optionItem), {}, {
            name: optionItem.name["auto"],
            quantity: bundleItem.num,
            product_option_item_id: optionItem.option_group_item_id
          });
        }),
        price: (_bundleItem$bundle_se = bundleItem.bundle_selling_price) !== null && _bundleItem$bundle_se !== void 0 ? _bundleItem$bundle_se : price,
        original_total: (_bundleItem$bundle_se2 = bundleItem.bundle_selling_price) !== null && _bundleItem$bundle_se2 !== void 0 ? _bundleItem$bundle_se2 : price,
        original_price: (_bundleItem$bundle_se3 = bundleItem.bundle_selling_price) !== null && _bundleItem$bundle_se3 !== void 0 ? _bundleItem$bundle_se3 : price,
        product_price: (_bundleItem$bundle_se4 = bundleItem.bundle_selling_price) !== null && _bundleItem$bundle_se4 !== void 0 ? _bundleItem$bundle_se4 : price
      });
    }),
    product_variant_id: detail.product_variant_id || 0,
    quantity: Number(detail.product_quantity)
  };
};
var formatProductValues = function formatProductValues(data) {
  return data.map(function (d) {
    var _d$metadata;
    return _objectSpread(_objectSpread({}, d), {}, {
      _id: (Math.random() + '').replace(/\D/g, '').substring(0, 11),
      id: d.product_id,
      title: d.product_title,
      quantity: d.product_quantity,
      cover: d.product_cover,
      price: Number(d.source_product_price),
      booking_id: d.order_id,
      isEditBooking: true,
      _extend: {
        main_product_selling_price: (_d$metadata = d.metadata) === null || _d$metadata === void 0 ? void 0 : _d$metadata.main_product_selling_price,
        discount_reason: d.product_discount_reason,
        order_detail_id: d.order_detail_id,
        discount_per: d.discount_per,
        discount_type: d.discount_type,
        discountway: d.discountway,
        product_name: d.product_title,
        total: Number(d.selling_price),
        origin_total: Number(d.original_price),
        payment_price: Number(d.payment_price),
        price: Number(d.source_product_price),
        quantity: d.product_quantity,
        bundle_edit: 0,
        other: _restoreProductOtherData(d),
        startDate: dayjs(),
        endDate: dayjs(),
        note: d.note
      },
      payment_price: Number(d.payment_price)
    });
  });
};
export var formatBookingDetail = function formatBookingDetail(data, modalState) {
  var _bookings, _bookings2, _bookings3;
  var customer_id = data.customer_id,
    holder = data.holder,
    customer_name = data.customer_name,
    email = data.email,
    phone = data.phone,
    country_calling_code = data.country_calling_code,
    bookings = data.bookings,
    customer = data.customer,
    note = data.note,
    payment_status = data.payment_status,
    is_other_entrance_edited = data.is_other_entrance_edited,
    shop_discount = data.shop_discount,
    contacts_info = data.contacts_info,
    _data$relation_detail = data.relation_details,
    relation_details = _data$relation_detail === void 0 ? [] : _data$relation_detail;
  if (!bookings) {
    bookings = {
      list: []
    };
  }
  var name = ((customer === null || customer === void 0 ? void 0 : customer.display_name) || "").trim() || ((customer === null || customer === void 0 ? void 0 : customer.nickname) || "").trim() || customer_name || "";
  var clientValue = {
    name: name,
    nickname: name,
    id: customer_id || (customer === null || customer === void 0 ? void 0 : customer.id) || (customer === null || customer === void 0 ? void 0 : customer.customer_id) || 1,
    email: customer === null || customer === void 0 ? void 0 : customer.email,
    phone: (customer === null || customer === void 0 ? void 0 : customer.phone) || "",
    country_calling_code: (customer === null || customer === void 0 ? void 0 : customer.country_calling_code) || "",
    cover: (customer === null || customer === void 0 ? void 0 : customer.cover) || "",
    waiver_form: (customer === null || customer === void 0 ? void 0 : customer.waiver_form) || [],
    contacts_info: {
      value: contacts_info
    }
  };
  var start_date = (_bookings = bookings) === null || _bookings === void 0 || (_bookings = _bookings.list) === null || _bookings === void 0 || (_bookings = _bookings[0]) === null || _bookings === void 0 ? void 0 : _bookings.start_date;
  var dateValue = dayjs(start_date ? start_date + " " + ((_bookings2 = bookings) === null || _bookings2 === void 0 || (_bookings2 = _bookings2.list) === null || _bookings2 === void 0 || (_bookings2 = _bookings2[0]) === null || _bookings2 === void 0 ? void 0 : _bookings2.start_time) : new Date());
  var petsValue = holder;
  var getCapacity = function getCapacity(resetD) {
    var _resetD$metadata;
    return (_resetD$metadata = resetD.metadata) === null || _resetD$metadata === void 0 ? void 0 : _resetD$metadata.capacity;
  };
  var getResource = function getResource(resetD) {
    if (!resetD.relation_type) {
      try {
        var _resetD$event_resourc;
        if (resetD !== null && resetD !== void 0 && resetD.event_resources && resetD !== null && resetD !== void 0 && (_resetD$event_resourc = resetD.event_resources) !== null && _resetD$event_resourc !== void 0 && _resetD$event_resourc.length) {
          return resetD.event_resources.reduce(function (pre, cur) {
            if (pre[cur.form_id]) {
              pre[cur.form_id].push(cur);
            } else {
              pre[cur.form_id] = [cur];
            }
            return pre;
          }, {});
        }
      } catch (err) {
        debugger;
      }
    }
    return resetD.relation_id;
  };
  var serviceValue = bookings.list.map(function (d) {
    var _detail$metadata;
    var detail = d.detail,
      product = d.product,
      resetD = _objectWithoutProperties(d, _excluded2);
    var _detail = detail || mockProduct;
    var start_date = dayjs(resetD.start_date + " " + resetD.start_time);
    var end_date = dayjs(resetD.end_date + " " + resetD.end_time);
    var endDate = dayjs(end_date).add(1, 'minute');
    var multiObj = {};
    var isMultiDayProduct = _isMultiDayProduct(d);
    if (isMultiDayProduct) {
      multiObj.duration = endDate.diff(start_date, 'days');
    }
    return _objectSpread(_objectSpread({
      _id: (Math.random() + "").replace(/\D/g, "").substring(0, 11)
    }, _detail), {}, {
      duration: (_detail === null || _detail === void 0 ? void 0 : _detail.duration) || (product === null || product === void 0 ? void 0 : product.duration),
      "schedule.ids": (_detail === null || _detail === void 0 ? void 0 : _detail["schedule.ids"]) || (product === null || product === void 0 ? void 0 : product["schedule.ids"]),
      booking_id: resetD.id,
      id: _detail.product_id,
      title: _detail.product_title,
      quantity: Number(_detail.product_quantity),
      cover: _detail.product_cover,
      // 商品本身的价格
      price: Number(_detail.source_product_price),
      relation_form_name: resetD.relation_form_name,
      product_resource: product === null || product === void 0 ? void 0 : product.product_resource,
      capacity: product === null || product === void 0 ? void 0 : product.capacity,
      _extend: _objectSpread(_objectSpread(_objectSpread({}, resetD), {}, {
        quantity: Number(_detail.product_quantity),
        startDate: start_date,
        start_date: start_date,
        endDate: end_date,
        end_date: end_date,
        holder_id: _formatHolder(resetD),
        // 商品本身的价格
        price: Number(_detail.source_product_price),
        product_name: _detail.product_title,
        discount_reason: _detail.product_discount_reason,
        // resource: resetD.relation_id,
        order_detail_id: _detail.order_detail_id,
        // 总价: 商品本身价格 + 套餐价格 + 单规格价格   包含折扣
        total: Number(_detail.selling_price),
        // 原总价: 商品本身价格 + 套餐价格 + 单规格价格 不包含折扣
        origin_total: Number(_detail.original_price),
        payment_price: Number(_detail.payment_price),
        main_product_selling_price: (_detail$metadata = _detail.metadata) === null || _detail$metadata === void 0 ? void 0 : _detail$metadata.main_product_selling_price,
        other: _restoreProductOtherData(d),
        // 是否编辑了套餐 默认为0  如果编辑之后为1
        bundle_edit: 0,
        discount_per: _detail.discount_per,
        discount_type: _detail.discount_type,
        discountway: _detail.discountway,
        capacity: getCapacity(resetD),
        resource: getResource(resetD)
      }, multiObj), _formatBookingMetadata(d))
    });
  });
  serviceValue = [].concat(_toConsumableArray(serviceValue || []), _toConsumableArray(formatProductValues(relation_details || [])));
  var disabledEdit = _getDisabledEdit(data);
  var channelDisabledEdit = false;
  return _objectSpread(_objectSpread({}, modalState), {}, {
    client: _objectSpread(_objectSpread({}, modalState.client), {}, {
      value: clientValue,
      edit: false,
      showModal: false
    }),
    contacts_info: _objectSpread(_objectSpread({}, (modalState === null || modalState === void 0 ? void 0 : modalState.contacts_info) || {}), {}, {
      edit: false,
      value: contacts_info,
      client_id: clientValue === null || clientValue === void 0 ? void 0 : clientValue.id
    }),
    date: _objectSpread(_objectSpread({}, modalState.date), {}, {
      value: dateValue,
      edit: false,
      showModal: false
    }),
    addons: _objectSpread(_objectSpread({}, modalState.addons), {}, {
      load: false
    }),
    pet: _objectSpread(_objectSpread({}, modalState.pet), {}, {
      showModal: false,
      edit: false
    }),
    service: _objectSpread(_objectSpread({}, modalState.service), {}, {
      value: serviceValue,
      resources: [],
      addModal: false,
      editModal: false,
      edit: false
    }),
    notes: _objectSpread(_objectSpread({}, modalState.notes), {}, {
      orderNote: note,
      edit: false
    }),
    disabledEdit: disabledEdit,
    channelDisabledEdit: channelDisabledEdit,
    appointment_status: (_bookings3 = bookings) === null || _bookings3 === void 0 ? void 0 : _bookings3.appointment_status
  });
};