(function (root, factory) {
  "use strict";
  const api = factory();
  if (typeof module === "object" && module.exports) module.exports = api;
  root.SDMPriceParser = api;
})(typeof globalThis !== "undefined" ? globalThis : this, function () {
  "use strict";

  const ZERO_DECIMAL_CURRENCIES = new Set(["JPY", "KRW", "VND"]);

  function clean(value) {
    return String(value == null ? "" : value)
      .replace(/[\u00a0\u202f]/g, " ")
      .replace(/\s+/g, " ")
      .trim();
  }

  // Amazon may render the same numeric value as 1.234,56 (EU) or 1,234.56
  // (US). This helper is intentionally money-specific: ratings and review
  // counts continue to use their existing parsers.
  function parseMoney(value, currency = "") {
    const source = clean(value);
    const token = source.match(/[+-]?\d[\d\s.,'’]*/)?.[0]?.trim() || "";
    if (!token) return "";

    const compact = token.replace(/[\s'’]/g, "");
    const dots = [...compact.matchAll(/\./g)].map(match => match.index);
    const commas = [...compact.matchAll(/,/g)].map(match => match.index);
    let normalized = compact;

    if (dots.length && commas.length) {
      const decimal = dots.at(-1) > commas.at(-1) ? "." : ",";
      const grouping = decimal === "." ? "," : ".";
      normalized = compact.replaceAll(grouping, "").replace(decimal, ".");
    } else {
      const separator = dots.length ? "." : commas.length ? "," : "";
      if (separator) {
        const parts = compact.split(separator);
        const fraction = parts.at(-1) || "";
        const normalizedCurrency = clean(currency).toUpperCase();
        const isDecimal = !ZERO_DECIMAL_CURRENCIES.has(normalizedCurrency) && (fraction.length === 1 || fraction.length === 2);
        normalized = isDecimal
          ? `${parts.slice(0, -1).join("")}.${fraction}`
          : parts.join("");
      }
    }

    if (!/^[+-]?\d+(?:\.\d+)?$/.test(normalized)) return "";
    const result = Number(normalized);
    return Number.isFinite(result) ? result : "";
  }

  return { parseMoney };
});
