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/hooks/useCartProductStockCheck.ts
var useCartProductStockCheck_exports = {};
__export(useCartProductStockCheck_exports, {
  default: () => useCartProductStockCheck_default
});
module.exports = __toCommonJS(useCartProductStockCheck_exports);
var import_react = require("react");
var import_utils = require("../components/appointmentBooking/utils");
var useCartProductStockCheck = () => {
  const filterUnavailableProducts = async (cartProducts, data, callback, appointmentBooking) => {
    if (!(cartProducts == null ? void 0 : cartProducts.length) || !(data == null ? void 0 : data.length)) return;
    const unavailableIds = data.map(
      (item) => item == null ? void 0 : item.product_id
    );
    let newCart = (cartProducts == null ? void 0 : cartProducts.filter(
      (item) => {
        var _a, _b;
        return unavailableIds.includes(((_b = (_a = item == null ? void 0 : item._origin) == null ? void 0 : _a.product) == null ? void 0 : _b.product_id) || (item == null ? void 0 : item.id));
      }
    )) || [];
    if (appointmentBooking) {
      newCart.forEach((item) => {
        appointmentBooking.deleteCart(item == null ? void 0 : item._id);
      });
      newCart = await appointmentBooking.getCart();
    }
    callback && callback(newCart);
    return newCart;
  };
  const handleContinue = ({ cartProducts, data, callback, appointmentBooking }) => {
    filterUnavailableProducts(cartProducts, data, callback, appointmentBooking);
  };
  const handleReturn = async ({
    cartProducts,
    data,
    callback,
    appointmentBooking,
    setTimes,
    isSkipClearData = true
  }) => {
    await filterUnavailableProducts(cartProducts, data, callback, appointmentBooking);
    (0, import_utils.handleGoToStep)({
      appointmentBooking,
      setTimes,
      isSkipClearData
    });
  };
  const checkProductStock = (0, import_react.useCallback)(
    (productStockModalRef, res, cartProducts) => {
      var _a;
      if (!(productStockModalRef == null ? void 0 : productStockModalRef.current)) return false;
      const { unavailable_products } = res || {};
      if (!(unavailable_products == null ? void 0 : unavailable_products.length) || !(cartProducts == null ? void 0 : cartProducts.length)) return false;
      let _isAllItemsSoldOut = false;
      const unavailableIds = unavailable_products.map(
        (item) => item.product_id
      );
      const allUnavailable = cartProducts == null ? void 0 : cartProducts.every(
        (item) => {
          var _a2, _b;
          return unavailableIds.includes(((_b = (_a2 = item == null ? void 0 : item._origin) == null ? void 0 : _a2.product) == null ? void 0 : _b.product_id) || (item == null ? void 0 : item.id));
        }
      );
      _isAllItemsSoldOut = allUnavailable;
      const needStop = (_a = productStockModalRef.current) == null ? void 0 : _a.open({
        unavailableProducts: unavailable_products,
        isAllItemsSoldOut: _isAllItemsSoldOut
      });
      return needStop;
    },
    []
  );
  return {
    handleContinue,
    handleReturn,
    checkProductStock
  };
};
var useCartProductStockCheck_default = useCartProductStockCheck;
