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/Sales/Checkout/utils.ts
var utils_exports = {};
__export(utils_exports, {
  formatProductBundle: () => formatProductBundle,
  formatProductOption: () => formatProductOption,
  formatValues: () => formatValues,
  getFormParams: () => getFormParams
});
module.exports = __toCommonJS(utils_exports);
var import_decimal = __toESM(require("decimal.js"));
var formatProductOption = (option) => {
  if (!option) {
    return [];
  }
  return option.map((d) => {
    return {
      num: d.num,
      option_group_item_id: d.product_option_item_id,
      option_group_id: d.option_group_id
    };
  });
};
var formatProductBundle = (bundle) => {
  if (!bundle) {
    return;
  }
  return bundle.map((d) => {
    return {
      bundle_group_id: d.group_id,
      bundle_id: d.id,
      bundle_product_id: d._bundle_product_id,
      bundle_variant_id: d.bundle_variant_id,
      num: d.num,
      extension_id: d.extension_id,
      extension_type: d.extension_type,
      option: formatProductOption(d.option)
    };
  });
};
var formatBookings = (list) => {
  let bookings = [];
  bookings = list.map((item) => {
    var _a, _b, _c;
    const newOriginData = { ...item._originData || {} };
    const product = (newOriginData == null ? void 0 : newOriginData.product) || {};
    const discount_list = product == null ? void 0 : product.discount_list;
    const main_product_discount = discount_list == null ? void 0 : discount_list.filter(
      (d) => {
        var _a2;
        return !((_a2 = d.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id);
      }
    );
    if (main_product_discount == null ? void 0 : main_product_discount.length) {
      product.metadata = {
        product_discount_difference: handleProductDiscountDifference([
          main_product_discount
        ])
      };
    }
    console.log("main_product_discount", main_product_discount);
    if ((_a = product == null ? void 0 : product.product_bundle) == null ? void 0 : _a.length) {
      product.product_bundle = (_c = (_b = product == null ? void 0 : product.product_bundle) == null ? void 0 : _b.map) == null ? void 0 : _c.call(_b, (v) => {
        var _a2, _b2, _c2, _d;
        const bundle_item_discount = [];
        (_b2 = (_a2 = item == null ? void 0 : item.selectedBundle) == null ? void 0 : _a2.forEach) == null ? void 0 : _b2.call(_a2, (s) => {
          if ((s == null ? void 0 : s.id) === (v == null ? void 0 : v.bundle_id) && (s == null ? void 0 : s.group_id) === (v == null ? void 0 : v.bundle_group_id) && (s == null ? void 0 : s._bundle_product_id) === (v == null ? void 0 : v.bundle_product_id)) {
            bundle_item_discount.push(...(s == null ? void 0 : s.discount_list) || []);
          }
        });
        console.log("bundle_item_discount", bundle_item_discount);
        const newBundleItem = { ...v || {} };
        if (bundle_item_discount == null ? void 0 : bundle_item_discount.length) {
          newBundleItem.metadata = {
            custom_product_bundle_map_id: (_d = (_c2 = bundle_item_discount == null ? void 0 : bundle_item_discount[0]) == null ? void 0 : _c2.metadata) == null ? void 0 : _d.custom_product_bundle_map_id,
            product_discount_difference: handleProductDiscountDifference(bundle_item_discount)
          };
        }
        return newBundleItem;
      });
    }
    newOriginData.product = product;
    return newOriginData;
  });
  console.log("bookings>>>>", bookings);
  return bookings;
};
var handleProductDiscountDifference = (discount_list) => {
  const product_discount_difference = discount_list == null ? void 0 : discount_list.reduce(
    (pre, cur) => {
      var _a;
      return pre.plus(
        new import_decimal.default(((_a = cur == null ? void 0 : cur.metadata) == null ? void 0 : _a.product_discount_difference) || 0)
      );
    },
    new import_decimal.default(0)
  );
  return product_discount_difference.toNumber();
};
var formatValues = (params) => {
  var _a, _b;
  const { state, otherValue, platform, excludeDisabled, extra } = params;
  let values = {
    type: "event_booking_v2",
    platform: platform === "pc" ? "PC" : "H5",
    sales_channel: "my_pisel",
    order_sales_channel: "online_store",
    bookings: [],
    shop_note: "",
    schedule_date: "",
    ...extra || {},
    ...otherValue || {}
  };
  console.log("values", values);
  let data = [];
  if (excludeDisabled) {
    data = (_b = (_a = state.cartData.cartItems) == null ? void 0 : _a.filter) == null ? void 0 : _b.call(_a, (v) => v.disabled !== true);
  } else {
    data = state.cartData.cartItems;
  }
  const is_deposit = data.find((v) => v.deposit) ? 1 : 0;
  values.is_deposit = is_deposit;
  values.bookings = formatBookings(data);
  return values;
};
var getFormParams = (state) => {
  var _a;
  const product_id = (((_a = state == null ? void 0 : state.cartData) == null ? void 0 : _a.cartItems) || []).map((item) => {
    var _a2, _b;
    return (_b = (_a2 = item._originData) == null ? void 0 : _a2.product) == null ? void 0 : _b.product_id;
  });
  return {
    product_id,
    appointment_type: "event_booking_v2"
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  formatProductBundle,
  formatProductOption,
  formatValues,
  getFormParams
});
