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/pro/skuDetailModal/utils.ts
var utils_exports = {};
__export(utils_exports, {
  checkProductStock: () => checkProductStock,
  checkSkuStockCanSell: () => checkSkuStockCanSell,
  formatSelectedSkuItems: () => formatSelectedSkuItems,
  formatSelectedSkuMessage: () => formatSelectedSkuMessage,
  getLocalDayKey: () => getLocalDayKey,
  getStockAlertStorageKey: () => getStockAlertStorageKey,
  getStockValidationResult: () => getStockValidationResult,
  hasHiddenStockAlertForToday: () => hasHiddenStockAlertForToday,
  setHiddenStockAlertForToday: () => setHiddenStockAlertForToday
});
module.exports = __toCommonJS(utils_exports);
var import_utils = require("@pisell/utils");
var checkSkuStockCanSell = (sku) => {
  let canSell = true;
  let hasStock = true;
  if ((sku == null ? void 0 : sku.is_track) === 1) {
    if (Number((sku == null ? void 0 : sku.stock_quantity) || 0) <= 0) {
      canSell = false;
      hasStock = false;
    }
  } else {
    canSell = true;
    hasStock = true;
  }
  return { canSell, hasStock };
};
var checkProductStock = (params) => {
  var _a, _b;
  const value = params == null ? void 0 : params.value;
  const data = params == null ? void 0 : params.data;
  if (!data) return true;
  if (value == null ? void 0 : value.product_variant_id) {
    const variantItem = (_a = data == null ? void 0 : data.variant) == null ? void 0 : _a.find(
      (item) => Number(item == null ? void 0 : item.id) === Number(value.product_variant_id)
    );
    const variantCanSell = checkSkuStockCanSell(variantItem);
    if (!variantCanSell.hasStock) {
      return false;
    }
  }
  if (((_b = value == null ? void 0 : value.bundle) == null ? void 0 : _b.length) > 0) {
    const bundleCanSell = value == null ? void 0 : value.bundle.every((item) => {
      const bundleStock = checkSkuStockCanSell(item);
      return bundleStock.hasStock;
    });
    if (!bundleCanSell) {
      return false;
    }
  }
  if (value == null ? void 0 : value.product_variant_id) {
    return true;
  }
  const productCanSell = checkSkuStockCanSell(data);
  if (!productCanSell.hasStock) {
    return false;
  }
  return true;
};
var formatSelectedSkuMessage = (skuValue, currencySymbol = "") => {
  if (!skuValue) return "";
  const lines = [];
  const optionIndent = "  ";
  const optionList = Array.isArray(skuValue == null ? void 0 : skuValue.option) ? skuValue.option : Array.isArray(skuValue == null ? void 0 : skuValue.options) ? skuValue.options : [];
  if (skuValue == null ? void 0 : skuValue.product_option_string) {
    lines.push(String(skuValue.product_option_string));
  }
  optionList.forEach((item) => {
    if (!(item == null ? void 0 : item.name)) return;
    const quantity = Number((item == null ? void 0 : item.num) || 1);
    const hasPrice = (item == null ? void 0 : item.price) !== void 0 && (item == null ? void 0 : item.price) !== null && (item == null ? void 0 : item.price) !== "";
    const priceText = hasPrice ? `  ${(0, import_utils.formatAmount)(item == null ? void 0 : item.price, 2, currencySymbol)}` : "";
    lines.push(`${item.name}${priceText} x ${quantity}`);
  });
  if (Array.isArray(skuValue == null ? void 0 : skuValue.bundle) && skuValue.bundle.length > 0) {
    skuValue.bundle.forEach((item) => {
      if (!(item == null ? void 0 : item.title)) return;
      const quantity = Number((item == null ? void 0 : item.num) || 1);
      const hasPrice = (item == null ? void 0 : item.price) !== void 0 && (item == null ? void 0 : item.price) !== null && (item == null ? void 0 : item.price) !== "";
      const priceText = hasPrice ? `  ${(0, import_utils.formatAmount)(item == null ? void 0 : item.price, 2, currencySymbol)}` : "";
      lines.push(`${item.title}${priceText} x ${quantity}`);
      const bundleOptions = Array.isArray(item == null ? void 0 : item.options) ? item.options : Array.isArray(item == null ? void 0 : item.option) ? item.option : [];
      bundleOptions.forEach((opt) => {
        if (!(opt == null ? void 0 : opt.name)) return;
        const optionQuantity = Number((opt == null ? void 0 : opt.num) || 1);
        const optionHasPrice = (opt == null ? void 0 : opt.price) !== void 0 && (opt == null ? void 0 : opt.price) !== null && (opt == null ? void 0 : opt.price) !== "";
        const optionPriceText = optionHasPrice ? `  ${(0, import_utils.formatAmount)(opt == null ? void 0 : opt.price, 2, currencySymbol)}` : "";
        lines.push(
          `${optionIndent}${opt.name}${optionPriceText} x ${optionQuantity}`
        );
      });
    });
  }
  return lines.join("\n");
};
var formatSelectedSkuItems = (skuValue, currencySymbol = "") => {
  if (!skuValue) return [];
  const items = [];
  const optionList = Array.isArray(skuValue == null ? void 0 : skuValue.option) ? skuValue.option : Array.isArray(skuValue == null ? void 0 : skuValue.options) ? skuValue.options : [];
  if (skuValue == null ? void 0 : skuValue.product_option_string) {
    items.push({
      key: `variant-${skuValue.product_option_string}`,
      type: "variant",
      label: String(skuValue.product_option_string)
    });
  }
  optionList.forEach((item, index) => {
    if (!(item == null ? void 0 : item.name)) return;
    const quantity = Number((item == null ? void 0 : item.num) || 1);
    const hasPrice = (item == null ? void 0 : item.price) !== void 0 && (item == null ? void 0 : item.price) !== null && (item == null ? void 0 : item.price) !== "";
    items.push({
      key: `option-${(item == null ? void 0 : item.id) ?? index}-${item == null ? void 0 : item.name}`,
      type: "option",
      label: item.name,
      meta: `${hasPrice ? `${(0, import_utils.formatAmount)(item == null ? void 0 : item.price, 2, currencySymbol)} ` : ""}x ${quantity}`
    });
  });
  if (Array.isArray(skuValue == null ? void 0 : skuValue.bundle) && skuValue.bundle.length > 0) {
    skuValue.bundle.forEach((item, index) => {
      if (!(item == null ? void 0 : item.title)) return;
      const quantity = Number((item == null ? void 0 : item.num) || 1);
      const hasPrice = (item == null ? void 0 : item.price) !== void 0 && (item == null ? void 0 : item.price) !== null && (item == null ? void 0 : item.price) !== "";
      items.push({
        key: `bundle-${(item == null ? void 0 : item.id) ?? index}-${item == null ? void 0 : item.title}`,
        type: "bundle",
        label: item.title,
        meta: `${hasPrice ? `${(0, import_utils.formatAmount)(item == null ? void 0 : item.price, 2, currencySymbol)} ` : ""}x ${quantity}`,
        children: []
      });
      const bundleDisplayItem = items[items.length - 1];
      const bundleOptions = Array.isArray(item == null ? void 0 : item.options) ? item.options : Array.isArray(item == null ? void 0 : item.option) ? item.option : [];
      bundleOptions.forEach((opt, optionIndex) => {
        var _a;
        if (!(opt == null ? void 0 : opt.name)) return;
        const optionQuantity = Number((opt == null ? void 0 : opt.num) || 1);
        const optionHasPrice = (opt == null ? void 0 : opt.price) !== void 0 && (opt == null ? void 0 : opt.price) !== null && (opt == null ? void 0 : opt.price) !== "";
        (_a = bundleDisplayItem.children) == null ? void 0 : _a.push({
          key: `bundle-option-${(item == null ? void 0 : item.id) ?? index}-${(opt == null ? void 0 : opt.id) ?? optionIndex}-${opt == null ? void 0 : opt.name}`,
          type: "bundleOption",
          label: opt.name,
          meta: `${optionHasPrice ? `${(0, import_utils.formatAmount)(opt == null ? void 0 : opt.price, 2, currencySymbol)} ` : ""}x ${optionQuantity}`
        });
      });
    });
  }
  return items;
};
var getStockValidationResult = (params) => {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  const stockValidation = Number(
    ((_b = (_a = params == null ? void 0 : params.bookingConfig) == null ? void 0 : _a.stock_setting) == null ? void 0 : _b.stock_validation) ?? 0
  );
  const allowAddToCartWhenOutStock = Number(
    ((_d = (_c = params == null ? void 0 : params.bookingConfig) == null ? void 0 : _c.stock_setting) == null ? void 0 : _d.allow_add_to_cart_when_out_stock) ?? 0
  );
  const enableDontShowAgainToday = Number(
    ((_f = (_e = params == null ? void 0 : params.bookingConfig) == null ? void 0 : _e.stock_setting) == null ? void 0 : _f.do_not_show_again_today) ?? 0
  );
  const productId = Number(((_g = params == null ? void 0 : params.data) == null ? void 0 : _g.product_id) || ((_h = params == null ? void 0 : params.data) == null ? void 0 : _h.id) || 0);
  if (stockValidation !== 1) {
    return {
      stockValidation,
      allowAddToCartWhenOutStock,
      enableDontShowAgainToday,
      productId,
      hasStock: true,
      shouldBlock: false,
      shouldShowAlert: false
    };
  }
  const hasStock = checkProductStock({
    value: params == null ? void 0 : params.value,
    data: params == null ? void 0 : params.data
  });
  if (hasStock) {
    return {
      stockValidation,
      allowAddToCartWhenOutStock,
      enableDontShowAgainToday,
      productId,
      hasStock,
      shouldBlock: false,
      shouldShowAlert: false
    };
  }
  const shouldSkipAlert = allowAddToCartWhenOutStock === 1 && enableDontShowAgainToday === 1 && hasHiddenStockAlertForToday({ productId });
  return {
    stockValidation,
    allowAddToCartWhenOutStock,
    enableDontShowAgainToday,
    productId,
    hasStock,
    shouldBlock: allowAddToCartWhenOutStock !== 1,
    shouldShowAlert: allowAddToCartWhenOutStock === 1 && !shouldSkipAlert
  };
};
function getLocalDayKey() {
  const now = /* @__PURE__ */ new Date();
  const yyyy = String(now.getFullYear());
  const mm = String(now.getMonth() + 1).padStart(2, "0");
  const dd = String(now.getDate()).padStart(2, "0");
  return `${yyyy}-${mm}-${dd}`;
}
function getStockAlertStorageKey(params) {
  return `pisell2_stock_alert_hidden_${params.productId}`;
}
function hasHiddenStockAlertForToday(params) {
  try {
    if (!params.productId) return false;
    if (typeof window === "undefined") return false;
    const key = getStockAlertStorageKey(params);
    const value = window.localStorage.getItem(key);
    return value === getLocalDayKey();
  } catch (error) {
    return false;
  }
}
function setHiddenStockAlertForToday(params) {
  try {
    if (!params.productId) return;
    if (typeof window === "undefined") return;
    const key = getStockAlertStorageKey(params);
    window.localStorage.setItem(key, getLocalDayKey());
  } catch (error) {
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  checkProductStock,
  checkSkuStockCanSell,
  formatSelectedSkuItems,
  formatSelectedSkuMessage,
  getLocalDayKey,
  getStockAlertStorageKey,
  getStockValidationResult,
  hasHiddenStockAlertForToday,
  setHiddenStockAlertForToday
});
