var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/ticketBooking/utils/edit.ts
var edit_exports = {};
__export(edit_exports, {
  formatBookingDetail: () => formatBookingDetail
});
module.exports = __toCommonJS(edit_exports);
var import_dayjs = __toESM(require("dayjs"));
var _isMultiDayProduct = (item) => {
  var _a;
  return (_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.groupId;
};
var _formatHolder = (detail) => {
  var _a, _b;
  return ((_b = (_a = detail == null ? void 0 : detail.holder) == null ? void 0 : _a.form_record) == null ? void 0 : _b[0]) || [];
};
var _formatBookingMetadata = (item) => {
  var _a, _b, _c;
  let holder_id = 0;
  if ((_b = (_a = item.holder) == null ? void 0 : _a.form_record) == null ? void 0 : _b.length) {
    holder_id = item.holder.form_record[0];
  }
  if (!holder_id) {
    holder_id = (_c = item.metadata) == null ? void 0 : _c.holder_id;
  }
  return {
    ...item.metadata || {},
    holder_id
  };
};
var _getDisabledEdit = ({ shop_discount, bookings, payment_status }) => {
  const bookingList = Array.isArray(bookings == null ? void 0 : bookings.list) ? bookings.list : [];
  const appointmentStatus = bookings == null ? void 0 : bookings.appointment_status;
  if (bookingList.some((d) => !d.detail)) {
    return true;
  }
  if (shop_discount && Number(shop_discount)) {
  }
  let list = [
    "unfulfilled",
    "paid",
    "partially_paid",
    "unpaid",
    "payment_processing"
  ];
  if (appointmentStatus === "cancelled" || !list.includes(payment_status)) {
    return true;
  }
  return false;
};
var _formatKey = (data) => {
  var _a, _b, _c, _d, _e, _f;
  const _data = {
    ...data
  };
  let key = `${_data.product_id}`;
  let rowKey = key;
  if (_data.product_variant_id) {
    let str = `_${_data.product_variant_id}`;
    rowKey += str;
  }
  if ((_b = (_a = _data == null ? void 0 : _data.product_sku) == null ? void 0 : _a.option) == null ? void 0 : _b.length) {
    for (const item of (_c = _data == null ? void 0 : _data.product_sku) == null ? void 0 : _c.option) {
      let str = `_${item.option_group_id}_${item.product_option_item_id || item.option_group_item_id}`;
      rowKey += `${str}_${item.quantity || item.num}`;
    }
  }
  if ((_d = _data == null ? void 0 : _data.product_bundle) == null ? void 0 : _d.length) {
    for (const item of _data.product_bundle) {
      let str = `_${item.bundle_group_id}_${item.bundle_product_id}`;
      rowKey += `${str}_${item.quantity || item.num}`;
      if (item == null ? void 0 : item.option) {
        for (const opt of item.option) {
          let str2 = `_${opt.option_group_id}_${opt.product_option_item_id}`;
          key += str2;
          rowKey += `${str2}_${opt.quantity || opt.num}`;
        }
      }
    }
  }
  if (_data == null ? void 0 : _data.session) {
    let str = `_${(_e = _data == null ? void 0 : _data.session) == null ? void 0 : _e.key}`;
    rowKey += str;
  }
  if (_data == null ? void 0 : _data.schedule) {
    let str = `_${(_f = _data == null ? void 0 : _data.schedule) == null ? void 0 : _f.key}`;
    rowKey += str;
  }
  return rowKey;
};
var _formatOptions = (detail) => {
  var _a;
  return (((_a = detail == null ? void 0 : detail.product_sku) == null ? void 0 : _a.option) || []).map((item) => {
    return {
      ...item,
      price: item.price || item.add_price
    };
  });
};
var _restoreProductOtherData = (data) => {
  let { detail, ...resetD } = data;
  if (!detail) {
    return;
  }
  const { discount_list } = detail;
  return {
    rowKey: _formatKey(data),
    product_id: detail.product_id,
    option: _formatOptions(detail),
    bundle: (detail.product_bundle || []).map((bundleItem) => {
      const price = (bundleItem == null ? void 0 : bundleItem.bundle_sum_type) === "markdown" ? `-${Number(bundleItem.bundle_sum_price)}` : Number(bundleItem.bundle_sum_price);
      const discountList = (discount_list || []).filter((item) => {
        var _a, _b;
        return ((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id) === ((_b = bundleItem.metadata) == null ? void 0 : _b.custom_product_bundle_map_id);
      });
      return {
        ...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((optionItem) => {
          return {
            ...optionItem,
            name: optionItem.name["auto"],
            quantity: bundleItem.num,
            product_option_item_id: optionItem.option_group_item_id
          };
        }),
        price: bundleItem.bundle_selling_price ?? price,
        original_total: bundleItem.bundle_selling_price ?? price,
        original_price: bundleItem.bundle_selling_price ?? price,
        product_price: bundleItem.bundle_selling_price ?? price
      };
    }),
    product_variant_id: detail.product_variant_id || 0,
    quantity: Number(detail.product_quantity)
  };
};
var formatProductValues = (data) => {
  return data.map((d) => {
    var _a;
    return {
      ...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: (_a = d.metadata) == null ? void 0 : _a.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: (0, import_dayjs.default)(),
        endDate: (0, import_dayjs.default)(),
        note: d.note
      },
      payment_price: Number(d.payment_price)
    };
  });
};
var formatBookingDetail = (data, modalState) => {
  var _a, _b, _c, _d;
  let {
    customer_id,
    holder,
    customer_name,
    email,
    phone,
    country_calling_code,
    bookings,
    customer,
    note,
    payment_status,
    is_other_entrance_edited,
    shop_discount,
    contacts_info,
    relation_details = []
  } = data;
  if (!bookings) {
    bookings = {
      list: []
    };
  }
  const name = ((customer == null ? void 0 : customer.display_name) || "").trim() || ((customer == null ? void 0 : customer.nickname) || "").trim() || customer_name || "";
  const clientValue = {
    name,
    nickname: name,
    id: customer_id || (customer == null ? void 0 : customer.id) || (customer == null ? void 0 : customer.customer_id) || 1,
    email: customer == null ? void 0 : customer.email,
    phone: (customer == null ? void 0 : customer.phone) || "",
    country_calling_code: (customer == null ? void 0 : customer.country_calling_code) || "",
    cover: (customer == null ? void 0 : customer.cover) || "",
    waiver_form: (customer == null ? void 0 : customer.waiver_form) || [],
    contacts_info: {
      value: contacts_info
    }
  };
  const start_date = (_b = (_a = bookings == null ? void 0 : bookings.list) == null ? void 0 : _a[0]) == null ? void 0 : _b.start_date;
  const dateValue = (0, import_dayjs.default)(
    start_date ? start_date + " " + ((_d = (_c = bookings == null ? void 0 : bookings.list) == null ? void 0 : _c[0]) == null ? void 0 : _d.start_time) : /* @__PURE__ */ new Date()
  );
  const petsValue = holder;
  const getCapacity = (resetD) => {
    var _a2;
    return (_a2 = resetD.metadata) == null ? void 0 : _a2.capacity;
  };
  const getResource = (resetD) => {
    var _a2;
    if (!resetD.relation_type) {
      try {
        if ((resetD == null ? void 0 : resetD.event_resources) && ((_a2 = resetD == null ? void 0 : resetD.event_resources) == null ? void 0 : _a2.length)) {
          return resetD.event_resources.reduce((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;
  };
  let serviceValue = bookings.list.map((d) => {
    var _a2;
    let { detail, product, ...resetD } = d;
    let _detail = detail || mockProduct;
    let start_date2 = (0, import_dayjs.default)(resetD.start_date + " " + resetD.start_time);
    let end_date = (0, import_dayjs.default)(resetD.end_date + " " + resetD.end_time);
    const endDate = (0, import_dayjs.default)(end_date).add(1, "minute");
    const multiObj = {};
    const isMultiDayProduct = _isMultiDayProduct(d);
    if (isMultiDayProduct) {
      multiObj.duration = endDate.diff(start_date2, "days");
    }
    return {
      _id: (Math.random() + "").replace(/\D/g, "").substring(0, 11),
      ..._detail,
      duration: (_detail == null ? void 0 : _detail.duration) || (product == null ? void 0 : product.duration),
      "schedule.ids": (_detail == null ? void 0 : _detail["schedule.ids"]) || (product == null ? 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 ? void 0 : product.product_resource,
      capacity: product == null ? void 0 : product.capacity,
      _extend: {
        ...resetD,
        quantity: Number(_detail.product_quantity),
        startDate: start_date2,
        start_date: start_date2,
        endDate: 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: (_a2 = _detail.metadata) == null ? void 0 : _a2.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 = [...serviceValue || [], ...formatProductValues(relation_details || [])];
  let disabledEdit = _getDisabledEdit(data);
  let channelDisabledEdit = false;
  return {
    ...modalState,
    client: {
      ...modalState.client,
      value: clientValue,
      edit: false,
      showModal: false
    },
    contacts_info: {
      ...(modalState == null ? void 0 : modalState.contacts_info) || {},
      edit: false,
      value: contacts_info,
      client_id: clientValue == null ? void 0 : clientValue.id
    },
    date: {
      ...modalState.date,
      value: dateValue,
      edit: false,
      showModal: false
    },
    addons: {
      ...modalState.addons,
      load: false
    },
    pet: {
      ...modalState.pet,
      showModal: false,
      edit: false
    },
    service: {
      ...modalState.service,
      value: serviceValue,
      resources: [],
      addModal: false,
      editModal: false,
      edit: false
    },
    notes: {
      ...modalState.notes,
      orderNote: note,
      edit: false
    },
    disabledEdit,
    channelDisabledEdit,
    appointment_status: bookings == null ? void 0 : bookings.appointment_status
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  formatBookingDetail
});
