"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Price", {
    enumerable: true,
    get: function() {
        return Price;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _define_property = require("@swc/helpers/_/_define_property");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _components = require("@tarojs/components");
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _typings = require("../../utils/typings");
var _index = require("../configprovider/index");
var _types = require("../../types");
var _platform = require("../../utils/taro/platform");
var _shouldrenderpriceraw = require("../../utils/should-render-price-raw");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    color: 'primary',
    price: 0,
    symbol: '&yen;',
    digits: 2,
    thousands: false,
    position: 'before',
    size: 'normal',
    line: false
});
var Price = function Price(props) {
    var _$_object_spread = (0, _object_spread._)({}, defaultProps, props), color = _$_object_spread.color, originalPrice = _$_object_spread.price, symbol = _$_object_spread.symbol, digits = _$_object_spread.digits, thousands = _$_object_spread.thousands, position = _$_object_spread.position, size = _$_object_spread.size, line = _$_object_spread.line, className = _$_object_spread.className, style = _$_object_spread.style;
    var classPrefix = 'nut-price';
    var rtl = (0, _index.useRtl)();
    var isRenderPriceRaw = (0, _react.useMemo)(function() {
        return typeof originalPrice === 'string' && (0, _shouldrenderpriceraw.shouldRenderPriceAsRaw)(originalPrice);
    }, [
        originalPrice
    ]);
    var price = (0, _react.useMemo)(function() {
        if (isRenderPriceRaw) return '';
        return originalPrice.toString().replace(/[^\d.]/g, '');
    }, [
        originalPrice,
        isRenderPriceRaw
    ]);
    var isCustomPriceColor = (0, _react.useMemo)(function() {
        var specificPriceColor = Object.values(_types.PriceColorEnum);
        return !specificPriceColor.includes(color);
    }, [
        color
    ]);
    var priceColorStyle = (0, _react.useMemo)(function() {
        return isCustomPriceColor ? {
            color: color
        } : {};
    }, [
        isCustomPriceColor,
        color
    ]);
    var replaceSpecialChar = function replaceSpecialChar(url) {
        url = url.replace(/&quot;/g, '"');
        url = url.replace(/&amp;/g, '&');
        url = url.replace(/&lt;/g, '<');
        url = url.replace(/&gt;/g, '>');
        url = url.replace(/&nbsp;/g, ' ');
        url = url.replace(/&yen;/g, '¥');
        return url;
    };
    var checkPoint = function checkPoint(price) {
        return String(price).indexOf('.') > 0;
    };
    var formatThousands = function formatThousands(num) {
        if (Number(num) === 0) {
            num = 0;
        }
        if (checkPoint(num)) {
            num = num.toString();
            num = typeof num.split('.') === 'string' ? num.split('.') : num.split('.')[0];
        }
        if (thousands) {
            return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
        }
        return num;
    };
    var formatDecimal = function formatDecimal(decimalNum) {
        if (Number(decimalNum) === 0) {
            decimalNum = 0;
        }
        if (checkPoint(decimalNum)) {
            decimalNum = digits ? Number(decimalNum).toFixed(digits) : "".concat(decimalNum);
            decimalNum = typeof decimalNum.split('.') === 'string' ? 0 : decimalNum.split('.')[1] || '';
        } else {
            decimalNum = '';
        }
        if (digits) {
            var result = "0.".concat(decimalNum);
            var resultFixed = Number(result).toFixed(digits);
            return String(resultFixed).substring(2, resultFixed.length);
        }
        return decimalNum;
    };
    var renderSymbol = function renderSymbol() {
        var _obj;
        return /*#__PURE__*/ _react.default.createElement(_components.Text, {
            className: (0, _classnames.default)([
                "".concat(classPrefix, "-symbol"),
                "".concat(classPrefix, "-symbol-").concat(size),
                (_obj = {}, (0, _define_property._)(_obj, "".concat(classPrefix, "-line"), line), (0, _define_property._)(_obj, "".concat(classPrefix, "-rtl"), rtl), _obj)
            ]),
            style: priceColorStyle
        }, symbol ? replaceSpecialChar(symbol) : '');
    };
    var renderInner = function renderInner() {
        return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, symbol && position === 'before' ? renderSymbol() : null, /*#__PURE__*/ _react.default.createElement(_components.Text, {
            className: "".concat(classPrefix, "-integer ").concat(classPrefix, "-integer-").concat(size, " ").concat(line ? "".concat(classPrefix, "-line") : ''),
            style: priceColorStyle
        }, formatThousands(price)), digits !== 0 ? /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, checkPoint(price) || digits ? /*#__PURE__*/ _react.default.createElement(_components.Text, {
            className: "".concat(classPrefix, "-decimal ").concat(classPrefix, "-decimal-").concat(size, " ").concat(line ? "".concat(classPrefix, "-line") : ''),
            style: priceColorStyle
        }, ".") : null, /*#__PURE__*/ _react.default.createElement(_components.Text, {
            className: "".concat(classPrefix, "-decimal ").concat(classPrefix, "-decimal-").concat(size, " ").concat(line ? "".concat(classPrefix, "-line") : ''),
            style: priceColorStyle
        }, formatDecimal(price))) : null, symbol && position === 'after' ? renderSymbol() : null);
    };
    var renderRawContent = function renderRawContent() {
        return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, originalPrice);
    };
    return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, (0, _platform.harmony)() || process.env.TARO_ENV === 'dynamic' ? /*#__PURE__*/ _react.default.createElement(_components.Text, {
        className: "".concat(classPrefix, " ").concat(classPrefix, "-").concat(color, " ").concat(className),
        style: style
    }, isRenderPriceRaw ? renderRawContent() : renderInner()) : /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "".concat(classPrefix, " ").concat(classPrefix, "-").concat(color, " ").concat(className),
        style: style
    }, isRenderPriceRaw ? renderRawContent() : renderInner()));
};
Price.displayName = 'NutPrice';
