"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InternalAdaptablePercentageEditor = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const icons_1 = require("../../../components/icons");
const useProperty_1 = tslib_1.__importDefault(require("../../../components/utils/useProperty"));
const NumberInput_1 = require("../../../components/Input/NumberInput");
const fillStyle = {
    position: 'absolute',
    left: 0,
    top: 0,
    right: 0,
    bottom: 0,
};
const hostStyle = {
    ...fillStyle,
    boxSizing: 'border-box',
    display: 'flex',
    flexDirection: 'row',
    alignItems: 'center',
};
const inputStyle = {
    position: 'relative',
    background: 'transparent',
    boxSizing: 'border-box',
    color: 'currentcolor',
    minWidth: 0,
    outline: 'none',
    textAlign: 'right',
    width: '100%',
    border: 'none',
    boxShadow: 'none',
    paddingRight: '3ch',
};
exports.InternalAdaptablePercentageEditor = React.forwardRef(function InternalAdaptablePercentageEditorFn(props, ref) {
    const inputRef = React.useRef(null);
    const focus = () => {
        inputRef.current?.focus();
    };
    const [value, setValue] = (0, useProperty_1.default)(props, 'value', props.defaultValue, {
        onChange: (value) => {
            if (value !== '' && value == Number(value)) {
                value = Number(value);
            }
            props.onValueChange?.(value);
        },
    });
    React.useImperativeHandle(ref, () => {
        return {
            focus,
            setValue,
        };
    });
    const showClear = props.showClearButton && props.emptyValue != null && value != props.emptyValue;
    return (React.createElement("div", { style: hostStyle, className: "ab-PercentageEditor", onKeyDown: (e) => {
            if (e.key === 'Tab' && showClear && document.activeElement === inputRef.current) {
                e.stopPropagation();
            }
        } },
        React.createElement(NumberInput_1.NumberInput, { type: props.type ?? 'number', className: "ab-PercentageEditor-Input", value: value, noArrows: true, autoFocus: true, onChange: React.useCallback((value) => {
                setValue(value);
            }, []), style: inputStyle, ref: inputRef }),
        showClear ? (React.createElement("div", { className: "twa:inline-block twa:mx-1", onClick: (e) => {
                if (props.emptyValue != null) {
                    setValue(props.emptyValue);
                    e.stopPropagation();
                    focus();
                }
            }, onKeyDown: (e) => {
                if (props.emptyValue != null && e.key === 'Enter') {
                    setValue(props.emptyValue);
                    e.stopPropagation();
                    focus();
                }
            } },
            React.createElement(icons_1.Icon, { size: 15, name: "close", tabIndex: 0 }))) : null,
        React.createElement("div", { style: {
                position: 'absolute',
                right: showClear ? 20 : 0,
                paddingInlineEnd: '0.5ch',
            } }, "%")));
});
