import * as i0 from '@angular/core';
import { input, output, inject, computed, signal, effect, ChangeDetectionStrategy, Component } from '@angular/core';
import * as i1 from '@angular/forms';
import { NgControl, Validators, FormsModule } from '@angular/forms';
import * as i2 from 'primeng/radiobutton';
import { RadioButtonModule } from 'primeng/radiobutton';
import { FieldValidation } from '@masterteam/components/field-validation';
import { isInvalid } from '@masterteam/components';

class RadioButtonField {
    radioButton;
    labelPosition = input('end', ...(ngDevMode ? [{ debugName: "labelPosition" }] : []));
    orientation = input('vertical', ...(ngDevMode ? [{ debugName: "orientation" }] : []));
    readonly = input(false, ...(ngDevMode ? [{ debugName: "readonly" }] : []));
    pInputs = input(...(ngDevMode ? [undefined, { debugName: "pInputs" }] : []));
    required = input(false, ...(ngDevMode ? [{ debugName: "required" }] : []));
    options = input([], ...(ngDevMode ? [{ debugName: "options" }] : []));
    size = input(...(ngDevMode ? [undefined, { debugName: "size" }] : []));
    label = input(...(ngDevMode ? [undefined, { debugName: "label" }] : []));
    onChange = output();
    ngControl = inject(NgControl, { self: true });
    isInvalid = isInvalid;
    isVertical = computed(() => this.labelPosition() === 'top' || this.labelPosition() === 'bottom', ...(ngDevMode ? [{ debugName: "isVertical" }] : []));
    requiredValidator = Validators.required;
    value = signal(null, ...(ngDevMode ? [{ debugName: "value" }] : []));
    disabled = signal(false, ...(ngDevMode ? [{ debugName: "disabled" }] : []));
    onTouched = () => { };
    onModelChange = () => { };
    constructor() {
        if (this.ngControl) {
            this.ngControl.valueAccessor = this;
        }
        // Effect to apply pInputs reactively when the pInputs signal changes
        effect(() => {
            const currentPInputs = this.pInputs();
            if (this.radioButton && currentPInputs) {
                this.applyInputsToRadioButton();
            }
        });
        // Effect to add required validator if required input is true
        effect(() => {
            if (this.ngControl?.control && this.required()) {
                this.ngControl.control.addValidators(Validators.required);
                this.ngControl.control.updateValueAndValidity();
            }
        });
    }
    applyInputsToRadioButton() {
        const currentPInputs = this.pInputs();
        if (currentPInputs) {
            Object.assign(this.radioButton, currentPInputs);
        }
    }
    onValueChange(value) {
        this.onModelChange(value);
        this.value.set(value);
    }
    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: RadioButtonField, deps: [], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "21.0.3", type: RadioButtonField, isStandalone: true, selector: "mt-radio-button-field", inputs: { labelPosition: { classPropertyName: "labelPosition", publicName: "labelPosition", isSignal: true, isRequired: false, transformFunction: null }, orientation: { classPropertyName: "orientation", publicName: "orientation", isSignal: true, isRequired: false, transformFunction: null }, readonly: { classPropertyName: "readonly", publicName: "readonly", isSignal: true, isRequired: false, transformFunction: null }, pInputs: { classPropertyName: "pInputs", publicName: "pInputs", isSignal: true, isRequired: false, transformFunction: null }, required: { classPropertyName: "required", publicName: "required", isSignal: true, isRequired: false, transformFunction: null }, options: { classPropertyName: "options", publicName: "options", isSignal: true, isRequired: false, transformFunction: null }, size: { classPropertyName: "size", publicName: "size", isSignal: true, isRequired: false, transformFunction: null }, label: { classPropertyName: "label", publicName: "label", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { onChange: "onChange" }, host: { classAttribute: "grid gap-1" }, ngImport: i0, template: "@if (label()) {\n  <label\n    [class.required]=\"ngControl?.control?.hasValidator(requiredValidator)\"\n    >{{ label() }}</label\n  >\n}\n\n<div\n  class=\"flex gap-3\"\n  [class.flex-col]=\"orientation() === 'vertical'\"\n  [class.flex-row]=\"orientation() === 'horizontal'\"\n  [class.flex-wrap]=\"orientation() === 'horizontal'\"\n>\n  @for (option of options(); track option) {\n    <div\n      class=\"flex gap-1\"\n      [class.items-center]=\"!isVertical()\"\n      [class.gap-2]=\"!isVertical()\"\n      [class.flex-col]=\"isVertical()\"\n      [class.flex-col-reverse]=\"labelPosition() === 'top'\"\n      [class.flex-row-reverse]=\"labelPosition() === 'start'\"\n      [class.justify-end]=\"labelPosition() === 'start'\"\n    >\n      <p-radiobutton\n        [size]=\"size()\"\n        #radiobutton\n        [ngModel]=\"value()\"\n        [value]=\"option.value\"\n        [inputId]=\"option?.label\"\n        (ngModelChange)=\"onValueChange($event)\"\n        (onClick)=\"onChange.emit($event)\"\n        (onBlur)=\"onTouched()\"\n        [disabled]=\"disabled() || readonly()\"\n        [invalid]=\"isInvalid(ngControl?.control)\"\n        class=\"leading-none\"\n      ></p-radiobutton>\n\n      @if (option?.label) {\n        <label [for]=\"option?.label\">{{ option?.label }}</label>\n      }\n    </div>\n  }\n</div>\n<mt-field-validation [control]=\"ngControl?.control\"></mt-field-validation>\n", styles: [""], dependencies: [{ kind: "ngmodule", type: FormsModule }, { 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: RadioButtonModule }, { kind: "component", type: i2.RadioButton, selector: "p-radioButton, p-radiobutton, p-radio-button", inputs: ["value", "tabindex", "inputId", "ariaLabelledBy", "ariaLabel", "styleClass", "autofocus", "binary", "variant", "size"], outputs: ["onClick", "onFocus", "onBlur"] }, { 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: RadioButtonField, decorators: [{
            type: Component,
            args: [{ selector: 'mt-radio-button-field', standalone: true, imports: [FormsModule, RadioButtonModule, FieldValidation], changeDetection: ChangeDetectionStrategy.OnPush, host: {
                        class: 'grid gap-1',
                    }, template: "@if (label()) {\n  <label\n    [class.required]=\"ngControl?.control?.hasValidator(requiredValidator)\"\n    >{{ label() }}</label\n  >\n}\n\n<div\n  class=\"flex gap-3\"\n  [class.flex-col]=\"orientation() === 'vertical'\"\n  [class.flex-row]=\"orientation() === 'horizontal'\"\n  [class.flex-wrap]=\"orientation() === 'horizontal'\"\n>\n  @for (option of options(); track option) {\n    <div\n      class=\"flex gap-1\"\n      [class.items-center]=\"!isVertical()\"\n      [class.gap-2]=\"!isVertical()\"\n      [class.flex-col]=\"isVertical()\"\n      [class.flex-col-reverse]=\"labelPosition() === 'top'\"\n      [class.flex-row-reverse]=\"labelPosition() === 'start'\"\n      [class.justify-end]=\"labelPosition() === 'start'\"\n    >\n      <p-radiobutton\n        [size]=\"size()\"\n        #radiobutton\n        [ngModel]=\"value()\"\n        [value]=\"option.value\"\n        [inputId]=\"option?.label\"\n        (ngModelChange)=\"onValueChange($event)\"\n        (onClick)=\"onChange.emit($event)\"\n        (onBlur)=\"onTouched()\"\n        [disabled]=\"disabled() || readonly()\"\n        [invalid]=\"isInvalid(ngControl?.control)\"\n        class=\"leading-none\"\n      ></p-radiobutton>\n\n      @if (option?.label) {\n        <label [for]=\"option?.label\">{{ option?.label }}</label>\n      }\n    </div>\n  }\n</div>\n<mt-field-validation [control]=\"ngControl?.control\"></mt-field-validation>\n" }]
        }], ctorParameters: () => [], propDecorators: { labelPosition: [{ type: i0.Input, args: [{ isSignal: true, alias: "labelPosition", required: false }] }], orientation: [{ type: i0.Input, args: [{ isSignal: true, alias: "orientation", required: false }] }], readonly: [{ type: i0.Input, args: [{ isSignal: true, alias: "readonly", required: false }] }], pInputs: [{ type: i0.Input, args: [{ isSignal: true, alias: "pInputs", required: false }] }], required: [{ type: i0.Input, args: [{ isSignal: true, alias: "required", required: false }] }], options: [{ type: i0.Input, args: [{ isSignal: true, alias: "options", required: false }] }], size: [{ type: i0.Input, args: [{ isSignal: true, alias: "size", required: false }] }], label: [{ type: i0.Input, args: [{ isSignal: true, alias: "label", required: false }] }], onChange: [{ type: i0.Output, args: ["onChange"] }] } });

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

export { RadioButtonField };
//# sourceMappingURL=masterteam-components-radio-button-field.mjs.map
