import * as i0 from '@angular/core';
import { inject, ChangeDetectorRef, input, EventEmitter, signal, effect, HostBinding, Output, ViewChild, ChangeDetectionStrategy, Component } from '@angular/core';
import * as i1 from '@angular/forms';
import { Validators, NgControl, FormsModule } from '@angular/forms';
import { Select } from 'primeng/select';
import { FieldValidation } from '@masterteam/components/field-validation';
import { isInvalid } from '@masterteam/components';
import * as i2 from '@jsverse/transloco';
import { TranslocoModule } from '@jsverse/transloco';

class SelectField {
    cdr = inject(ChangeDetectorRef);
    dropdown;
    field = input(true, ...(ngDevMode ? [{ debugName: "field" }] : []));
    label = input('', ...(ngDevMode ? [{ debugName: "label" }] : []));
    placeholder = input('', ...(ngDevMode ? [{ debugName: "placeholder" }] : []));
    hasPlaceholderPrefix = input(true, ...(ngDevMode ? [{ debugName: "hasPlaceholderPrefix" }] : []));
    class = input('', ...(ngDevMode ? [{ debugName: "class" }] : []));
    readonly = input(false, ...(ngDevMode ? [{ debugName: "readonly" }] : []));
    pInputs = input(...(ngDevMode ? [undefined, { debugName: "pInputs" }] : []));
    options = input(undefined, ...(ngDevMode ? [{ debugName: "options" }] : []));
    optionValue = input(undefined, ...(ngDevMode ? [{ debugName: "optionValue" }] : []));
    optionLabel = input(undefined, ...(ngDevMode ? [{ debugName: "optionLabel" }] : []));
    filter = input(undefined, ...(ngDevMode ? [{ debugName: "filter" }] : []));
    filterBy = input(undefined, ...(ngDevMode ? [{ debugName: "filterBy" }] : []));
    dataKey = input(undefined, ...(ngDevMode ? [{ debugName: "dataKey" }] : []));
    showClear = input(undefined, ...(ngDevMode ? [{ debugName: "showClear" }] : []));
    clearAfterSelect = input(undefined, ...(ngDevMode ? [{ debugName: "clearAfterSelect" }] : []));
    required = input(false, ...(ngDevMode ? [{ debugName: "required" }] : [])); // Add readonly required input
    group = input(false, ...(ngDevMode ? [{ debugName: "group" }] : []));
    optionGroupLabel = input('label', ...(ngDevMode ? [{ debugName: "optionGroupLabel" }] : []));
    optionGroupChildren = input('items', ...(ngDevMode ? [{ debugName: "optionGroupChildren" }] : []));
    loading = input(false, ...(ngDevMode ? [{ debugName: "loading" }] : []));
    onChange = new EventEmitter();
    styleClass;
    requiredValidator = Validators.required;
    value = signal(null, ...(ngDevMode ? [{ debugName: "value" }] : []));
    disabled = signal(false, ...(ngDevMode ? [{ debugName: "disabled" }] : []));
    onTouched = () => { };
    onModelChange = () => { };
    ngControl = null;
    isInvalid = isInvalid;
    constructor() {
        try {
            this.ngControl = inject(NgControl, { self: true, optional: true });
            if (this.ngControl) {
                this.ngControl.valueAccessor = this;
            }
        }
        catch {
            this.ngControl = null;
        }
        effect(() => {
            if (this.ngControl?.control && this.required()) {
                this.ngControl.control.addValidators(Validators.required);
                this.ngControl.control.updateValueAndValidity();
            }
        });
    }
    applyInputsToDropdown() {
        // Object.assign(this.dropdown, this.pInputs());
    }
    ngOnInit() {
        this.styleClass = this.class();
        if (this.pInputs()) {
            this.applyInputsToDropdown();
        }
    }
    onValueChange(value) {
        this.onModelChange(value);
        this.value.set(value);
        this.onChange.emit(value);
        if (this.clearAfterSelect()) {
            this.clearSelection();
        }
    }
    ngOnChanges(changes) {
        if (changes['pInputs']) {
            this.applyInputsToDropdown();
        }
    }
    writeValue(value) {
        this.value.set(value);
    }
    registerOnChange(fn) {
        this.onModelChange = fn;
    }
    registerOnTouched(fn) {
        this.onTouched = fn;
    }
    setDisabledState(disabled) {
        this.disabled.set(disabled);
    }
    // TODO: make this method work
    clearSelection() {
        this.dropdown.clear(this.value());
        this.dropdown.value = null;
        this.value.set(null);
        this.onModelChange(null);
        this.onChange.emit(null);
        this.cdr.detectChanges();
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.0.3", ngImport: i0, type: SelectField, deps: [], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "21.0.3", type: SelectField, isStandalone: true, selector: "mt-select-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 }, hasPlaceholderPrefix: { classPropertyName: "hasPlaceholderPrefix", publicName: "hasPlaceholderPrefix", 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 }, pInputs: { classPropertyName: "pInputs", publicName: "pInputs", isSignal: true, isRequired: false, transformFunction: null }, options: { classPropertyName: "options", publicName: "options", isSignal: true, isRequired: false, transformFunction: null }, optionValue: { classPropertyName: "optionValue", publicName: "optionValue", isSignal: true, isRequired: false, transformFunction: null }, optionLabel: { classPropertyName: "optionLabel", publicName: "optionLabel", isSignal: true, isRequired: false, transformFunction: null }, filter: { classPropertyName: "filter", publicName: "filter", isSignal: true, isRequired: false, transformFunction: null }, filterBy: { classPropertyName: "filterBy", publicName: "filterBy", isSignal: true, isRequired: false, transformFunction: null }, dataKey: { classPropertyName: "dataKey", publicName: "dataKey", isSignal: true, isRequired: false, transformFunction: null }, showClear: { classPropertyName: "showClear", publicName: "showClear", isSignal: true, isRequired: false, transformFunction: null }, clearAfterSelect: { classPropertyName: "clearAfterSelect", publicName: "clearAfterSelect", isSignal: true, isRequired: false, transformFunction: null }, required: { classPropertyName: "required", publicName: "required", isSignal: true, isRequired: false, transformFunction: null }, group: { classPropertyName: "group", publicName: "group", isSignal: true, isRequired: false, transformFunction: null }, optionGroupLabel: { classPropertyName: "optionGroupLabel", publicName: "optionGroupLabel", isSignal: true, isRequired: false, transformFunction: null }, optionGroupChildren: { classPropertyName: "optionGroupChildren", publicName: "optionGroupChildren", isSignal: true, isRequired: false, transformFunction: null }, loading: { classPropertyName: "loading", publicName: "loading", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { onChange: "onChange" }, host: { properties: { "class": "this.styleClass" }, classAttribute: "grid gap-1" }, viewQueries: [{ propertyName: "dropdown", first: true, predicate: ["dropdown"], descendants: true, static: true }], usesOnChanges: true, ngImport: i0, template: "@if (label()) {\n  <label\n    (click)=\"dropdown.show(); dropdown.focus()\"\n    [class.required]=\"ngControl?.control?.hasValidator(requiredValidator)\"\n    [for]=\"ngControl?.name || label()\"\n    >{{ label() }}</label\n  >\n}\n<p-select\n  #dropdown\n  appendTo=\"body\"\n  [overlayOptions]=\"{ baseZIndex: 1051 }\"\n  [ngModel]=\"value()\"\n  (ngModelChange)=\"onValueChange($event)\"\n  [options]=\"options()\"\n  [optionLabel]=\"optionLabel()\"\n  [optionValue]=\"optionValue()\"\n  (onBlur)=\"onTouched()\"\n  [disabled]=\"disabled() || readonly()\"\n  [inputId]=\"ngControl?.name ? ngControl?.name?.toString() : label()\"\n  [id]=\"ngControl?.name ? ngControl?.name?.toString() : label()\"\n  [invalid]=\"isInvalid(ngControl?.control)\"\n  placeholder=\"{{\n    (hasPlaceholderPrefix() ? ('components.table.select' | transloco) : '') +\n      ' ' +\n      (placeholder() ?? label() ?? '')\n  }}\"\n  [filter]=\"filter()\"\n  [filterBy]=\"filterBy()\"\n  [dataKey]=\"dataKey()\"\n  styleClass=\"w-full\"\n  [showClear]=\"showClear()\"\n  [group]=\"group()\"\n  [optionGroupLabel]=\"optionGroupLabel()\"\n  [optionGroupChildren]=\"optionGroupChildren()\"\n  [loading]=\"loading()\"\n  appendTo=\"body\"\n></p-select>\n\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: "component", type: Select, selector: "p-select", inputs: ["id", "scrollHeight", "filter", "panelStyle", "styleClass", "panelStyleClass", "readonly", "editable", "tabindex", "placeholder", "loadingIcon", "filterPlaceholder", "filterLocale", "inputId", "dataKey", "filterBy", "filterFields", "autofocus", "resetFilterOnHide", "checkmark", "dropdownIcon", "loading", "optionLabel", "optionValue", "optionDisabled", "optionGroupLabel", "optionGroupChildren", "group", "showClear", "emptyFilterMessage", "emptyMessage", "lazy", "virtualScroll", "virtualScrollItemSize", "virtualScrollOptions", "overlayOptions", "ariaFilterLabel", "ariaLabel", "ariaLabelledBy", "filterMatchMode", "tooltip", "tooltipPosition", "tooltipPositionStyle", "tooltipStyleClass", "focusOnHover", "selectOnFocus", "autoOptionFocus", "autofocusFilter", "filterValue", "options", "appendTo", "motionOptions"], outputs: ["onChange", "onFilter", "onFocus", "onBlur", "onClick", "onShow", "onHide", "onClear", "onLazyLoad"] }, { kind: "component", type: FieldValidation, selector: "mt-field-validation", inputs: ["control", "touched"] }, { kind: "ngmodule", type: TranslocoModule }, { kind: "pipe", type: i2.TranslocoPipe, name: "transloco" }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.3", ngImport: i0, type: SelectField, decorators: [{
            type: Component,
            args: [{ selector: 'mt-select-field', standalone: true, imports: [FormsModule, Select, FieldValidation, TranslocoModule], changeDetection: ChangeDetectionStrategy.OnPush, host: {
                        class: 'grid gap-1',
                    }, template: "@if (label()) {\n  <label\n    (click)=\"dropdown.show(); dropdown.focus()\"\n    [class.required]=\"ngControl?.control?.hasValidator(requiredValidator)\"\n    [for]=\"ngControl?.name || label()\"\n    >{{ label() }}</label\n  >\n}\n<p-select\n  #dropdown\n  appendTo=\"body\"\n  [overlayOptions]=\"{ baseZIndex: 1051 }\"\n  [ngModel]=\"value()\"\n  (ngModelChange)=\"onValueChange($event)\"\n  [options]=\"options()\"\n  [optionLabel]=\"optionLabel()\"\n  [optionValue]=\"optionValue()\"\n  (onBlur)=\"onTouched()\"\n  [disabled]=\"disabled() || readonly()\"\n  [inputId]=\"ngControl?.name ? ngControl?.name?.toString() : label()\"\n  [id]=\"ngControl?.name ? ngControl?.name?.toString() : label()\"\n  [invalid]=\"isInvalid(ngControl?.control)\"\n  placeholder=\"{{\n    (hasPlaceholderPrefix() ? ('components.table.select' | transloco) : '') +\n      ' ' +\n      (placeholder() ?? label() ?? '')\n  }}\"\n  [filter]=\"filter()\"\n  [filterBy]=\"filterBy()\"\n  [dataKey]=\"dataKey()\"\n  styleClass=\"w-full\"\n  [showClear]=\"showClear()\"\n  [group]=\"group()\"\n  [optionGroupLabel]=\"optionGroupLabel()\"\n  [optionGroupChildren]=\"optionGroupChildren()\"\n  [loading]=\"loading()\"\n  appendTo=\"body\"\n></p-select>\n\n<mt-field-validation [control]=\"ngControl?.control\"></mt-field-validation>\n" }]
        }], ctorParameters: () => [], propDecorators: { dropdown: [{
                type: ViewChild,
                args: ['dropdown', { 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 }] }], hasPlaceholderPrefix: [{ type: i0.Input, args: [{ isSignal: true, alias: "hasPlaceholderPrefix", required: false }] }], class: [{ type: i0.Input, args: [{ isSignal: true, alias: "class", required: false }] }], readonly: [{ type: i0.Input, args: [{ isSignal: true, alias: "readonly", required: false }] }], pInputs: [{ type: i0.Input, args: [{ isSignal: true, alias: "pInputs", required: false }] }], options: [{ type: i0.Input, args: [{ isSignal: true, alias: "options", required: false }] }], optionValue: [{ type: i0.Input, args: [{ isSignal: true, alias: "optionValue", required: false }] }], optionLabel: [{ type: i0.Input, args: [{ isSignal: true, alias: "optionLabel", required: false }] }], filter: [{ type: i0.Input, args: [{ isSignal: true, alias: "filter", required: false }] }], filterBy: [{ type: i0.Input, args: [{ isSignal: true, alias: "filterBy", required: false }] }], dataKey: [{ type: i0.Input, args: [{ isSignal: true, alias: "dataKey", required: false }] }], showClear: [{ type: i0.Input, args: [{ isSignal: true, alias: "showClear", required: false }] }], clearAfterSelect: [{ type: i0.Input, args: [{ isSignal: true, alias: "clearAfterSelect", required: false }] }], required: [{ type: i0.Input, args: [{ isSignal: true, alias: "required", required: false }] }], group: [{ type: i0.Input, args: [{ isSignal: true, alias: "group", required: false }] }], optionGroupLabel: [{ type: i0.Input, args: [{ isSignal: true, alias: "optionGroupLabel", required: false }] }], optionGroupChildren: [{ type: i0.Input, args: [{ isSignal: true, alias: "optionGroupChildren", required: false }] }], loading: [{ type: i0.Input, args: [{ isSignal: true, alias: "loading", required: false }] }], onChange: [{
                type: Output
            }], styleClass: [{
                type: HostBinding,
                args: ['class']
            }] } });

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

export { SelectField };
//# sourceMappingURL=masterteam-components-select-field.mjs.map
