import { SdFormControl, SdLabelDefDirective } from '@sd-angular/core/common';
import { EventEmitter, Component, ChangeDetectionStrategy, ChangeDetectorRef, Input, ContentChild, Output, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Validators, NgForm, ReactiveFormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatIconModule } from '@angular/material/icon';
import { MatTooltipModule } from '@angular/material/tooltip';
import { __classPrivateFieldGet, __classPrivateFieldSet } from 'tslib';
import { Subscription } from 'rxjs';
import { v4 } from 'uuid';
import { SdTranslateModule } from '@sd-angular/core/translate';
import { MatRadioModule } from '@angular/material/radio';

var _name, _form, _model, _required, _subscription, _updateValidator;
class SdRadio {
    constructor(ref) {
        this.ref = ref;
        _name.set(this, v4());
        this.formControl = new SdFormControl();
        // get isNumber() {
        //   if (this.#model || this.#model === 0) {
        //     return typeof (this.#model) === 'number';
        //   }
        //   if (this.items?.length) {
        //     if (this.valueField) {
        //       return typeof (this.items[0][this.valueField]) === 'number';
        //     } else {
        //       return typeof (this.items[0]) === 'number';
        //     }
        //   }
        //   return false;
        // }
        _form.set(this, void 0);
        this.display = 'row';
        // Model
        _model.set(this, void 0);
        // Items
        this.items = [];
        // Validator
        _required.set(this, false);
        this.modelChange = new EventEmitter();
        this.sdChange = new EventEmitter();
        this.sdSelection = new EventEmitter();
        _subscription.set(this, new Subscription());
        _updateValidator.set(this, () => {
            this.formControl.clearValidators();
            const validators = [];
            if (__classPrivateFieldGet(this, _required)) {
                validators.push(Validators.required);
            }
            this.formControl.setValidators(validators);
            this.formControl.updateValueAndValidity();
        });
        this.reValidate = () => {
            this.formControl.updateValueAndValidity({ emitEvent: true });
        };
    }
    set name(val) {
        if (val) {
            __classPrivateFieldSet(this, _name, val);
        }
    }
    set form(val) {
        if (val) {
            if (val instanceof NgForm) {
                __classPrivateFieldSet(this, _form, val.form);
            }
            else {
                __classPrivateFieldSet(this, _form, val);
            }
        }
    }
    set model(value) {
        __classPrivateFieldSet(this, _model, value);
        if (value !== this.formControl.value) {
            this.formControl.setValue(value, {
                emitEvent: false
            });
        }
    }
    set pItems(items) {
        if (!Array.isArray(items)) {
            this.items = [];
        }
        this.items = items;
    }
    set required(val) {
        __classPrivateFieldSet(this, _required, (val === '') || val);
        __classPrivateFieldGet(this, _updateValidator).call(this);
    }
    // Optional
    set disabled(val) {
        val = (val === '') || val;
        if (val) {
            this.formControl.disable();
        }
        else {
            this.formControl.enable();
        }
    }
    ngOnInit() {
        __classPrivateFieldGet(this, _subscription).add(this.formControl.sdChanges.subscribe(() => {
            // this.formControl.updateValueAndValidity();
            this.ref.markForCheck();
        }));
    }
    ngAfterViewInit() {
        var _a;
        __classPrivateFieldGet(this, _subscription).add(this.formControl.valueChanges.subscribe(value => {
            var _a;
            let val = value;
            // if (this.isNumber && Number.isNumber(value)) {
            //   val = +value;
            // }
            this.modelChange.emit(val);
            this.sdChange.emit(val);
            this.sdSelection.emit({
                value: val,
                item: (_a = this.items) === null || _a === void 0 ? void 0 : _a.find(e => { var _a; return (val === null || val === void 0 ? void 0 : val.toString()) === ((_a = e === null || e === void 0 ? void 0 : e[this.valueField]) === null || _a === void 0 ? void 0 : _a.toString()); })
            });
        }));
        (_a = __classPrivateFieldGet(this, _form)) === null || _a === void 0 ? void 0 : _a.addControl(__classPrivateFieldGet(this, _name), this.formControl);
    }
    ngOnDestroy() {
        var _a;
        __classPrivateFieldGet(this, _subscription).unsubscribe();
        (_a = __classPrivateFieldGet(this, _form)) === null || _a === void 0 ? void 0 : _a.removeControl(__classPrivateFieldGet(this, _name));
    }
}
_name = new WeakMap(), _form = new WeakMap(), _model = new WeakMap(), _required = new WeakMap(), _subscription = new WeakMap(), _updateValidator = new WeakMap();
SdRadio.decorators = [
    { type: Component, args: [{
                selector: 'sd-radio',
                template: "<section [ngClass]=\"{'c-section': display == 'row'}\">\r\n  <label *ngIf=\"label&&!sdLabelDef?.templateRef\" class=\"d-block T14M\" [class.mb-0]=\"display === 'row'\">{{label}} <span class=\"text-danger mb-2\" *ngIf=\"required\">*</span></label>\r\n  <ng-container *ngIf=\"sdLabelDef?.templateRef\" matSuffix>\r\n    <ng-container *ngTemplateOutlet=\"sdLabelDef.templateRef\">\r\n    </ng-container>\r\n  </ng-container>\r\n  <mat-radio-group [ngClass]=\"{'c-radio-group-column': display === 'column', 'c-radio-group-row': display === 'row'}\"\r\n    [formControl]=\"formControl\">\r\n    <mat-radio-button color=\"primary\" [ngClass]=\"{'m-0': display === 'column', 'mr-16': display === 'row'}\"\r\n      *ngFor=\"let item of items\" [value]=\"item[valueField]\">\r\n      {{item[displayField]}}\r\n    </mat-radio-button>\r\n  </mat-radio-group>\r\n</section>",
                changeDetection: ChangeDetectionStrategy.OnPush,
                styles: [":host{display:flex;flex-direction:column;height:100%}.c-section{align-content:center;align-items:center}.c-radio-group-column{align-items:flex-start;display:flex;flex:1;flex-direction:column}.c-radio-group-row{align-items:center;display:flex;flex:1;flex-direction:row}"]
            },] }
];
SdRadio.ctorParameters = () => [
    { type: ChangeDetectorRef }
];
SdRadio.propDecorators = {
    name: [{ type: Input }],
    form: [{ type: Input }],
    label: [{ type: Input }],
    placeholder: [{ type: Input }],
    display: [{ type: Input }],
    model: [{ type: Input }],
    pItems: [{ type: Input, args: ['items',] }],
    valueField: [{ type: Input }],
    displayField: [{ type: Input }],
    required: [{ type: Input }],
    disabled: [{ type: Input }],
    sdLabelDef: [{ type: ContentChild, args: [SdLabelDefDirective,] }],
    modelChange: [{ type: Output }],
    sdChange: [{ type: Output }],
    sdSelection: [{ type: Output }]
};

class SdRadioModule {
}
SdRadioModule.decorators = [
    { type: NgModule, args: [{
                imports: [
                    CommonModule,
                    ReactiveFormsModule,
                    MatTooltipModule,
                    MatFormFieldModule,
                    MatIconModule,
                    MatRadioModule,
                    SdTranslateModule
                ],
                declarations: [
                    SdRadio
                ],
                exports: [
                    SdRadio
                ],
                providers: []
            },] }
];

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

export { SdRadio, SdRadioModule };
//# sourceMappingURL=sd-angular-core-radio.js.map
