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/pro/skuDetailModal/components/weighing/utils.ts
var utils_exports = {};
__export(utils_exports, {
  CUSTOM_TARE_STORAGE_KEY: () => CUSTOM_TARE_STORAGE_KEY,
  DEFAULT_TARE_STORAGE_KEY: () => DEFAULT_TARE_STORAGE_KEY,
  PRODUCT_TARE_MAP_KEY: () => PRODUCT_TARE_MAP_KEY,
  UNIT_LABEL: () => UNIT_LABEL,
  formatMoney: () => formatMoney,
  formatWeight: () => formatWeight,
  getProductTareKey: () => getProductTareKey,
  getRestoredTareId: () => getRestoredTareId,
  getScaleTag: () => getScaleTag,
  getWeighedUnitPrice: () => getWeighedUnitPrice,
  isWeighingProduct: () => isWeighingProduct,
  readCustomTares: () => readCustomTares,
  readDefaultTareId: () => readDefaultTareId,
  readProductTareMap: () => readProductTareMap,
  resolveWeighingSource: () => resolveWeighingSource,
  sanitizeManualWeight: () => sanitizeManualWeight,
  saveProductTarePref: () => saveProductTarePref
});
module.exports = __toCommonJS(utils_exports);
var import_decimal = __toESM(require("decimal.js"));
var CUSTOM_TARE_STORAGE_KEY = "pisell-weighing-custom-tares";
var DEFAULT_TARE_STORAGE_KEY = "pisell-weighing-default-tare";
var formatWeight = (value) => Number(value || 0).toFixed(3);
var formatMoney = (value, symbol = "$") => `${symbol}${Number(value || 0).toFixed(2)}`;
var getScaleTag = (status, error) => {
  if (error) return "Error";
  if (status === "stable") return "Stable";
  if (status === "unstable") return "Unstable";
  if (status === "overload") return "Overload";
  return "Listening";
};
var sanitizeManualWeight = (value) => {
  const nextValue = value.replace(/[^0-9.]/g, "");
  const [integerPart, ...decimalParts] = nextValue.split(".");
  if (!decimalParts.length) {
    return integerPart;
  }
  return `${integerPart}.${decimalParts.join("")}`;
};
var getRestoredTareId = (tare) => `restored-${tare.label.replace(/\s+/g, "-")}-${tare.value}`;
var readCustomTares = () => {
  if (typeof window === "undefined") {
    return [];
  }
  try {
    const rawValue = window.localStorage.getItem(CUSTOM_TARE_STORAGE_KEY);
    if (!rawValue) {
      return [];
    }
    const parsedValue = JSON.parse(rawValue);
    if (!Array.isArray(parsedValue)) {
      return [];
    }
    return parsedValue.filter((item) => {
      const hasBasicShape = item && typeof item.id === "string" && typeof item.label === "string" && typeof item.value === "number";
      const hasValidUnit = typeof (item == null ? void 0 : item.unit) === "undefined" || item.unit === "KILOGRAMS" || item.unit === "GRAMS" || item.unit === "POUNDS" || item.unit === "OUNCES";
      return hasBasicShape && hasValidUnit;
    });
  } catch {
    return [];
  }
};
var readDefaultTareId = () => {
  if (typeof window === "undefined") {
    return void 0;
  }
  try {
    const rawValue = window.localStorage.getItem(DEFAULT_TARE_STORAGE_KEY);
    return rawValue || void 0;
  } catch {
    return void 0;
  }
};
var PRODUCT_TARE_MAP_KEY = "pisell-weighing-product-tare-map";
var getProductTareKey = (productId, variantId) => `${productId}:${variantId ?? ""}`;
var readProductTareMap = () => {
  if (typeof window === "undefined") {
    return {};
  }
  try {
    const raw = window.localStorage.getItem(PRODUCT_TARE_MAP_KEY);
    if (!raw) return {};
    const parsed = JSON.parse(raw);
    return typeof parsed === "object" && parsed !== null ? parsed : {};
  } catch {
    return {};
  }
};
var saveProductTarePref = (productKey, tareId) => {
  if (typeof window === "undefined") {
    return;
  }
  try {
    const map = readProductTareMap();
    map[productKey] = tareId;
    window.localStorage.setItem(PRODUCT_TARE_MAP_KEY, JSON.stringify(map));
  } catch {
  }
};
var UNIT_LABEL = {
  KILOGRAMS: "kg",
  GRAMS: "g",
  POUNDS: "lb",
  OUNCES: "oz"
};
var isWeighingProduct = (productData) => {
  return Number((productData == null ? void 0 : productData.open_sold_weight) || 0) === 1;
};
var resolveWeighingSource = (productData, variantId) => {
  const pd = productData;
  const variants = (pd == null ? void 0 : pd.variant) || [];
  const activeVariant = variantId ? variants.find((v) => Number(v.id) === Number(variantId)) ?? null : null;
  return activeVariant ?? pd;
};
var getWeighedUnitPrice = (params) => {
  const { weighedTotalAmount, productData, skuValue } = params;
  if (!productData) {
    return 0;
  }
  let price = new import_decimal.default(weighedTotalAmount ?? 0);
  if (Array.isArray(skuValue == null ? void 0 : skuValue.bundle) && skuValue.bundle.length > 0) {
    price = skuValue.bundle.reduce(
      (accumulator, currentValue) => {
        const currentPrice = new import_decimal.default(
          (currentValue == null ? void 0 : currentValue.original_price) ?? (currentValue == null ? void 0 : currentValue.price) ?? 0
        );
        const currentNum = new import_decimal.default((currentValue == null ? void 0 : currentValue.num) || 0);
        return accumulator.add(currentPrice.mul(currentNum));
      },
      price
    );
  }
  if (Array.isArray(skuValue == null ? void 0 : skuValue.option) && skuValue.option.length > 0) {
    price = skuValue.option.reduce(
      (accumulator, currentValue) => {
        const currentPrice = new import_decimal.default(
          (currentValue == null ? void 0 : currentValue.original_price) ?? (currentValue == null ? void 0 : currentValue.price) ?? 0
        );
        const currentNum = new import_decimal.default((currentValue == null ? void 0 : currentValue.num) || 0);
        return accumulator.add(currentPrice.mul(currentNum));
      },
      price
    );
  }
  return price.toNumber();
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CUSTOM_TARE_STORAGE_KEY,
  DEFAULT_TARE_STORAGE_KEY,
  PRODUCT_TARE_MAP_KEY,
  UNIT_LABEL,
  formatMoney,
  formatWeight,
  getProductTareKey,
  getRestoredTareId,
  getScaleTag,
  getWeighedUnitPrice,
  isWeighingProduct,
  readCustomTares,
  readDefaultTareId,
  readProductTareMap,
  resolveWeighingSource,
  sanitizeManualWeight,
  saveProductTarePref
});
