export class MaskPosition {
    constructor(_options) {
        this._options = _options;
        this._position = 0;
        this._positionEnd = 0;
        this._isRangeSelection = false;
        if (_options.component && _options.component.nativeInput) {
            this._position = _options.component.nativeInput.selectionStart || 0;
            this._positionEnd = _options.component.nativeInput.selectionEnd || 0;
            this._isRangeSelection = this._position !== this._positionEnd;
        }
    }
    get isRangeSelection() {
        return this._isRangeSelection;
    }
    get value() {
        if (this._options.isBackspace && this._position > 0) {
            return this._position - 1;
        }
        return this._position;
    }
    set value(index) {
        const validatedIndex = index < 0 ? 0 : index > this._options.mask.maxLength ? this._options.mask.maxLength : index;
        this._position = validatedIndex;
    }
    toStart() {
        this.value = 0;
    }
    toEnd() {
        const target = this.getTarget();
        if (target) {
            this.value = target.value.length;
        }
    }
    next(next = 1) {
        const newPosition = this._position + next;
        if (newPosition <= this._options.mask.maxLength) {
            this._position = newPosition;
        }
        return this._position;
    }
    previous(previous = 1) {
        const newPosition = this._position - previous;
        if (newPosition >= 0) {
            this._position = newPosition;
        }
        else {
            this._position = 0;
        }
        return this._position;
    }
    submit() {
        const target = this.getTarget();
        if (target && target.selectionStart !== this._position && target.selectionEnd !== this._position) {
            target.selectionStart = this._position;
            target.selectionEnd = this._position;
        }
    }
    getTarget() {
        if (this._options.component && this._options.component.nativeInput) {
            return this._options.component.nativeInput;
        }
        return undefined;
    }
}
