import { jsx } from "react/jsx-runtime";
import { useMemo } from "react";
import { connect, isNum, mapReadPretty } from "@tachybase/schema";
import { InputNumber } from "antd";
import { round } from "mathjs";
import { ReadPretty } from "../input-number/ReadPretty.mjs";
const isNumberLike = (index)=>isNum(index) || /^-?\d+(\.\d+)?$/.test(index);
const toValue = (value, callback)=>{
    if (isNumberLike(value)) return round(callback(value), 9);
    return null;
};
const Percent = connect((props)=>{
    const { value, onChange } = props;
    const v = useMemo(()=>toValue(value, (v)=>100 * v), [
        value
    ]);
    return /*#__PURE__*/ jsx(InputNumber, {
        ...props,
        addonAfter: "%",
        value: v,
        onChange: (v)=>{
            if (onChange) onChange(toValue(v, (v)=>v / 100));
        }
    });
}, mapReadPretty((props)=>{
    const value = useMemo(()=>toValue(props.value, (v)=>100 * v), [
        props.value
    ]);
    return /*#__PURE__*/ jsx(ReadPretty, {
        ...props,
        value: value
    });
}));
export { Percent };
