import { EventEmitter, Component, ChangeDetectionStrategy, ChangeDetectorRef, Input, Output, NgModule } from '@angular/core';
import { FormControl, NgForm, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { MatSlideToggleModule } from '@angular/material/slide-toggle';
import { __classPrivateFieldSet, __classPrivateFieldGet } from 'tslib';
import { Subscription } from 'rxjs';
import { v4 } from 'uuid';
import { MatFormFieldModule } from '@angular/material/form-field';

var _name, _form, _subscription, _onChange, _updateValidator;
class SdSwitch {
    constructor(ref) {
        this.ref = ref;
        _name.set(this, v4());
        _form.set(this, void 0);
        this.color = 'primary';
        this.modelChange = new EventEmitter();
        this.sdChange = new EventEmitter();
        this.formControl = new FormControl();
        _subscription.set(this, new Subscription());
        _onChange.set(this, (value) => {
            this.modelChange.emit(value);
            this.sdChange.emit(value);
        });
        _updateValidator.set(this, () => {
            this.formControl.clearValidators();
            const validators = [];
            if (this.inlineError) {
                validators.push(this.customInlineErrorValidator());
            }
            this.formControl.setValidators(validators);
            this.formControl.updateValueAndValidity();
        }
        // Hàm tạo Validators tùy chỉnh cho inlineError
        );
    }
    set form(val) {
        if (val) {
            if (val instanceof NgForm) {
                __classPrivateFieldSet(this, _form, val.form);
            }
            else {
                __classPrivateFieldSet(this, _form, val);
            }
        }
    }
    set disabled(val) {
        val = (val === '') || val;
        if (val) {
            this.formControl.disable();
        }
        else {
            this.formControl.enable();
        }
    }
    set model(value) {
        if (this.formControl.value !== value) {
            this.formControl.setValue(value, {
                emitEvent: false
            });
        }
    }
    set _inlineError(val) {
        this.inlineError = val;
        __classPrivateFieldGet(this, _updateValidator).call(this);
    }
    ngOnInit() {
    }
    ngAfterViewInit() {
        var _a;
        __classPrivateFieldGet(this, _subscription).add(this.formControl.valueChanges.subscribe(__classPrivateFieldGet(this, _onChange)));
        (_a = __classPrivateFieldGet(this, _form)) === null || _a === void 0 ? void 0 : _a.addControl(__classPrivateFieldGet(this, _name), this.formControl);
        this.ref.detectChanges();
    }
    ngOnDestroy() {
        var _a;
        (_a = __classPrivateFieldGet(this, _form)) === null || _a === void 0 ? void 0 : _a.removeControl(__classPrivateFieldGet(this, _name));
        __classPrivateFieldGet(this, _subscription).unsubscribe();
    }
    // Hàm tạo Validators tùy chỉnh cho inlineError
    customInlineErrorValidator() {
        return (control) => {
            return { inlineError: true };
        };
    }
}
_name = new WeakMap(), _form = new WeakMap(), _subscription = new WeakMap(), _onChange = new WeakMap(), _updateValidator = new WeakMap();
SdSwitch.decorators = [
    { type: Component, args: [{
                selector: 'sd-switch',
                template: "<mat-slide-toggle [formControl]=\"formControl\" [color]=\"color\"><span\r\n    class=\"c-label\">{{label}}</span></mat-slide-toggle>\r\n    <mat-error *ngIf=\"formControl.errors?.inlineError && formControl?.touched\">\r\n      {{inlineError}}\r\n    </mat-error>",
                changeDetection: ChangeDetectionStrategy.OnPush,
                styles: [""]
            },] }
];
SdSwitch.ctorParameters = () => [
    { type: ChangeDetectorRef }
];
SdSwitch.propDecorators = {
    size: [{ type: Input }],
    form: [{ type: Input }],
    label: [{ type: Input }],
    color: [{ type: Input }],
    disabled: [{ type: Input }],
    model: [{ type: Input }],
    _inlineError: [{ type: Input, args: ['inlineError',] }],
    modelChange: [{ type: Output }],
    sdChange: [{ type: Output }]
};

class SdSwitchModule {
}
SdSwitchModule.decorators = [
    { type: NgModule, args: [{
                imports: [
                    CommonModule,
                    FormsModule,
                    ReactiveFormsModule,
                    MatSlideToggleModule,
                    MatFormFieldModule
                ],
                declarations: [
                    SdSwitch
                ],
                exports: [
                    SdSwitch
                ]
            },] }
];

/*
 * Public API Surface of superdev-angular-core
 */

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

export { SdSwitch, SdSwitchModule };
//# sourceMappingURL=sd-angular-core-switch.js.map
