"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IntegerEditor = void 0;
const integerValidator_js_1 = require("../editorValidators/integerValidator.js");
const inputEditor_js_1 = require("./inputEditor.js");
const utilities_js_1 = require("../services/utilities.js");
class IntegerEditor extends inputEditor_js_1.InputEditor {
    constructor(args) {
        super(args, 'number');
        this.args = args;
    }
    loadValue(item) {
        const fieldName = this.columnDef?.field;
        if (item && fieldName !== undefined && this._input) {
            // is the field a complex object, "address.streetNumber"
            const isComplexObject = fieldName?.indexOf('.') > 0;
            const value = isComplexObject ? (0, utilities_js_1.getDescendantProperty)(item, fieldName) : item[fieldName];
            this._originalValue = isNaN(value) || value === null || value === undefined ? value : `${value}`;
            this._input.value = `${this._originalValue}`;
            this._input.select();
        }
    }
    serializeValue() {
        const elmValue = this._input?.value;
        if (elmValue === undefined || elmValue === '' || isNaN(+elmValue)) {
            return elmValue;
        }
        const output = isNaN(+elmValue) ? elmValue : parseInt(elmValue, 10);
        return isNaN(+output) ? elmValue : output;
    }
    validate(_targetElm, inputValue) {
        // when using Composite Editor, we also want to recheck if the field if disabled/enabled since it might change depending on other inputs on the composite form
        if (this.args.compositeEditorOptions) {
            this.applyInputUsabilityState();
        }
        // when field is disabled, we can assume it's valid
        if (this.disabled) {
            return { valid: true, msg: '' };
        }
        const elmValue = inputValue !== undefined ? inputValue : this.getValue();
        return (0, integerValidator_js_1.integerValidator)(elmValue, {
            editorArgs: this.args,
            errorMessage: this.columnEditor.errorMessage,
            minValue: this.columnEditor.minValue,
            maxValue: this.columnEditor.maxValue,
            operatorConditionalType: this.columnEditor.operatorConditionalType,
            required: this.args?.compositeEditorOptions ? false : this.columnEditor.required,
            validator: this.validator,
        });
    }
    // --
    // protected functions
    // ------------------
    /** When the input value changes (this will cover the input spinner arrows on the right) */
    handleOnMouseWheel(event) {
        this._isValueTouched = true;
        const compositeEditorOptions = this.args.compositeEditorOptions;
        if (compositeEditorOptions) {
            this.handleChangeOnCompositeEditor(event, compositeEditorOptions);
        }
    }
}
exports.IntegerEditor = IntegerEditor;
//# sourceMappingURL=integerEditor.js.map