(function(root, factory) {
  var api = factory();
  if (typeof module === 'object' && module.exports) module.exports = api;
  if (root) root.PandaOzonPrice = api;
})(typeof self !== 'undefined' ? self : this, function() {
  'use strict';

  function numberFrom(value) {
    if (value == null || value === '') return null;
    var match = String(value).replace(/\s/g, '').replace(',', '.').match(/\d+(?:\.\d+)?/);
    if (!match) return null;
    var number = Number(match[0]);
    return Number.isFinite(number) && number > 0 ? number : null;
  }

  function currencyFrom(value) {
    var text = String(value == null ? '' : value).toUpperCase();
    if (/¥|CNY|RMB|人民币|元/.test(text)) return 'CNY';
    if (/₽|RUB|RUR|РУБ/.test(text)) return 'RUB';
    if (/₸|KZT|ТЕНГЕ/.test(text)) return 'KZT';
    if (/BYN|BELARUS|БЕЛ/.test(text)) return 'BYN';
    if (/\$|USD/.test(text)) return 'USD';
    if (/€|EUR/.test(text)) return 'EUR';
    return '';
  }

  function resultFrom(value, inheritedCurrency) {
    if (value == null) return null;
    if (typeof value === 'number' || typeof value === 'string') {
      var number = numberFrom(value);
      return number == null ? null : {
        price: number,
        raw: String(value),
        currency: currencyFrom(value) || inheritedCurrency || ''
      };
    }
    if (typeof value !== 'object') return null;
    var currency = currencyFrom(value.currency || value.currencyCode || value.currency_code) || inheritedCurrency || '';
    var keys = ['price', 'value', 'amount', 'current', 'final'];
    for (var i = 0; i < keys.length; i++) {
      if (!Object.prototype.hasOwnProperty.call(value, keys[i])) continue;
      var parsed = resultFrom(value[keys[i]], currency);
      if (parsed) return parsed;
    }
    return null;
  }

  function parseModel(model) {
    if (!model || typeof model !== 'object') return null;
    var currency = currencyFrom(model.currency || model.currencyCode || model.currency_code);
    var keys = ['cardPrice', 'price', 'finalPrice', 'salePrice', 'currentPrice'];
    for (var i = 0; i < keys.length; i++) {
      if (!Object.prototype.hasOwnProperty.call(model, keys[i])) continue;
      var parsed = resultFrom(model[keys[i]], currency);
      if (parsed) return parsed;
    }
    if (model.priceInfo && typeof model.priceInfo === 'object') return parseModel(model.priceInfo);
    return null;
  }

  function parseState(value) {
    if (!value) return null;
    var model = value;
    if (typeof value === 'string') {
      try { model = JSON.parse(value); } catch (ignore) { return null; }
    }
    return parseModel(model);
  }

  function fromWidgetStates(states) {
    if (!states || typeof states !== 'object') return null;
    var keys = Object.keys(states).filter(function(key) { return /(?:web|sku)price/i.test(key); });
    for (var i = 0; i < keys.length; i++) {
      var parsed = parseState(states[keys[i]]);
      if (parsed) return parsed;
    }
    return null;
  }

  function fromDataStates(values) {
    for (var i = 0; i < (values || []).length; i++) {
      var parsed = parseState(values[i]);
      if (parsed) return parsed;
    }
    return null;
  }

  function fromPriceText(text) {
    var normalized = String(text || '').replace(/\s+/g, ' ').trim();
    if (!normalized) return null;
    var match = normalized.match(/(\d[\d\s]*(?:[.,]\d+)?)\s*(₽|₸|¥|€|\$|BYN|RUB|KZT|CNY|руб(?:\.|ля|лей)?|元)/i);
    if (!match) return null;
    return resultFrom(match[1] + ' ' + match[2], '');
  }

  return {
    numberFrom: numberFrom,
    currencyFrom: currencyFrom,
    parseModel: parseModel,
    parseState: parseState,
    fromWidgetStates: fromWidgetStates,
    fromDataStates: fromDataStates,
    fromPriceText: fromPriceText
  };
});
