import * as i0 from '@angular/core';
import { input, booleanAttribute, contentChild, TemplateRef, signal, computed, inject, effect, ChangeDetectionStrategy, Component } from '@angular/core';
import { Validators, NgControl, FormsModule } from '@angular/forms';
import { RadioCards } from '@masterteam/components/radio-cards';
import { FieldValidation } from '@masterteam/components/field-validation';
import { Tooltip } from '@masterteam/components/tooltip';
import { Icon } from '@masterteam/icons';

class RadioCardsField {
    circle = input(false, { ...(ngDevMode ? { debugName: "circle" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
    label = input(...(ngDevMode ? [undefined, { debugName: "label" }] : /* istanbul ignore next */ []));
    hint = input(...(ngDevMode ? [undefined, { debugName: "hint" }] : /* istanbul ignore next */ []));
    readonly = input(false, ...(ngDevMode ? [{ debugName: "readonly" }] : /* istanbul ignore next */ []));
    required = input(false, ...(ngDevMode ? [{ debugName: "required" }] : /* istanbul ignore next */ []));
    color = input('primary', ...(ngDevMode ? [{ debugName: "color" }] : /* istanbul ignore next */ []));
    size = input('medium', ...(ngDevMode ? [{ debugName: "size" }] : /* istanbul ignore next */ []));
    optionLabel = input('label', ...(ngDevMode ? [{ debugName: "optionLabel" }] : /* istanbul ignore next */ []));
    optionValue = input('value', ...(ngDevMode ? [{ debugName: "optionValue" }] : /* istanbul ignore next */ []));
    options = input.required(...(ngDevMode ? [{ debugName: "options" }] : /* istanbul ignore next */ []));
    itemTemplate = contentChild('option', { ...(ngDevMode ? { debugName: "itemTemplate" } : /* istanbul ignore next */ {}), read: TemplateRef });
    selectedId = signal(null, ...(ngDevMode ? [{ debugName: "selectedId" }] : /* istanbul ignore next */ []));
    isDisabled = signal(false, ...(ngDevMode ? [{ debugName: "isDisabled" }] : /* istanbul ignore next */ []));
    normalizedOptions = computed(() => this.options()
        .map((option) => normalizeRadioItem(option, this.optionLabel(), this.optionValue()))
        .filter((option) => option !== null), ...(ngDevMode ? [{ debugName: "normalizedOptions" }] : /* istanbul ignore next */ []));
    requiredValidator = Validators.required;
    ngControl = null;
    constructor() {
        try {
            this.ngControl = inject(NgControl, { self: true, optional: true });
            if (this.ngControl) {
                this.ngControl.valueAccessor = this;
            }
        }
        catch (_e) {
            this.ngControl = null;
        }
        effect(() => {
            if (this.ngControl?.control && this.required()) {
                this.ngControl.control.addValidators(Validators.required);
                this.ngControl.control.updateValueAndValidity({ emitEvent: false });
            }
        });
    }
    // --- CVA Methods ---
    onChange = () => { };
    onTouched = () => { };
    writeValue(value) {
        this.selectedId.set(value ?? null);
    }
    registerOnChange(fn) {
        this.onChange = fn;
    }
    registerOnTouched(fn) {
        this.onTouched = fn;
    }
    setDisabledState(isDisabled) {
        this.isDisabled.set(isDisabled);
    }
    onSelectionChange(selectedItem) {
        if (this.isDisabled() || this.readonly()) {
            return;
        }
        const newId = selectedItem.id;
        this.selectedId.set(newId);
        const control = this.ngControl?.control;
        if (control) {
            control.setValue(newId);
            control.markAsDirty();
            control.markAsTouched();
            control.updateValueAndValidity({ emitEvent: false });
            return;
        }
        this.onChange(newId);
        this.onTouched();
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.8", ngImport: i0, type: RadioCardsField, deps: [], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "21.2.8", type: RadioCardsField, isStandalone: true, selector: "mt-radio-cards-field", inputs: { circle: { classPropertyName: "circle", publicName: "circle", isSignal: true, isRequired: false, transformFunction: null }, label: { classPropertyName: "label", publicName: "label", isSignal: true, isRequired: false, transformFunction: null }, hint: { classPropertyName: "hint", publicName: "hint", isSignal: true, isRequired: false, transformFunction: null }, readonly: { classPropertyName: "readonly", publicName: "readonly", isSignal: true, isRequired: false, transformFunction: null }, required: { classPropertyName: "required", publicName: "required", isSignal: true, isRequired: false, transformFunction: null }, color: { classPropertyName: "color", publicName: "color", isSignal: true, isRequired: false, transformFunction: null }, size: { classPropertyName: "size", publicName: "size", isSignal: true, isRequired: false, transformFunction: null }, optionLabel: { classPropertyName: "optionLabel", publicName: "optionLabel", isSignal: true, isRequired: false, transformFunction: null }, optionValue: { classPropertyName: "optionValue", publicName: "optionValue", isSignal: true, isRequired: false, transformFunction: null }, options: { classPropertyName: "options", publicName: "options", isSignal: true, isRequired: true, transformFunction: null } }, host: { classAttribute: "grid gap-1" }, providers: [], queries: [{ propertyName: "itemTemplate", first: true, predicate: ["option"], descendants: true, read: TemplateRef, isSignal: true }], ngImport: i0, template: "@if (label() || hint()) {\n  <div class=\"flex items-center gap-1\">\n    @if (label()) {\n      <label\n        [class.required]=\"ngControl?.control?.hasValidator(requiredValidator)\"\n        [for]=\"ngControl?.name || label()\"\n        >{{ label() }}</label\n      >\n    }\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\" />\n      </span>\n    }\n  </div>\n}\n<div\n  class=\"relative\"\n  [class.opacity-50]=\"isDisabled()\"\n  [class.pointer-events-none]=\"isDisabled()\"\n  [id]=\"ngControl?.name || label()\"\n>\n  <mt-radio-cards\n    [options]=\"normalizedOptions()\"\n    [activeId]=\"selectedId()\"\n    [color]=\"color()\"\n    [size]=\"size()\"\n    [circle]=\"circle()\"\n    (selectionChange)=\"onSelectionChange($event)\"\n    [itemTemplate]=\"itemTemplate()\"\n  ></mt-radio-cards>\n</div>\n\n<mt-field-validation [control]=\"ngControl?.control\"></mt-field-validation>\n", styles: [""], dependencies: [{ kind: "ngmodule", type: FormsModule }, { kind: "component", type: RadioCards, selector: "mt-radio-cards", inputs: ["circle", "color", "size", "columns", "options", "activeId", "itemTemplate"], outputs: ["optionsChange", "activeIdChange", "selectionChange"] }, { 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: RadioCardsField, decorators: [{
            type: Component,
            args: [{ selector: 'mt-radio-cards-field', standalone: true, imports: [FormsModule, RadioCards, FieldValidation, Tooltip, Icon], changeDetection: ChangeDetectionStrategy.OnPush, host: {
                        class: 'grid gap-1',
                    }, providers: [], template: "@if (label() || hint()) {\n  <div class=\"flex items-center gap-1\">\n    @if (label()) {\n      <label\n        [class.required]=\"ngControl?.control?.hasValidator(requiredValidator)\"\n        [for]=\"ngControl?.name || label()\"\n        >{{ label() }}</label\n      >\n    }\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\" />\n      </span>\n    }\n  </div>\n}\n<div\n  class=\"relative\"\n  [class.opacity-50]=\"isDisabled()\"\n  [class.pointer-events-none]=\"isDisabled()\"\n  [id]=\"ngControl?.name || label()\"\n>\n  <mt-radio-cards\n    [options]=\"normalizedOptions()\"\n    [activeId]=\"selectedId()\"\n    [color]=\"color()\"\n    [size]=\"size()\"\n    [circle]=\"circle()\"\n    (selectionChange)=\"onSelectionChange($event)\"\n    [itemTemplate]=\"itemTemplate()\"\n  ></mt-radio-cards>\n</div>\n\n<mt-field-validation [control]=\"ngControl?.control\"></mt-field-validation>\n" }]
        }], ctorParameters: () => [], propDecorators: { circle: [{ type: i0.Input, args: [{ isSignal: true, alias: "circle", required: false }] }], label: [{ type: i0.Input, args: [{ isSignal: true, alias: "label", required: false }] }], hint: [{ type: i0.Input, args: [{ isSignal: true, alias: "hint", required: false }] }], readonly: [{ type: i0.Input, args: [{ isSignal: true, alias: "readonly", required: false }] }], required: [{ type: i0.Input, args: [{ isSignal: true, alias: "required", required: false }] }], color: [{ type: i0.Input, args: [{ isSignal: true, alias: "color", required: false }] }], size: [{ type: i0.Input, args: [{ isSignal: true, alias: "size", required: false }] }], optionLabel: [{ type: i0.Input, args: [{ isSignal: true, alias: "optionLabel", required: false }] }], optionValue: [{ type: i0.Input, args: [{ isSignal: true, alias: "optionValue", required: false }] }], options: [{ type: i0.Input, args: [{ isSignal: true, alias: "options", required: true }] }], itemTemplate: [{ type: i0.ContentChild, args: ['option', { ...{ read: TemplateRef }, isSignal: true }] }] } });
function normalizeRadioItem(option, optionLabel, optionValue) {
    if (option === null || option === undefined) {
        return null;
    }
    if (typeof option === 'string' || typeof option === 'number') {
        return {
            id: option,
            name: String(option),
        };
    }
    if (typeof option !== 'object') {
        return null;
    }
    const source = option;
    const id = readRadioItemValue(source, optionValue, ['id', 'value']);
    if (typeof id !== 'string' && typeof id !== 'number') {
        return null;
    }
    const name = readRadioItemValue(source, optionLabel, ['name', 'label']);
    return {
        ...source,
        id,
        name: typeof name === 'string' ? name : String(name ?? id),
    };
}
function readRadioItemValue(source, preferredKey, fallbackKeys) {
    const candidateKeys = [preferredKey, ...fallbackKeys].filter(Boolean);
    for (const key of candidateKeys) {
        if (key in source) {
            return source[key];
        }
    }
    return undefined;
}

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

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