var _a;
import { __decorate, __extends } from "tslib";
import Component from 'vue-class-component';
import { Prop, Ref, Watch } from 'vue-property-decorator';
import { TEXTAREA_AUTO_HEIGHT } from '../../directives/directive-names';
import { MTextareaAutoHeight } from '../../directives/textarea-auto-height/textarea-auto-height';
import { InputLabel } from '../../mixins/input-label/input-label';
import { InputManagement } from '../../mixins/input-management/input-management';
import { InputState } from '../../mixins/input-state/input-state';
import { InputWidth } from '../../mixins/input-width/input-width';
import { Enums } from '../../utils/enums/enums';
import { changeSelection } from '../../utils/input/input';
import uuid from '../../utils/uuid/uuid';
import { ModulVue } from '../../utils/vue/vue';
import { MCharacterCount } from '../character-count/character-count';
import { TEXTFIELD_NAME } from '../component-names';
import { MIconButton } from '../icon-button/icon-button';
import { MInputStyle } from '../input-style/input-style';
import { MValidationMessage } from '../validation-message/validation-message';
import WithRender from './textfield.html?style=./textfield.scss';
export var MTextfieldType;
(function (MTextfieldType) {
    MTextfieldType["Text"] = "text";
    MTextfieldType["Password"] = "password";
    MTextfieldType["Email"] = "email";
    MTextfieldType["Url"] = "url";
    MTextfieldType["Telephone"] = "tel";
    MTextfieldType["Number"] = "number";
})(MTextfieldType || (MTextfieldType = {}));
var ICON_NAME_PASSWORD_VISIBLE = 'm-svg__show';
var ICON_NAME_PASSWORD_HIDDEN = 'm-svg__hide';
var MTextfield = /** @class */ (function (_super) {
    __extends(MTextfield, _super);
    function MTextfield() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.passwordAsText = false;
        _this.iconDescriptionShowPassword = _this.$i18n.translate('m-textfield:show-password');
        _this.iconDescriptionHidePassword = _this.$i18n.translate('m-textfield:hide-password');
        return _this;
    }
    MTextfield.prototype.created = function () {
        if (!this.$i18n) {
            throw new Error('<m-text-field> -> this.$i18n is undefined, you must install the i18n plugin.');
        }
    };
    MTextfield.prototype.mounted = function () {
        this.as().trimWordWrap = this.hasWordWrap;
    };
    MTextfield.prototype.typeChanged = function (type) {
        this.$log.warn(TEXTFIELD_NAME + ': Change of property "type" is not supported');
    };
    MTextfield.prototype.inputTypeChanged = function (value) {
        this.as().trimWordWrap = this.hasWordWrap;
    };
    MTextfield.prototype.wordWrapChanged = function (wordWrap) {
        this.as().trimWordWrap = this.hasWordWrap;
    };
    MTextfield.prototype.togglePasswordVisibility = function (event) {
        this.passwordAsText = !this.passwordAsText;
    };
    Object.defineProperty(MTextfield.prototype, "inputType", {
        get: function () {
            switch (this.type) {
                case MTextfieldType.Password:
                    return this.passwordAsText ? MTextfieldType.Text : MTextfieldType.Password;
                default:
                    return this.type;
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTextfield.prototype, "passwordIcon", {
        get: function () {
            return this.icon && this.type === MTextfieldType.Password && this.as().active;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTextfield.prototype, "passwordIconName", {
        get: function () {
            return this.passwordAsText ? ICON_NAME_PASSWORD_HIDDEN : ICON_NAME_PASSWORD_VISIBLE;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTextfield.prototype, "passwordIconDescription", {
        get: function () {
            return this.passwordAsText ? this.iconDescriptionHidePassword : this.iconDescriptionShowPassword;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTextfield.prototype, "hasWordWrap", {
        get: function () {
            var hasWordWrap = this.inputType === MTextfieldType.Text && this.wordWrap;
            if (this.inputType !== MTextfieldType.Text && this.wordWrap) {
                this.$log.warn(TEXTFIELD_NAME + ': If you want to use word-wrap prop, you need to set type prop at "text"');
            }
            return hasWordWrap;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTextfield.prototype, "valueLength", {
        get: function () {
            return (this.internalValue || '').length;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTextfield.prototype, "maxLengthNumber", {
        get: function () {
            return !this.lengthOverflow && this.maxLength > 0 ? this.maxLength : Infinity;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTextfield.prototype, "hasTextfieldError", {
        get: function () {
            return this.as().hasError;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTextfield.prototype, "isTextfieldValid", {
        get: function () {
            return this.as().isValid;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTextfield.prototype, "hasCounterTransition", {
        get: function () {
            return !this.as().hasErrorMessage;
        },
        enumerable: false,
        configurable: true
    });
    MTextfield.prototype.resetModel = function () {
        this.$emit('input', '');
    };
    MTextfield.prototype.updateSelection = function () {
        changeSelection(this.as().getInput(), this.selection);
    };
    __decorate([
        Prop({
            default: MTextfieldType.Text,
            validator: function (value) { return Enums.toValueArray(MTextfieldType).includes(value); }
        })
    ], MTextfield.prototype, "type", void 0);
    __decorate([
        Prop({ default: true })
    ], MTextfield.prototype, "icon", void 0);
    __decorate([
        Prop({ default: false })
    ], MTextfield.prototype, "wordWrap", void 0);
    __decorate([
        Prop()
    ], MTextfield.prototype, "characterCount", void 0);
    __decorate([
        Prop({ default: 0 })
    ], MTextfield.prototype, "maxLength", void 0);
    __decorate([
        Prop({ default: true })
    ], MTextfield.prototype, "lengthOverflow", void 0);
    __decorate([
        Prop({ default: 0 })
    ], MTextfield.prototype, "characterCountThreshold", void 0);
    __decorate([
        Prop({ default: '' })
    ], MTextfield.prototype, "selection", void 0);
    __decorate([
        Prop({ default: function () { return "mTextfield-" + uuid.generate(); } })
    ], MTextfield.prototype, "id", void 0);
    __decorate([
        Prop({ default: function () { return uuid.generate(); } })
    ], MTextfield.prototype, "inputAriaDescribedby", void 0);
    __decorate([
        Prop()
    ], MTextfield.prototype, "inputAriaActivedescendant", void 0);
    __decorate([
        Prop()
    ], MTextfield.prototype, "inputAriaAutocomplete", void 0);
    __decorate([
        Prop()
    ], MTextfield.prototype, "inputAriaControls", void 0);
    __decorate([
        Prop({ default: false })
    ], MTextfield.prototype, "append", void 0);
    __decorate([
        Ref('input')
    ], MTextfield.prototype, "refInput", void 0);
    __decorate([
        Watch('type')
    ], MTextfield.prototype, "typeChanged", null);
    __decorate([
        Watch('inputType')
    ], MTextfield.prototype, "inputTypeChanged", null);
    __decorate([
        Watch('wordWrap')
    ], MTextfield.prototype, "wordWrapChanged", null);
    __decorate([
        Watch('selection')
    ], MTextfield.prototype, "updateSelection", null);
    MTextfield = __decorate([
        WithRender,
        Component({
            components: {
                MInputStyle: MInputStyle,
                MValidationMessage: MValidationMessage,
                MCharacterCount: MCharacterCount,
                MIconButton: MIconButton
            },
            directives: (_a = {},
                _a[TEXTAREA_AUTO_HEIGHT] = MTextareaAutoHeight,
                _a),
            mixins: [
                InputState,
                InputManagement,
                InputWidth,
                InputLabel
            ]
        })
    ], MTextfield);
    return MTextfield;
}(ModulVue));
export { MTextfield };
var TextfieldPlugin = {
    install: function (v, options) {
        v.component(TEXTFIELD_NAME, MTextfield);
    }
};
export default TextfieldPlugin;
//# sourceMappingURL=textfield.js.map