import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { cn } from '../../../utils/cn.js';
import { format_percentage } from '../../../utils/financial_format.js';
import { DataFormFieldLayout } from '../shared/data_form_field_layout.js';
export function PercentageField(props) {
    const { field, mode, value, config, on_change, on_blur } = props;
    const suffix = config.percentage_suffix;
    const decimals = field.decimal_places ?? config.default_decimal_places;
    const num_value = value != null && value !== '' ? Number(value) : null;
    const string_value = num_value != null ? String(num_value) : '';
    const field_style = config.style_resolver.has_style(mode === 'view' ? 'field_view' : 'field')
        ? config.style_resolver.to_style(mode === 'view' ? 'field_view' : 'field')
        : undefined;
    const display_value = num_value != null
        ? format_percentage(num_value, decimals, suffix)
        : '';
    return (_jsx(DataFormFieldLayout, { ...props, children: mode === 'edit' ? (_jsxs("div", { className: "cls_data_form_percentage_input relative flex items-center", children: [_jsx("input", { type: "number", className: cn('cls_data_form_percentage_number', 'w-full text-right pr-6'), style: field_style, value: string_value, placeholder: field.placeholder || '0.00', disabled: field.disabled, step: Math.pow(10, -decimals), min: field.min, max: field.max, onChange: (e) => {
                        const parsed = e.target.value === '' ? '' : parseFloat(e.target.value);
                        on_change(parsed === '' ? '' : isNaN(parsed) ? '' : parsed);
                    }, onBlur: on_blur }), _jsx("span", { className: "absolute right-2 text-gray-500 text-sm pointer-events-none select-none", children: suffix })] })) : (_jsx("div", { className: "cls_data_form_percentage_view text-right", style: field_style, children: display_value || '\u00A0' })) }));
}
//# sourceMappingURL=percentage_field.js.map