/**
 * amis v3.1.0
 * Copyright 2018-2023 baidu
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var NumberField = /** @class */ (function (_super) {
    tslib.__extends(NumberField, _super);
    function NumberField() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    NumberField.prototype.render = function () {
        var _a = this.props, placeholder = _a.placeholder, kilobitSeparator = _a.kilobitSeparator, precision = _a.precision, prefix = _a.prefix, affix = _a.affix, suffix = _a.suffix, percent = _a.percent, className = _a.className, style = _a.style, cx = _a.classnames, __ = _a.translate;
        var viewValue = (React__default["default"].createElement("span", { className: "text-muted" }, placeholder));
        var value = amisCore.getPropValue(this.props);
        if (value) {
            // 设置了精度，但是原始数据是字符串，需要转成 float 之后再处理
            if (typeof value === 'string' && precision) {
                value = amisCore.stripNumber(parseFloat(value));
            }
            if (isNaN(value)) {
                viewValue = false;
            }
            else if (percent) {
                // 如果是百分比展示
                value = parseFloat(value) || 0;
                var decimals = typeof percent === 'number' ? percent : 0;
                var whole = value * 100;
                var multiplier = Math.pow(10, decimals);
                value =
                    (Math.round(whole * multiplier) / multiplier).toFixed(decimals) + '%';
                viewValue = React__default["default"].createElement("span", null, value);
            }
            else {
                if (typeof value === 'number' && precision) {
                    value = value.toFixed(precision);
                }
                if (kilobitSeparator) {
                    var parts = String(value).split('.');
                    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
                    value = parts.join('.');
                }
                viewValue = React__default["default"].createElement("span", null, value);
            }
        }
        viewValue = !viewValue ? (React__default["default"].createElement("span", { className: "text-danger" }, __('Number.invalid'))) : (React__default["default"].createElement(React__default["default"].Fragment, null,
            prefix,
            viewValue, affix !== null && affix !== void 0 ? affix : suffix));
        return (React__default["default"].createElement("span", { className: cx('NumberField', className), style: style }, viewValue));
    };
    NumberField.defaultProps = {
        placeholder: '-',
        kilobitSeparator: true
    };
    return NumberField;
}(React__default["default"].Component));
/** @class */ ((function (_super) {
    tslib.__extends(NumberFieldRenderer, _super);
    function NumberFieldRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    NumberFieldRenderer = tslib.__decorate([
        amisCore.Renderer({
            type: 'number'
        })
    ], NumberFieldRenderer);
    return NumberFieldRenderer;
})(NumberField));

exports.NumberField = NumberField;
