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

class CheckboxField {
    checkbox;
    label = input(...(ngDevMode ? [undefined, { debugName: "label" }] : []));
    labelPosition = input('end', ...(ngDevMode ? [{ debugName: "labelPosition" }] : []));
    placeholder = input(...(ngDevMode ? [undefined, { debugName: "placeholder" }] : []));
    readonly = input(false, ...(ngDevMode ? [{ debugName: "readonly" }] : []));
    pInputs = input(...(ngDevMode ? [undefined, { debugName: "pInputs" }] : []));
    required = input(false, ...(ngDevMode ? [{ debugName: "required" }] : []));
    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();
            // Ensure checkbox ViewChild is initialized and pInputs has a value
            // @ViewChild with static: true makes 'checkbox' available during construction/ngOnInit
            if (this.checkbox && currentPInputs) {
                this.applyInputsToCheckbox();
            }
        });
        // 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();
            }
        });
    }
    applyInputsToCheckbox() {
        const currentPInputs = this.pInputs();
        if (currentPInputs) {
            Object.assign(this.checkbox, 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: CheckboxField, deps: [], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "21.0.3", type: CheckboxField, isStandalone: true, selector: "mt-checkbox-field", inputs: { label: { classPropertyName: "label", publicName: "label", isSignal: true, isRequired: false, transformFunction: null }, labelPosition: { classPropertyName: "labelPosition", publicName: "labelPosition", isSignal: true, isRequired: false, transformFunction: null }, placeholder: { classPropertyName: "placeholder", publicName: "placeholder", 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 } }, outputs: { onChange: "onChange" }, host: { classAttribute: "grid gap-1" }, viewQueries: [{ propertyName: "checkbox", first: true, predicate: ["checkbox"], descendants: true, static: true }], ngImport: i0, template: "<div\r\n  class=\"flex gap-1\"\r\n  [class.items-center]=\"!isVertical()\"\r\n  [class.gap-2]=\"!isVertical()\"\r\n  [class.flex-col]=\"isVertical()\"\r\n  [class.flex-col-reverse]=\"labelPosition() === 'top'\"\r\n  [class.flex-row-reverse]=\"labelPosition() === 'start'\"\r\n  [class.justify-end]=\"labelPosition() === 'start'\"\r\n>\r\n  <p-checkbox\r\n    #checkbox\r\n    [ngModel]=\"value()\"\r\n    (ngModelChange)=\"onValueChange($event)\"\r\n    (onChange)=\"onChange.emit($event)\"\r\n    (onBlur)=\"onTouched()\"\r\n    [disabled]=\"disabled() || readonly()\"\r\n    [inputId]=\"ngControl?.name ? ngControl?.name?.toString() : label()\"\r\n    [binary]=\"true\"\r\n    [invalid]=\"isInvalid(ngControl?.control)\"\r\n    class=\"leading-none\"\r\n  ></p-checkbox>\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</div>\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.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: CheckboxModule }, { kind: "component", type: i2.Checkbox, selector: "p-checkbox, p-checkBox, p-check-box", inputs: ["hostName", "value", "binary", "ariaLabelledBy", "ariaLabel", "tabindex", "inputId", "inputStyle", "styleClass", "inputClass", "indeterminate", "formControl", "checkboxIcon", "readonly", "autofocus", "trueValue", "falseValue", "variant", "size"], outputs: ["onChange", "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: CheckboxField, decorators: [{
            type: Component,
            args: [{ selector: 'mt-checkbox-field', standalone: true, imports: [FormsModule, CheckboxModule, FieldValidation], changeDetection: ChangeDetectionStrategy.OnPush, host: {
                        class: 'grid gap-1',
                    }, template: "<div\r\n  class=\"flex gap-1\"\r\n  [class.items-center]=\"!isVertical()\"\r\n  [class.gap-2]=\"!isVertical()\"\r\n  [class.flex-col]=\"isVertical()\"\r\n  [class.flex-col-reverse]=\"labelPosition() === 'top'\"\r\n  [class.flex-row-reverse]=\"labelPosition() === 'start'\"\r\n  [class.justify-end]=\"labelPosition() === 'start'\"\r\n>\r\n  <p-checkbox\r\n    #checkbox\r\n    [ngModel]=\"value()\"\r\n    (ngModelChange)=\"onValueChange($event)\"\r\n    (onChange)=\"onChange.emit($event)\"\r\n    (onBlur)=\"onTouched()\"\r\n    [disabled]=\"disabled() || readonly()\"\r\n    [inputId]=\"ngControl?.name ? ngControl?.name?.toString() : label()\"\r\n    [binary]=\"true\"\r\n    [invalid]=\"isInvalid(ngControl?.control)\"\r\n    class=\"leading-none\"\r\n  ></p-checkbox>\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</div>\r\n\r\n<mt-field-validation [control]=\"ngControl?.control\"></mt-field-validation>\r\n" }]
        }], ctorParameters: () => [], propDecorators: { checkbox: [{
                type: ViewChild,
                args: ['checkbox', { static: true }]
            }], label: [{ type: i0.Input, args: [{ isSignal: true, alias: "label", required: false }] }], labelPosition: [{ type: i0.Input, args: [{ isSignal: true, alias: "labelPosition", required: false }] }], placeholder: [{ type: i0.Input, args: [{ isSignal: true, alias: "placeholder", 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 }] }], onChange: [{ type: i0.Output, args: ["onChange"] }] } });

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

export { CheckboxField };
//# sourceMappingURL=masterteam-components-checkbox-field.mjs.map
