import * as i0 from '@angular/core';
import { 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 { FieldValidation } from '@masterteam/components/field-validation';
import { isInvalid } from '@masterteam/components';

// TODO: text stuck when reset
class TextareaField {
    textarea;
    field = input(true, ...(ngDevMode ? [{ debugName: "field" }] : []));
    label = input(...(ngDevMode ? [undefined, { debugName: "label" }] : []));
    placeholder = input(...(ngDevMode ? [undefined, { debugName: "placeholder" }] : []));
    class = input('', ...(ngDevMode ? [{ debugName: "class" }] : []));
    readonly = input(false, ...(ngDevMode ? [{ debugName: "readonly" }] : []));
    noErrorStyle = input(false, ...(ngDevMode ? [{ debugName: "noErrorStyle" }] : []));
    pInputs = input(...(ngDevMode ? [undefined, { debugName: "pInputs" }] : []));
    rows = input('4', ...(ngDevMode ? [{ debugName: "rows" }] : []));
    required = input(false, ...(ngDevMode ? [{ debugName: "required" }] : []));
    styleClass;
    requiredValidator = Validators.required;
    value = signal(null, ...(ngDevMode ? [{ debugName: "value" }] : []));
    disabled = signal(false, ...(ngDevMode ? [{ debugName: "disabled" }] : []));
    onTouched = () => { };
    onModelChange = () => { };
    ngControl = inject(NgControl, { self: true });
    isInvalid = isInvalid;
    constructor() {
        if (this.ngControl) {
            this.ngControl.valueAccessor = this;
        }
        effect(() => {
            if (this.ngControl.control && this.required()) {
                this.ngControl.control.addValidators(Validators.required);
                this.ngControl.control.updateValueAndValidity();
            }
        });
    }
    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.0.3", ngImport: i0, type: TextareaField, deps: [], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "21.0.3", type: TextareaField, isStandalone: true, selector: "mt-textarea-field", inputs: { field: { classPropertyName: "field", publicName: "field", 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 } }, 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()) {\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<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"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.3", ngImport: i0, type: TextareaField, decorators: [{
            type: Component,
            args: [{ selector: 'mt-textarea-field', standalone: true, imports: [FormsModule, TextareaModule, FieldValidation], changeDetection: ChangeDetectionStrategy.OnPush, host: {
                        class: 'grid gap-1',
                    }, template: "@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<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 }] }], 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 }] }], styleClass: [{
                type: HostBinding,
                args: ['class']
            }] } });

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

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