/**
 * DevExtreme (cjs/ui/text_box/ui.text_editor.mask.rule.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.StubMaskRule = exports.MaskRule = exports.EmptyMaskRule = void 0;
var _extend = require("../../core/utils/extend");
var _type = require("../../core/utils/type");

function _inheritsLoose(subClass, superClass) {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    _setPrototypeOf(subClass, superClass)
}

function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
        o.__proto__ = p;
        return o
    };
    return _setPrototypeOf(o, p)
}
const EMPTY_CHAR = " ";
let BaseMaskRule = function() {
    function BaseMaskRule(config) {
        this._value = " ";
        (0, _extend.extend)(this, config)
    }
    var _proto = BaseMaskRule.prototype;
    _proto.next = function(rule) {
        if (!arguments.length) {
            return this._next
        }
        this._next = rule
    };
    _proto._prepareHandlingArgs = function(args, config) {
        var _config$str, _config$start, _config$length;
        config = config || {};
        const handlingProperty = Object.prototype.hasOwnProperty.call(args, "value") ? "value" : "text";
        args[handlingProperty] = null !== (_config$str = config.str) && void 0 !== _config$str ? _config$str : args[handlingProperty];
        args.start = null !== (_config$start = config.start) && void 0 !== _config$start ? _config$start : args.start;
        args.length = null !== (_config$length = config.length) && void 0 !== _config$length ? _config$length : args.length;
        args.index = args.index + 1;
        return args
    };
    _proto.first = function(index) {
        index = index || 0;
        return this.next().first(index + 1)
    };
    _proto.isAccepted = function() {
        return false
    };
    _proto.adjustedCaret = function(caret, isForwardDirection, char) {
        return isForwardDirection ? this._adjustedForward(caret, 0, char) : this._adjustedBackward(caret, 0, char)
    };
    _proto._adjustedForward = function() {};
    _proto._adjustedBackward = function() {};
    _proto.isValid = function() {};
    _proto.reset = function() {};
    _proto.clear = function() {};
    _proto.text = function() {};
    _proto.value = function() {};
    _proto.rawValue = function() {};
    _proto.handle = function() {};
    return BaseMaskRule
}();
let EmptyMaskRule = function(_BaseMaskRule) {
    _inheritsLoose(EmptyMaskRule, _BaseMaskRule);

    function EmptyMaskRule() {
        return _BaseMaskRule.apply(this, arguments) || this
    }
    var _proto2 = EmptyMaskRule.prototype;
    _proto2.next = function() {};
    _proto2.handle = function() {
        return 0
    };
    _proto2.text = function() {
        return ""
    };
    _proto2.value = function() {
        return ""
    };
    _proto2.first = function() {
        return 0
    };
    _proto2.rawValue = function() {
        return ""
    };
    _proto2.adjustedCaret = function() {
        return 0
    };
    _proto2.isValid = function() {
        return true
    };
    return EmptyMaskRule
}(BaseMaskRule);
exports.EmptyMaskRule = EmptyMaskRule;
let MaskRule = function(_BaseMaskRule2) {
    _inheritsLoose(MaskRule, _BaseMaskRule2);

    function MaskRule() {
        return _BaseMaskRule2.apply(this, arguments) || this
    }
    var _proto3 = MaskRule.prototype;
    _proto3.text = function() {
        return (" " !== this._value ? this._value : this.maskChar) + this.next().text()
    };
    _proto3.value = function() {
        return this._value + this.next().value()
    };
    _proto3.rawValue = function() {
        return this._value + this.next().rawValue()
    };
    _proto3.handle = function(args) {
        const str = Object.prototype.hasOwnProperty.call(args, "value") ? args.value : args.text;
        if (!str || !str.length || !args.length) {
            return 0
        }
        if (args.start) {
            return this.next().handle(this._prepareHandlingArgs(args, {
                start: args.start - 1
            }))
        }
        const char = str[0];
        const rest = str.substring(1);
        this._tryAcceptChar(char, args);
        return this._accepted() ? this.next().handle(this._prepareHandlingArgs(args, {
            str: rest,
            length: args.length - 1
        })) + 1 : this.handle(this._prepareHandlingArgs(args, {
            str: rest,
            length: args.length - 1
        }))
    };
    _proto3.clear = function(args) {
        this._tryAcceptChar(" ", args);
        this.next().clear(this._prepareHandlingArgs(args))
    };
    _proto3.reset = function() {
        this._accepted(false);
        this.next().reset()
    };
    _proto3._tryAcceptChar = function(char, args) {
        this._accepted(false);
        if (!this._isAllowed(char, args)) {
            return
        }
        const acceptedChar = " " === char ? this.maskChar : char;
        args.fullText = args.fullText.substring(0, args.index) + acceptedChar + args.fullText.substring(args.index + 1);
        this._accepted(true);
        this._value = char
    };
    _proto3._accepted = function(value) {
        if (!arguments.length) {
            return !!this._isAccepted
        }
        this._isAccepted = !!value
    };
    _proto3.first = function(index) {
        return " " === this._value ? index || 0 : _BaseMaskRule2.prototype.first.call(this, index)
    };
    _proto3._isAllowed = function(char, args) {
        if (" " === char) {
            return true
        }
        return this._isValid(char, args)
    };
    _proto3._isValid = function(char, args) {
        const allowedChars = this.allowedChars;
        if (allowedChars instanceof RegExp) {
            return allowedChars.test(char)
        }
        if ((0, _type.isFunction)(allowedChars)) {
            return allowedChars(char, args.index, args.fullText)
        }
        if (Array.isArray(allowedChars)) {
            return allowedChars.includes(char)
        }
        return allowedChars === char
    };
    _proto3.isAccepted = function(caret) {
        return 0 === caret ? this._accepted() : this.next().isAccepted(caret - 1)
    };
    _proto3._adjustedForward = function(caret, index, char) {
        if (index >= caret) {
            return index
        }
        return this.next()._adjustedForward(caret, index + 1, char) || index + 1
    };
    _proto3._adjustedBackward = function(caret, index) {
        if (index >= caret - 1) {
            return caret
        }
        return this.next()._adjustedBackward(caret, index + 1) || index + 1
    };
    _proto3.isValid = function(args) {
        return this._isValid(this._value, args) && this.next().isValid(this._prepareHandlingArgs(args))
    };
    return MaskRule
}(BaseMaskRule);
exports.MaskRule = MaskRule;
let StubMaskRule = function(_MaskRule) {
    _inheritsLoose(StubMaskRule, _MaskRule);

    function StubMaskRule() {
        return _MaskRule.apply(this, arguments) || this
    }
    var _proto4 = StubMaskRule.prototype;
    _proto4.value = function() {
        return this.next().value()
    };
    _proto4.handle = function(args) {
        const hasValueProperty = Object.prototype.hasOwnProperty.call(args, "value");
        const str = hasValueProperty ? args.value : args.text;
        if (!str.length || !args.length) {
            return 0
        }
        if (args.start || hasValueProperty) {
            return this.next().handle(this._prepareHandlingArgs(args, {
                start: args.start && args.start - 1
            }))
        }
        const char = str[0];
        const rest = str.substring(1);
        this._tryAcceptChar(char);
        const nextArgs = this._isAllowed(char) ? this._prepareHandlingArgs(args, {
            str: rest,
            length: args.length - 1
        }) : args;
        return this.next().handle(nextArgs) + 1
    };
    _proto4.clear = function(args) {
        this._accepted(false);
        this.next().clear(this._prepareHandlingArgs(args))
    };
    _proto4._tryAcceptChar = function(char) {
        this._accepted(this._isValid(char))
    };
    _proto4._isValid = function(char) {
        return char === this.maskChar
    };
    _proto4.first = function(index) {
        index = index || 0;
        return this.next().first(index + 1)
    };
    _proto4._adjustedForward = function(caret, index, char) {
        if (index >= caret && char === this.maskChar) {
            return index
        }
        if (caret === index + 1 && this._accepted()) {
            return caret
        }
        return this.next()._adjustedForward(caret, index + 1, char)
    };
    _proto4._adjustedBackward = function(caret, index) {
        if (index >= caret - 1) {
            return 0
        }
        return this.next()._adjustedBackward(caret, index + 1)
    };
    _proto4.isValid = function(args) {
        return this.next().isValid(this._prepareHandlingArgs(args))
    };
    return StubMaskRule
}(MaskRule);
exports.StubMaskRule = StubMaskRule;
