import * as i0 from '@angular/core';
import { numberAttribute, input, signal, inject, effect, HostBinding, ViewChild, ChangeDetectionStrategy, Component } from '@angular/core';
import * as i2 from 'primeng/textarea';
import { TextareaModule } from 'primeng/textarea';
import * as i1 from '@angular/forms';
import { Validators, NgControl, FormsModule } from '@angular/forms';
import { characterLimitValidator, FieldValidation } from '@masterteam/components/field-validation';
import { isInvalid } from '@masterteam/components';
import { Tooltip } from '@masterteam/components/tooltip';
import { Icon } from '@masterteam/icons';

const TEXTAREA_FIELD_MAX_LENGTH = 2000;
const optionalNumberAttribute = (value) => value === null || value === undefined
    ? null
    : numberAttribute(value, TEXTAREA_FIELD_MAX_LENGTH);
// TODO: text stuck when reset
class TextareaField {
    textarea;
    field = input(true, ...(ngDevMode ? [{ debugName: "field" }] : /* istanbul ignore next */ []));
    hint = input(...(ngDevMode ? [undefined, { debugName: "hint" }] : /* istanbul ignore next */ []));
    label = input(...(ngDevMode ? [undefined, { debugName: "label" }] : /* istanbul ignore next */ []));
    placeholder = input(...(ngDevMode ? [undefined, { debugName: "placeholder" }] : /* istanbul ignore next */ []));
    class = input('', ...(ngDevMode ? [{ debugName: "class" }] : /* istanbul ignore next */ []));
    readonly = input(false, ...(ngDevMode ? [{ debugName: "readonly" }] : /* istanbul ignore next */ []));
    noErrorStyle = input(false, ...(ngDevMode ? [{ debugName: "noErrorStyle" }] : /* istanbul ignore next */ []));
    pInputs = input(...(ngDevMode ? [undefined, { debugName: "pInputs" }] : /* istanbul ignore next */ []));
    rows = input('4', ...(ngDevMode ? [{ debugName: "rows" }] : /* istanbul ignore next */ []));
    required = input(false, ...(ngDevMode ? [{ debugName: "required" }] : /* istanbul ignore next */ []));
    maxLength = input(TEXTAREA_FIELD_MAX_LENGTH, { ...(ngDevMode ? { debugName: "maxLength" } : /* istanbul ignore next */ {}), transform: optionalNumberAttribute });
    styleClass;
    requiredValidator = Validators.required;
    maxLengthValidator = null;
    value = signal(null, ...(ngDevMode ? [{ debugName: "value" }] : /* istanbul ignore next */ []));
    disabled = signal(false, ...(ngDevMode ? [{ debugName: "disabled" }] : /* istanbul ignore next */ []));
    onTouched = () => { };
    onModelChange = () => { };
    ngControl = inject(NgControl, { self: true, optional: true });
    isInvalid = isInvalid;
    constructor() {
        if (this.ngControl) {
            this.ngControl.valueAccessor = this;
        }
        effect(() => {
            const control = this.ngControl?.control;
            if (!control) {
                return;
            }
            if (this.required()) {
                control.addValidators(this.requiredValidator);
            }
            if (this.maxLengthValidator) {
                control.removeValidators(this.maxLengthValidator);
                this.maxLengthValidator = null;
            }
            const maxLength = this.maxLength();
            if (maxLength !== null) {
                this.maxLengthValidator = characterLimitValidator(maxLength);
                control.addValidators(this.maxLengthValidator);
            }
            control.updateValueAndValidity({ emitEvent: false });
        });
    }
    applyInputsToTextarea() {
        Object.assign(this.textarea, this.pInputs());
    }
    ngOnInit() {
        this.styleClass = this.class();
        if (this.pInputs()) {
            this.applyInputsToTextarea();
        }
    }
    onValueChange(value) {
        this.value.set(value);
        const control = this.ngControl?.control;
        if (control) {
            control.setValue(value);
            control.markAsDirty();
            control.markAsTouched();
            control.updateValueAndValidity({ emitEvent: false });
            return;
        }
        this.onModelChange(value);
    }
    ngOnChanges(changes) {
        if (changes['pInputs']) {
            this.applyInputsToTextarea();
        }
    }
    writeValue(value) {
        this.value.set(value);
    }
    registerOnChange(fn) {
        this.onModelChange = fn;
    }
    registerOnTouched(fn) {
        this.onTouched = fn;
    }
    setDisabledState(disabled) {
        this.disabled.set(disabled);
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.8", ngImport: i0, type: TextareaField, deps: [], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "21.2.8", type: TextareaField, isStandalone: true, selector: "mt-textarea-field", inputs: { field: { classPropertyName: "field", publicName: "field", isSignal: true, isRequired: false, transformFunction: null }, hint: { classPropertyName: "hint", publicName: "hint", isSignal: true, isRequired: false, transformFunction: null }, label: { classPropertyName: "label", publicName: "label", isSignal: true, isRequired: false, transformFunction: null }, placeholder: { classPropertyName: "placeholder", publicName: "placeholder", isSignal: true, isRequired: false, transformFunction: null }, class: { classPropertyName: "class", publicName: "class", isSignal: true, isRequired: false, transformFunction: null }, readonly: { classPropertyName: "readonly", publicName: "readonly", isSignal: true, isRequired: false, transformFunction: null }, noErrorStyle: { classPropertyName: "noErrorStyle", publicName: "noErrorStyle", isSignal: true, isRequired: false, transformFunction: null }, pInputs: { classPropertyName: "pInputs", publicName: "pInputs", isSignal: true, isRequired: false, transformFunction: null }, rows: { classPropertyName: "rows", publicName: "rows", isSignal: true, isRequired: false, transformFunction: null }, required: { classPropertyName: "required", publicName: "required", isSignal: true, isRequired: false, transformFunction: null }, maxLength: { classPropertyName: "maxLength", publicName: "maxLength", isSignal: true, isRequired: false, transformFunction: null } }, host: { properties: { "class": "this.styleClass" }, classAttribute: "grid gap-1" }, viewQueries: [{ propertyName: "textarea", first: true, predicate: ["textarea"], descendants: true, static: true }], usesOnChanges: true, ngImport: i0, template: "@if (label() || hint()) {\n  <div class=\"flex items-center gap-1\">\r\n    @if (label()) {\r\n      <label\r\n        [class.required]=\"ngControl?.control?.hasValidator(requiredValidator)\"\r\n        [for]=\"ngControl?.name || label()\"\r\n        >{{ label() }}</label\r\n      >\r\n    }\r\n    @if (hint()) {\n      <span\n        class=\"inline-flex text-surface-400\"\n        [mtTooltip]=\"hint()\"\n        tooltipPosition=\"top\"\n      >\n        <mt-icon icon=\"general.help-circle\" />\r\n      </span>\r\n    }\r\n  </div>\r\n}\r\n<textarea\r\n  #textarea\r\n  pTextarea\r\n  [ngModel]=\"value()\"\r\n  (ngModelChange)=\"onValueChange($event)\"\r\n  (blur)=\"onTouched()\"\r\n  [disabled]=\"disabled() || readonly()\"\r\n  [id]=\"ngControl?.name || label()\"\r\n  [invalid]=\"!noErrorStyle() && isInvalid(ngControl?.control)\"\r\n  (blur)=\"onTouched()\"\r\n  placeholder=\"{{ placeholder() ?? label() ?? '' }}\"\r\n  class=\"w-full\"\r\n  [rows]=\"rows()\"\r\n  style=\"resize: none\"\r\n></textarea>\r\n\r\n<mt-field-validation [control]=\"ngControl?.control\"></mt-field-validation>\r\n", styles: [""], dependencies: [{ kind: "ngmodule", type: FormsModule }, { kind: "directive", type: i1.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i1.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i1.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "ngmodule", type: TextareaModule }, { kind: "directive", type: i2.Textarea, selector: "[pTextarea], [pInputTextarea]", inputs: ["pTextareaPT", "pTextareaUnstyled", "autoResize", "pSize", "variant", "fluid", "invalid"], outputs: ["onResize"] }, { kind: "component", type: FieldValidation, selector: "mt-field-validation", inputs: ["control", "touched"] }, { kind: "directive", type: Tooltip, selector: "[mtTooltip]" }, { kind: "component", type: Icon, selector: "mt-icon", inputs: ["icon"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.8", ngImport: i0, type: TextareaField, decorators: [{
            type: Component,
            args: [{ selector: 'mt-textarea-field', standalone: true, imports: [FormsModule, TextareaModule, FieldValidation, Tooltip, Icon], changeDetection: ChangeDetectionStrategy.OnPush, host: {
                        class: 'grid gap-1',
                    }, template: "@if (label() || hint()) {\n  <div class=\"flex items-center gap-1\">\r\n    @if (label()) {\r\n      <label\r\n        [class.required]=\"ngControl?.control?.hasValidator(requiredValidator)\"\r\n        [for]=\"ngControl?.name || label()\"\r\n        >{{ label() }}</label\r\n      >\r\n    }\r\n    @if (hint()) {\n      <span\n        class=\"inline-flex text-surface-400\"\n        [mtTooltip]=\"hint()\"\n        tooltipPosition=\"top\"\n      >\n        <mt-icon icon=\"general.help-circle\" />\r\n      </span>\r\n    }\r\n  </div>\r\n}\r\n<textarea\r\n  #textarea\r\n  pTextarea\r\n  [ngModel]=\"value()\"\r\n  (ngModelChange)=\"onValueChange($event)\"\r\n  (blur)=\"onTouched()\"\r\n  [disabled]=\"disabled() || readonly()\"\r\n  [id]=\"ngControl?.name || label()\"\r\n  [invalid]=\"!noErrorStyle() && isInvalid(ngControl?.control)\"\r\n  (blur)=\"onTouched()\"\r\n  placeholder=\"{{ placeholder() ?? label() ?? '' }}\"\r\n  class=\"w-full\"\r\n  [rows]=\"rows()\"\r\n  style=\"resize: none\"\r\n></textarea>\r\n\r\n<mt-field-validation [control]=\"ngControl?.control\"></mt-field-validation>\r\n" }]
        }], ctorParameters: () => [], propDecorators: { textarea: [{
                type: ViewChild,
                args: ['textarea', { static: true }]
            }], field: [{ type: i0.Input, args: [{ isSignal: true, alias: "field", required: false }] }], hint: [{ type: i0.Input, args: [{ isSignal: true, alias: "hint", required: false }] }], label: [{ type: i0.Input, args: [{ isSignal: true, alias: "label", required: false }] }], placeholder: [{ type: i0.Input, args: [{ isSignal: true, alias: "placeholder", required: false }] }], class: [{ type: i0.Input, args: [{ isSignal: true, alias: "class", required: false }] }], readonly: [{ type: i0.Input, args: [{ isSignal: true, alias: "readonly", required: false }] }], noErrorStyle: [{ type: i0.Input, args: [{ isSignal: true, alias: "noErrorStyle", required: false }] }], pInputs: [{ type: i0.Input, args: [{ isSignal: true, alias: "pInputs", required: false }] }], rows: [{ type: i0.Input, args: [{ isSignal: true, alias: "rows", required: false }] }], required: [{ type: i0.Input, args: [{ isSignal: true, alias: "required", required: false }] }], maxLength: [{ type: i0.Input, args: [{ isSignal: true, alias: "maxLength", required: false }] }], styleClass: [{
                type: HostBinding,
                args: ['class']
            }] } });

/**
 * Generated bundle index. Do not edit.
 */

export { TextareaField };
//# sourceMappingURL=masterteam-components-textarea-field.mjs.map
