var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/booking/components/footer/amount.ts
var amount_exports = {};
__export(amount_exports, {
  getRelationDetailsTotal: () => getRelationDetailsTotal,
  getSumTotal: () => getSumTotal,
  is_price_include_tax: () => is_price_include_tax
});
module.exports = __toCommonJS(amount_exports);
var import_utils = require("../../utils");
var import_utils2 = require("@pisell/utils");
var import_utils3 = require("../../info/service2/utils");
var getRelationDetailsTotal = (relation_details) => {
  let total = 0;
  for (let item of relation_details) {
    const number = item.product_quantity || item.num;
    const price = item.payment_price || item.price;
    total += Number(price) * Number(number);
  }
  return total;
};
var getProductAmount = (state, isEdit) => {
  var _a, _b;
  let price = 0;
  let duration = 0;
  const isDay = (0, import_utils3.isDayBooking)(state);
  if (isEdit) {
    if ((state == null ? void 0 : state.service) || (state == null ? void 0 : state.addons)) {
      for (let item of state == null ? void 0 : state.service.value) {
        if (isDay || (0, import_utils3.isNormalProductByDurationSchedule)(item)) {
          const quantity = item._extend.quantity || 1;
          price += Number(item._extend.total) * Number(quantity);
          const _duration = (0, import_utils3.isDayBookingProduct)(item, state) ? item._extend.duration : 0;
          duration += _duration;
        } else {
          const quantity = item._extend.quantity || 1;
          price += Number(item._extend.total) * Number(quantity);
          price += getRelationDetailsTotal(item._extend.relation_details || []);
          duration += import_utils.flexibleObj.getValue(item._extend.duration);
        }
      }
      if (!isDay) {
        for (let item of state.addons.value) {
          price += Number(item._extend.total) * item._extend.quantity;
        }
      }
    }
  } else {
    price = Number((_a = state.bookingDetail) == null ? void 0 : _a.product_expect_amount);
    let services = ((_b = state == null ? void 0 : state.service) == null ? void 0 : _b.value) || [];
    duration = services.reduce((pre, cur) => {
      var _a2;
      let current = 0;
      if (isDay) {
        const _duration = (0, import_utils3.isDayBookingProduct)(cur, state) ? cur._extend.duration : 0;
        current = _duration || 0;
      } else {
        current = import_utils.flexibleObj.getValue(((_a2 = cur._extend) == null ? void 0 : _a2.duration) || 0);
      }
      return pre + current;
    }, 0);
  }
  return {
    price: Number(price),
    duration: (0, import_utils.getDiscountTime)(duration, isDay)
  };
};
var getProductItemTax = (item, bookingDetail) => {
  let price = 0;
  const productDiscountPrice = Number(item.total);
  const shopTax = productDiscountPrice * Number(bookingDetail.tax_rate) * Number(item.is_charge_tax);
  const shopTaxRate = 1 + bookingDetail.tax_rate * bookingDetail.is_price_include_tax;
  const res = shopTax / shopTaxRate * item.quantity;
  price += res;
  return price;
};
var getTotal = (item) => {
  return item._extend.payment_price !== void 0 ? item._extend.payment_price : item._extend.total;
};
var getTax = (state, isEdit) => {
  var _a;
  let price = 0;
  if (!state.bookingId || !state.bookingDetail) {
    return 0;
  }
  if (!isEdit) {
    return Number((_a = state.bookingDetail) == null ? void 0 : _a.tax_fee);
  }
  if ((state == null ? void 0 : state.service) || (state == null ? void 0 : state.addons)) {
    for (let item of state == null ? void 0 : state.service.value) {
      price += getProductItemTax(
        {
          ...item,
          total: getTotal(item),
          price: item._extend.price,
          quantity: item._extend.quantity
        },
        state.bookingDetail
      );
    }
    for (let item of state.addons.value) {
      price += getProductItemTax({
        ...item,
        total: getTotal(item),
        price: item._extend.price,
        quantity: item._extend.quantity
      }, state.bookingDetail);
    }
  }
  return price;
};
var getPaySurchargeFee = (state) => {
  const { bookingDetail, bookingId } = state;
  if (!bookingDetail || !bookingId) return 0;
  return Number(bookingDetail.pay_processing_fee) + Number(bookingDetail.gateway_fee);
};
var getOtherAmount = (state) => {
  const { bookingDetail, bookingId } = state;
  if (!bookingDetail || !bookingId) return 0;
  return Number(bookingDetail.surcharge_fee);
};
var getPaidAmount = (state) => {
  var _a;
  const { bookingDetail, bookingId } = state;
  if (!bookingDetail || !bookingId) return 0;
  return Number((_a = state.bookingDetail) == null ? void 0 : _a.paid_amount) || 0;
};
var getOrderPaidAmount = (state) => {
  var _a;
  const { bookingDetail, bookingId } = state;
  if (!bookingDetail || !bookingId) return 0;
  return Number((_a = state.bookingDetail) == null ? void 0 : _a.order_paid_amount) || 0;
};
var is_price_include_tax = (state) => {
  var _a;
  return (_a = state.bookingDetail) == null ? void 0 : _a.is_price_include_tax;
};
var getExpectAmount = ({ price, tax, otherAmount, shopDiscount, state, isEdit }) => {
  var _a;
  if (!isEdit) {
    return Number((_a = state.bookingDetail) == null ? void 0 : _a.expect_amount) || 0;
  }
  let _tax = tax;
  if (state.bookingDetail && is_price_include_tax(state)) {
    _tax = 0;
  }
  return price + _tax + otherAmount - shopDiscount;
};
var getTotalRefundAmount = (state) => {
  var _a;
  return Number((_a = state.bookingDetail) == null ? void 0 : _a.total_refund_amount) || 0;
};
var getShopDiscount = (state) => {
  var _a;
  return Number((_a = state.bookingDetail) == null ? void 0 : _a.shop_discount) || 0;
};
var getRoundingAmount = (state) => {
  var _a;
  return Number((_a = state == null ? void 0 : state.bookingDetail) == null ? void 0 : _a.rounding_amount) || 0;
};
var getSumTotalAmount = ({ expectAmount, roundingAmount }) => {
  return expectAmount + roundingAmount;
};
var getSumTotal = (state, isEdit) => {
  const { price, duration } = getProductAmount(state, isEdit);
  const roundingAmount = getRoundingAmount(state);
  const tax = getTax(state, isEdit);
  const paySurchargeFee = getPaySurchargeFee(state);
  const otherAmount = getOtherAmount(state);
  const paidAmount = getPaidAmount(state);
  const orderPaidAmount = getOrderPaidAmount(state);
  const totalRefundAmount = getTotalRefundAmount(state);
  const shopDiscount = getShopDiscount(state);
  const expectAmount = getExpectAmount({ price, tax, otherAmount, shopDiscount, state, isEdit });
  const sumTotalAmount = getSumTotalAmount({ expectAmount, roundingAmount });
  const notPaidAmount = Number((0, import_utils2.formatAmount)(sumTotalAmount, 2, "")) - orderPaidAmount;
  const actualPaidAmount = paidAmount;
  const dueAmount = notPaidAmount < 0 ? Number((0, import_utils2.formatAmount)(notPaidAmount + totalRefundAmount, 2, "")) : 0;
  return {
    productAmount: price,
    duration,
    tax,
    shopDiscount,
    // taxText,
    otherAmount,
    sumTotalAmount,
    paidAmount,
    notPaidAmount,
    paySurchargeFee,
    expectAmount,
    actualPaidAmount,
    orderPaidAmount,
    totalRefundAmount,
    dueAmount,
    roundingAmount
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getRelationDetailsTotal,
  getSumTotal,
  is_price_include_tax
});
